neena
neena

Reputation: 31

ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type float)

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'

Upvotes: 1

Views: 6035

Answers (2)

Luke
Luke

Reputation: 459

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)

Upvotes: 0

geeky
geeky

Reputation: 71

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()

Upvotes: 1

Related Questions