2

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
  • 197
  • 11

1 Answers1

1

It is possible that your data has numpy array as elements instead of float. You should analyse your data.

Following example reproduces the same problem:

import numpy as np
import pandas as pd
from tensorflow.keras.layers import Dense
from tensorflow.keras.models import Sequential

data = [
  [np.asarray([.1]),np.asarray([.2]),np.asarray([.3])],
  [np.asarray([.1]),np.asarray([.2]),np.asarray([.3])],
  [np.asarray([.1]),np.asarray([.2]),np.asarray([.3])],
]
X_train = pd.DataFrame(data=data, columns=["x1","x2","x3"])
y_train = pd.DataFrame(data=[1,0,1], columns=["y"])


print(X_train)
>>       x1     x2     x3
>> 0  [0.1]  [0.2]  [0.3]
>> 1  [0.1]  [0.2]  [0.3]
>> 2  [0.1]  [0.2]  [0.3]


print(X_train.dtypes)
>> x1    object
>> x2    object
>> x3    object
>> dtype: object


model = Sequential()
model.add(Dense(1, input_dim=X_train.shape[1], activation='sigmoid'))
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
model.fit(X_train.to_numpy(), y_train, epochs=3)

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

If the above dataframe is fixed as follows, the MLP model works just fine:

from pandas.core.common import flatten
X_train = X_train.apply(lambda x: pd.Series(flatten(x)))

print(X_train)
>>     x1   x2   x3
>> 0  0.1  0.2  0.3
>> 1  0.1  0.2  0.3
>> 2  0.1  0.2  0.3


model = Sequential()
model.add(Dense(1, input_dim=X_train.shape[1], activation='sigmoid'))
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
model.fit(X_train.to_numpy(), y_train, epochs=3, verbose=3)
>> Epoch 1/3
>> Epoch 2/3
>> Epoch 3/3
Azhar Khan
  • 3,829
  • 11
  • 26
  • 32