Custom CTC loss function in Keras/Tensorflow

465 Views Asked by At

I feel like I'm fundamentally misunderstanding something. I went through the Keras documentation to no avail. I'm trying to implement the ctc_batch_cost loss for my neural network. My neural network ends with an LSTM layer which returns sequences into a dense layer with 4+1 symbols softmax output. The output shape has 20 time steps and looks as follows:

(. . .)
_________________________________________________________________
lstm_1 (LSTM)                (None, 20, 32)            8320      
_________________________________________________________________
dense_1 (Dense)              (None, 20, 5)             165       
=================================================================

My labels are simply the string that should be output, of variable lengths.

Here is my attempt at the CTC function:

def ctc_custom(prediction_batch, label_batch):
    pred_batch_size = prediction_batch.get_shape().as_list() # returns (?,?,?) at compile time
    label_batch_size = label_batch.get_shape().as_list()
    samples = pred_batch_size[0]

    input_length = tf.placeholder([[20] for x in range(samples)])
    label_length = tf.placeholder([[len(l)] for l in label_batch_size])
    return ctc_batch_cost(label_batch, prediction_batch, input_length, label_length)

Now, of course, Tensorflow currently errors with the following:

TypeError: 'NoneType' object cannot be interpreted as an integer

which is understandable since at compile time variables such as samples will be None. But I am at a loss about how to use this. Any hints are appreciated. I want to understand what's happening, not just get an easy fix. I tried testing for None and returning a placeholder but that didn't work either and felt like a hack to begin with.

Thank you

0

There are 0 best solutions below