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

Solving Tensorflow ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type float) is a common general python error.

In this post we will try to solve your error and see why it occurs in the first place, we will present multiple solutions so you can find the one which suits your particular case.

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

This is one of the most confusing and hard to solve errors we have dealt with, luckily there are a lot of possible fixes which might work for you.

First, let us checkout the message of the error.

                                                                       #
Traceback (most recent call last):
File "C:\Users\ash\Desktop\ProjectFiles\Code\Program.py", line 88, in FitModel
model.fit(x_train, y_train, epochs=3, validation_data=(x_test, y_test))
File "C:\Users\ash\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\keras\engine\test.py", line 328, in fit
use_multiprocessing=use_multiprocessing)
File "C:\Users\ash\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\keras\engine\test2.py", line 324, in fit
convert_to_eager_tensor
return ops.EagerTensor(value, ctx.device_name, dtype)
ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type float).
                                                                       #

The error should look more or less like the message we see above.

Bellow, I will present multiple solutions some have worked for me and others have worked for other developers.

Solution 1 : Convert everything into np.float32

Sometimes, one of the sections of your data has Boolean values which will without a doubt cause the error.

First, you should import numpy as usual like in the example bellow.

                                                                       #
import numpy as np
                                                                       #

And then, Convert all the data into np.float32, just like in the example bellow.

                                                                       #
atlas = np.asarray(atlas).astype(np.float32)
                                                                       #

The solution above should work, but if it does not, please make sure to try the solution bellow.

Solution 2 : edit the values of the pandas dataframe

Sometimes, there is a problem with the pandas dataframe that usually has np.NaN values inside.

You can get rid of the values by replaceing them by nothing aka an empty space.

                                                                       #
df.fillna(value='', inplace=True)
                                                                       #

This error could be confusing at first. But once you understand why it is happening, it is easy to solve by only using a more recent method that works and achieves the same function.

Summing-up

This is the end of this article, I hope we helped you solve the error or at least show you why it happens and guided you in the right direction.

If you want to help, you can donate to our Kofi account using the red button at the top of this page. Keep learning, keep coding and cheers. If you want to learn more about Python, please check out the Python Documentation : https://docs.python.org/3/