Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I know this problem has been answered previously in the link below,but it does not apply to my situation.(Tensorflow - ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type float))

Both my predictor (X) and target variables (y) are <class 'numpy.ndarray'> and their shapes are X: (8981, 25) y: (8981, 1)

Yet, I am still getting the error message. ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type float).

Please refer to the following code:

import tensorflow as tf
ndim = X.shape[1]
model = tf.keras.models.Sequential()
# model.add(tf.keras.layers.Flatten())
model.add(tf.keras.layers.Dense(36, activation = tf.nn.relu, input_dim=ndim))
model.add(tf.keras.layers.Dense(36, activation = tf.nn.relu))
model.add(tf.keras.layers.Dense(2, activation = tf.nn.softmax))
model.compile(optimizer = 'adam',
              loss = 'sparse_categorical_crossentropy',
              metrics = ['accuracy'])
model.fit(X.values, y, epochs = 5)
y_pred = model.predict([X_2019])

Any help will be really appreciated! Thanks!!!

like image 602
RonSg83 Avatar asked Jan 17 '20 18:01

RonSg83


2 Answers

Try inserting dtype=np.float when creating the np array:

np.array(*your list*, dtype=np.float)
like image 117
Jacopo Avatar answered Oct 01 '22 02:10

Jacopo


Some of my columns were categorical. Try printing X.dtypes and checking if any of the entries are as type 'object'. Another helpful command: X[X.dtypes=='object']

like image 20
Surya Narayanan Avatar answered Oct 01 '22 02:10

Surya Narayanan