1

Im facing problem with this line of code in keras with backend Tensorflow 2.0:

loss_out = Lambda(function=ctc_lambda_func, name='ctc', output_shape=(1,))([y_pred, Y_train, X_train_length, label_length])

Y_train, X_train_length are numpy.ndarrays y_pred and label_length are class 'tensorflow.python.framework.ops.Tensor'

2 Answers 2

1

You can use

        tf.convert_to_tensor()

example,

        import tensorflow as tf
        import numpy as np


        loss = Lambda(function=ctc_lambda_func, name='ctc', output_shape=(1,)) 
                       ([y_pred, Y_train, X_train_length, label_length])
        loss_np = np.asarray(loss, np.float32)

        loss_tf = tf.convert_to_tensor(loss_np, np.float32)

        sess = tf.InteractiveSession()  
        print(loss_tf.eval())

        sess.close()
Sign up to request clarification or add additional context in comments.

6 Comments

this line is not getting executed. Geeting value error: not able to convert numpy array to tensor loss = Lambda(function=ctc_lambda_func, name='ctc', output_shape=(1,)) ([y_pred, Y_train, X_train_length, label_length])
@neena yeah what in that line? Can you elaborate your question or issue.
this line [loss = Lambda(function=ctc_lambda_func, name='ctc', output_shape=(1,)) ([y_pred, Y_train, X_train_length, label_length])] is not getting executed. Getting value error: not able to convert numpy array to tensor
actually i just wrote loss not (loss_out) so change it according what you have written in your code.
I tried this: loss_out = Lambda(function=ctc_lambda_func, name='ctc', output_shape=(1,))([tf.convert_to_tensor(y_pred, np.float32), tf.convert_to_tensor(Y_train, np.float32), tf.convert_to_tensor(X_train_length, np.float32), tf.convert_to_tensor(label_length, np.float32)]) Now getting error : ValueError: setting an array element with a sequence.
|
0

You can create dummy inputs

# you have defined the rest of your graph somewhere here

Y_train = Input(shape=...)
X_train_length = Input(shape=...)

loss = Lambda(function=ctc_lambda_func, name='ctc', output_shape=(1,)
              )([y_pred, Y_train, X_train_length, label_length])

# defining the model is slightly different with multiple inputs
training_model = Model(inputs=[image_input, Y_train, X_train_length], outputs=[loss])

And when you want to train your model you will pass the parameter x as a list of length 3, such as

x = [<images - np.ndarray shape (batch, h, w, c)>, <Y_train inputs - np.ndarray>,
     <X_train_length inputs - np.ndarray>]

And of course dummy values for y

y = np.zeros((batch, 1))

And it's never been simpler finally than training_model.train_on_batch(x, y)

Alternatively make a generator that generates x and y in the form described above and use training_model.fit_generator(data_generator)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.