Last updated: Apr 12, 2024
Reading time·6 min
The Tensorflow "ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type float)" occurs when you try to convert a Python list or a NumPy array that doesn't contain all float values to a Tensor.
To solve the error, convert the values in the list/array to float32
numbers
before converting to a tensor.
Here is an example of how the error occurs.
import numpy as np import pandas as pd from tensorflow.keras.layers import Dense from tensorflow.keras.models import Sequential # ⛔️ Training data has non-float values data = [ [0.1, 0.2, '0.3'], [0.1, 0.2, '0.3'], [0.1, 0.2, '0.3'], ] X_train = pd.DataFrame(data=data, columns=["x1", "x2", "x3"]) y_train = pd.DataFrame(data=[1, 0, 1], columns=["y"]) print(X_train) print(X_train.dtypes) model = Sequential() model.add( Dense(1, input_dim=X_train.shape[1], activation='sigmoid') ) model.compile( optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'] ) # ⛔️ ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type float). model.fit(X_train.to_numpy(), y_train, epochs=3)
Running the code sample produces the following output.
x1 x2 x3 0 0.1 0.2 0.3 1 0.1 0.2 0.3 2 0.1 0.2 0.3 x1 float64 x2 float64 x3 object # 👈️ non-float value dtype: object ⛔️ ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type float).
Notice that the data
variable doesn't store all float values.
This causes an error when we try to train the model with model.fit()
.
float32
One way to solve the error is to convert all values in the list/array to
float32
using
ndarray.astype.
import numpy as np import pandas as pd from tensorflow.keras.layers import Dense from tensorflow.keras.models import Sequential data = [ [0.1, 0.2, '0.3'], [0.1, 0.2, '0.3'], [0.1, 0.2, '0.3'], ] data = np.asarray(data).astype('float32') X_train = pd.DataFrame(data=data, columns=["x1", "x2", "x3"]) y_train = pd.DataFrame(data=[1, 0, 1], columns=["y"]) print(X_train) print(X_train.dtypes) 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)
Running the code sample produces the following output.
x1 x2 x3 0 0.1 0.2 0.3 1 0.1 0.2 0.3 2 0.1 0.2 0.3 x1 float32 x2 float32 x3 float32 dtype: object
The following line converts the values in the data
list to float32
.
import numpy as np data = np.asarray(data).astype('float32')
We could've also solved the error by converting the X_train
DataFrame to a
NumPy array with float32
values.
import numpy as np import pandas as pd from tensorflow.keras.layers import Dense from tensorflow.keras.models import Sequential data = [ [0.1, 0.2, '0.3'], [0.1, 0.2, '0.3'], [0.1, 0.2, '0.3'], ] X_train = pd.DataFrame(data=data, columns=["x1", "x2", "x3"]) # ✅ Convert to an Array with `float32` values X_train = np.array(X_train).astype('float32') y_train = pd.DataFrame(data=[1, 0, 1], columns=["y"]) 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, y_train, epochs=3)
The following line is important:
X_train = np.array(X_train).astype('float32')
You can also use the following line if you don't want to hardcode the type.
X_train = np.asarray(X_train).astype(np.float32)
Notice that we are no longer calling X_train.to_numpy()
in the call to
model.fit()
because we already converted the DataFrame
to an array.
Note that you should be using NumPy arrays as inputs and not native Python lists.
Keras/TensorFlow doesn't support using native Python lists as inputs.
You can convert a Python list to a NumPy array by using np.asarray()
.
x_array = np.asarray(x_list)
If the error persists, try to also convert y_train
to a NumPy array of
floating-point numbers.
import numpy as np import pandas as pd from tensorflow.keras.layers import Dense from tensorflow.keras.models import Sequential data = [ [0.1, 0.2, '0.3'], [0.1, 0.2, '0.3'], [0.1, 0.2, '0.3'], ] X_train = pd.DataFrame(data=data, columns=["x1", "x2", "x3"]) # 1) Convert X_train to NumPy array of floats ✅ X_train = np.asarray(X_train).astype(np.float32) y_train = pd.DataFrame(data=[1, 0, 1], columns=["y"]) # 1) Convert y_train to NumPy array of floats ✅ y_train = np.asarray(y_train).astype(np.float32) 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, y_train, epochs=3)
You might also get the error if you have missing values in a numeric or a string
DataFrame
column.
Here is an example.
import numpy as np import pandas as pd from tensorflow.keras.layers import Dense from tensorflow.keras.models import Sequential # 👇️ Has missing values data = [ [0.1, 0.2, None], [0.1, 0.2, None], [0.1, 0.2, None], ] X_train = pd.DataFrame(data=data, columns=["x1", "x2", "x3"]) y_train = pd.DataFrame(data=[1, 0, 1], columns=["y"]) model = Sequential() model.add( Dense(1, input_dim=X_train.shape[1], activation='sigmoid') ) model.compile( optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'] ) # ⛔️ ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type float). model.fit(X_train, y_train, epochs=3)
Note that we have some missing values in the data
variable.
This can be caused by None
, np.nan
values, etc.
You can use the DataFrame.fillna()
method to solve the error in this case.
import numpy as np import pandas as pd from tensorflow.keras.layers import Dense from tensorflow.keras.models import Sequential # 👇️ Has missing values data = [ [0.1, 0.2, None], [0.1, 0.2, None], [0.1, 0.2, None], ] X_train = pd.DataFrame(data=data, columns=["x1", "x2", "x3"]) # ✅ Fill missing values using fillna() X_train.fillna(value=0, inplace=True) y_train = pd.DataFrame(data=[1, 0, 1], columns=["y"]) 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, y_train, epochs=3)
The code sample uses the DataFrame.fillna()
method to fill the missing values.
# ✅ Fill missing values using fillna() X_train.fillna(value=0, inplace=True)
If you have a string column that has missing values, you would likely want to
set the value
argument to an empty string.
# ✅ Fill missing values using fillna() df.fillna(value='', inplace=True)
You can also call the fillna()
method on the specific column.
# for a string column df['YourColumn'].fillna(value="", inplace=True) # or for a numeric column df['YourColumn'].fillna(value=0, inplace=True)
If you want to call fillna()
on all string columns in the DataFrame
, use a
for
loop.
cols = df.select_dtypes(include=['object']) for column in cols.columns.values: # for string values df[column] = df[column].fillna('') # or for numeric values # df[column] = df[column].fillna(0)
tf.convert_to_tensor()
to solve the errorYou can also use the tensorflow.convert_to_tensor()
method to solve the error.
import numpy as np import pandas as pd from tensorflow.keras.layers import Dense from tensorflow.keras.models import Sequential import tensorflow as tf data = [ [0.1, 0.2, '0.3'], [0.1, 0.2, '0.3'], [0.1, 0.2, '0.3'], ] X_train = pd.DataFrame(data=data, columns=["x1", "x2", "x3"]) y_train = pd.DataFrame(data=[1, 0, 1], columns=["y"]) model = Sequential() model.add( Dense(1, input_dim=X_train.shape[1], activation='sigmoid') ) model.compile( optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'] ) # 👇️ Using tf.convert_to_tensor model.fit( tf.convert_to_tensor(X_train.to_numpy(), dtype=tf.float32), y_train, epochs=3 )
The
tf.convert_to-tensor()
method converts a given value to a Tensor
.
The method takes a dtype
argument that can be set to tf.float32
.
The dtype
argument specifies the element type for the returned tensor.
If you don't explicitly set the dtype
argument, the type is inferred from the
type of the supplied value.
You could also convert the X_train
and y_train
variables to tensors using
convert_to_tensor
and pass them directly to model.fit()
.
import numpy as np import pandas as pd from tensorflow.keras.layers import Dense from tensorflow.keras.models import Sequential import tensorflow as tf data = [ [0.1, 0.2, '0.3'], [0.1, 0.2, '0.3'], [0.1, 0.2, '0.3'], ] X_train = pd.DataFrame(data=data, columns=["x1", "x2", "x3"]) # ✅ Convert X_train to tensor X_train = tf.convert_to_tensor(X_train, dtype=tf.float32) y_train = pd.DataFrame(data=[1, 0, 1], columns=["y"]) # ✅ Convert y_train to tensor y_train = tf.convert_to_tensor(y_train, dtype=tf.float32) 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, y_train, epochs=3 )
If you get the error "ValueError: Failed to convert a NumPy array to a Tensor
(Unsupported object type numpy.ndarray)", you likely passed an array of arrays
to model.fit()
.
Here is an example of how the error occurs.
import numpy as np import pandas as pd from tensorflow.keras.layers import Dense from tensorflow.keras.models import Sequential import tensorflow as tf data = [ [np.asarray([0.1]), np.asarray([0.2]), np.asarray([0.3])], [np.asarray([0.1]), np.asarray([0.2]), np.asarray([0.3])], [np.asarray([0.1]), np.asarray([0.2]), np.asarray([0.3])], ] X_train = pd.DataFrame(data=data, columns=["x1", "x2", "x3"]) y_train = pd.DataFrame(data=[1, 0, 1], columns=["y"]) print(X_train) print(X_train.dtypes) model = Sequential() model.add( Dense(1, input_dim=X_train.shape[1], activation='sigmoid') ) model.compile( optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'] ) # ⛔️ ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type numpy.ndarray). model.fit( X_train.to_numpy(), y_train, epochs=3 )
Running the code sample produces the following output.
x1 x2 x3 0 [0.1] [0.2] [0.3] 1 [0.1] [0.2] [0.3] 2 [0.1] [0.2] [0.3] x1 object x2 object x3 object dtype: object ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type numpy.ndarray).
One way to solve the error is to flatten each array in the DataFrame
.
import numpy as np import pandas as pd from tensorflow.keras.layers import Dense from tensorflow.keras.models import Sequential import tensorflow as tf data = [ [np.asarray([0.1]), np.asarray([0.2]), np.asarray([0.3])], [np.asarray([0.1]), np.asarray([0.2]), np.asarray([0.3])], [np.asarray([0.1]), np.asarray([0.2]), np.asarray([0.3])], ] X_train = pd.DataFrame(data=data, columns=["x1", "x2", "x3"]) # ✅ Flatten each array in the DataFrame X_train = X_train.apply( lambda x: pd.Series(pd.core.common.flatten(x)) ) y_train = pd.DataFrame(data=[1, 0, 1], columns=["y"]) print(X_train) print(X_train.dtypes) 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 )
You can learn more about the related topics by checking out the following tutorials: