1

as a continuation to Tensorflow - ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type float)

I had a similar issue where I had the following error

ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type numpy.ndarray).

I followed the different suggestions but it does not seem to solve my problem.

all the values below are <class 'numpy.ndarray'>

train_inputs=df_train_title_train
train_targets=y_train.to_numpy()
validation_inputs=df_train_title_test
validation_targets=y_test.to_numpy()

shapes are (63586,), (63586, 9), (7066,), (7066, 9) respectively where 9 is the number of class I am trying to classify

# Set the input and output sizes
input_size = 64
output_size = 9
# Use same hidden layer size for both hidden layers. Not a necessity.
hidden_layer_size = 64


# define how the model will look like
model = tf.keras.Sequential([
    tf.keras.layers.Dense(hidden_layer_size, activation='relu'), # 1st hidden layer
    tf.keras.layers.Dense(hidden_layer_size, activation='relu'), # 2nd hidden layer
    tf.keras.layers.Dense(hidden_layer_size, activation='relu'), # 2nd hidden layer
    tf.keras.layers.Dense(output_size, activation='softmax') # output layer
])


model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])


### Training
# That's where we train the model we have built.

# set the batch size
batch_size = 10

# set a maximum number of training epochs
max_epochs = 10

# fit the model
# note that this time the train, validation and test data are not iterable
model.fit(train_inputs, # train inputs
          train_targets, # train targets
          batch_size=batch_size, # batch size
          epochs=max_epochs, # epochs that we will train for (assuming early stopping doesn't kick in)
          validation_data=(validation_inputs, validation_targets), # validation data
          verbose = 2 # making sure we get enough information about the training process
          )  

Finally the error looks like this.

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-75-10183099f9ec> in <module>()
      6           epochs=max_epochs, # epochs that we will train for (assuming early stopping doesn't kick in)
      7           validation_data=(validation_inputs, validation_targets), # validation data
----> 8           verbose = 2 # making sure we get enough information about the training process
      9           )  

16 frames
/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/constant_op.py in convert_to_eager_tensor(value, ctx, dtype)
     96       dtype = dtypes.as_dtype(dtype).as_datatype_enum
     97   ctx.ensure_initialized()
---> 98   return ops.EagerTensor(value, ctx.device_name, dtype)
     99 
    100 

ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type numpy.ndarray).
unaied
  • 167
  • 8

0 Answers0