RonSg83
RonSg83

Reputation: 151

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

Upvotes: 15

Views: 45390

Answers (4)

Azhar Khan
Azhar Khan

Reputation: 4098

It is possible that your data has one or more non-float (possibly string) columns. 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 = [
  [.1, .2, ".3"],
  [.4, .5, ".6"],
  [.7, .8, ".9"],
]
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   ".3"
>> 1   0.4   0.5   ".6"
>> 2   0.7   0.8   ".9"


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

Note: Column `x3` above has string type.


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 float).

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

# X_train = X_train.apply(pd.to_numeric) 
# OR 
X_train["x3"] = X_train["x3"].apply(pd.to_numeric) 


print(X_train)
>>      x1    x2    x3
>> 0   0.1   0.2   0.3
>> 1   0.4   0.5   0.6
>> 2   0.7   0.8   0.9


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

Note: Now, column `x3` has float type.


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

Converting the respective column(s) or the entire dataframe to numeric type using any of the following solution solves the issue:

df = df.apply(pd.to_numeric) 

df["my_col"] = df[["my_col"]].apply(pd.to_numeric)

df = df.to_numpy().astype(np.float32)

df = df.to_numpy().astype("float")

Also make sure there are no NaN, na or null values in column.

The issue can also be with the shape of the input or shape of data elements in the input. Make sure the shapes are consistent.

Upvotes: 0

mike g
mike g

Reputation: 109

I had this error message too. My problem was there were few NULL characters in my input file which I imported into the dataframe that fed Keras/tensorflow.

I knew there were NULLs because:

df.isnull().any()    ## check for nulls ... should say False

... told me there were NULLS (i.e TRUE)

To remove the offending NULLs, I used this:

df = df.dropna(how='any',axis=0) 

... where df was my numpy dataframe.

After that my model.fit ran nicely!

Of course, the error message "Failed to convert a NumPy array to a Tensor (Unsupported object type float)" could have many causes.

My root problem was funky input data. The code above fixed it.

Upvotes: 0

Surya Narayanan
Surya Narayanan

Reputation: 455

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']

Upvotes: 3

JustMe
JustMe

Reputation: 208

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

np.array(*your list*, dtype=np.float)

Upvotes: 18

Related Questions