14

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

6
  • 1
    Can you add the complete traceback to your question?
    – Dr. Snoopy
    Jan 17, 2020 at 18:47
  • Also, are you sure your X and y are numpy arrays? np.array's do not have a .values attribute.
    – Dr. Snoopy
    Jan 17, 2020 at 18:55
  • Yes, byt X.values is an array, isn't it?
    – RonSg83
    Jan 30, 2020 at 2:58
  • Depends on what X is.
    – Dr. Snoopy
    Jan 30, 2020 at 4:58
  • X is a pandas dataframe with float entries
    – RonSg83
    Jan 31, 2020 at 5:10

4 Answers 4

18

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

np.array(*your list*, dtype=np.float)
1
  • @ronsg83 please mark as an answer if it helped :)
    – JustMe
    Jan 10, 2021 at 16:13
3

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

0

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.

0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.