close

[Solved] Tensorflow – ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type float)

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error Tensorflow – ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type float) in python. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How Tensorflow – ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type float) Error Occurs?

Today I get the following error Tensorflow – ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type float) in python.

How To Solve Tensorflow – ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type float) Error ?

  1. How To Solve Tensorflow – ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type float) Error ?

    To Solve Tensorflow – ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type float) Error After trying everything above with no success, I found that my problem was that one of the columns from my data had boolean values. Converting everything into np.float32 solved the issue!

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

    To Solve Tensorflow – ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type float) Error After trying everything above with no success, I found that my problem was that one of the columns from my data had boolean values. Converting everything into np.float32 solved the issue!

Solution 1

TL;DR Several possible errors, most fixed with x = np.asarray(x).astype('float32').

Others may be faulty data preprocessing; ensure everything is properly formatted (categoricals, nans, strings, etc). Below shows what the model expects:

[print(i.shape, i.dtype) for i in model.inputs]
[print(o.shape, o.dtype) for o in model.outputs]
[print(l.name, l.input_shape, l.dtype) for l in model.layers]

The problem’s rooted in using lists as inputs, as opposed to Numpy arrays; Keras/TF doesn’t support former. A simple conversion is: x_array = np.asarray(x_list).

The next step’s to ensure data is fed in expected format; for LSTM, that’d be a 3D tensor with dimensions (batch_size, timesteps, features) – or equivalently, (num_samples, timesteps, channels). Lastly, as a debug pro-tip, print ALL the shapes for your data. Code accomplishing all of the above, below:

Sequences = np.asarray(Sequences)
Targets   = np.asarray(Targets)
show_shapes()

Sequences = np.expand_dims(Sequences, -1)
Targets   = np.expand_dims(Targets, -1)
show_shapes()
# OUTPUTS
Expected: (num_samples, timesteps, channels)
Sequences: (200, 1000)
Targets:   (200,)

Expected: (num_samples, timesteps, channels)
Sequences: (200, 1000, 1)
Targets:   (200, 1)

As a bonus tip, I notice you’re running via main(), so your IDE probably lacks a Jupyter-like cell-based execution; I strongly recommend the Spyder IDE. It’s as simple as adding # In[], and pressing Ctrl + Enter below:


Function used:

def show_shapes(): # can make yours to take inputs; this'll use local variable values
    print("Expected: (num_samples, timesteps, channels)")
    print("Sequences: {}".format(Sequences.shape))
    print("Targets:   {}".format(Targets.shape))   

Solution 2

After trying everything above with no success, I found that my problem was that one of the columns from my data had boolean values. Converting everything into np.float32 solved the issue!

import numpy as np

X = np.asarray(X).astype(np.float32)

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read