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

204,811

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)

Solution 3

This should do the trick:

x_train = np.asarray(x_train).astype(np.float32)
y_train = np.asarray(y_train).astype(np.float32)

Solution 4

This is a HIGHLY misleading error, as this is basically a general error, which might have NOTHING to do with floats.

For example in my case it was caused by a string column of the pandas dataframe having some np.NaN values in it. Go figure!

Fixed it by replacing them with empty strings:

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

Or to be more specific doing this ONLY for the string (eg 'object') columns:

cols = df.select_dtypes(include=['object'])
for col in cols.columns.values:
    df[col] = df[col].fillna('')

Solution 5

I had many different inputs and target variables and didn't know which one was causing the problem.

To find out on which variable it breaks you can add a print value in the library package using the path is specified in your stack strace:

      File "C:\Users\bencu\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\framework\constant_op.py", line 96, in convert_to_eager_tensor
        return ops.EagerTensor(value, ctx.device_name, 

Adding a print statement in this part of the code allowed me to see which input was causing the problem:

constant_op.py:

  ....
      dtype = dtype.as_datatype_enum
    except AttributeError:
      dtype = dtypes.as_dtype(dtype).as_datatype_enum
  ctx.ensure_initialized()
  print(value) # <--------------------- PUT PRINT HERE
  return ops.EagerTensor(value, ctx.device_name, dtype)

After observing which value was problematic conversion from int to astype(np.float32) resolved the problem.

Share:
204,811
SuperHanz98
Author by

SuperHanz98

Software Engineer

Updated on February 19, 2022

Comments

  • SuperHanz98
    SuperHanz98 about 2 years

    Continuation from previous question: Tensorflow - TypeError: 'int' object is not iterable

    My training data is a list of lists each comprised of 1000 floats. For example, x_train[0] =

    [0.0, 0.0, 0.1, 0.25, 0.5, ...]
    

    Here is my model:

    model = Sequential()
    
    model.add(LSTM(128, activation='relu',
                   input_shape=(1000, 1), return_sequences=True))
    model.add(Dropout(0.2))
    model.add(LSTM(128, activation='relu'))
    model.add(Dropout(0.2))
    model.add(Dense(32, activation='relu'))
    model.add(Dropout(0.2))
    model.add(Dense(1, activation='sigmoid'))
    
    opt = tf.keras.optimizers.Adam(lr=1e-3, decay=1e-5)
    
    model.compile(optimizer='rmsprop',
                  loss='binary_crossentropy',
                  metrics=['accuracy'])
    
    model.fit(x_train, y_train, epochs=3, validation_data=(x_test, y_test))
    

    Here is the error I'm getting:

    Traceback (most recent call last):
          File "C:\Users\bencu\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\bencu\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\keras\engine\training.py", line 728, in fit
            use_multiprocessing=use_multiprocessing)
          File "C:\Users\bencu\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\keras\engine\training_v2.py", line 224, in fit
            distribution_strategy=strategy)
          File "C:\Users\bencu\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\keras\engine\training_v2.py", line 547, in _process_training_inputs
            use_multiprocessing=use_multiprocessing)
          File "C:\Users\bencu\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\keras\engine\training_v2.py", line 606, in _process_inputs
            use_multiprocessing=use_multiprocessing)
          File "C:\Users\bencu\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\keras\engine\data_adapter.py", line 479, in __init__
            batch_size=batch_size, shuffle=shuffle, **kwargs)
          File "C:\Users\bencu\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\keras\engine\data_adapter.py", line 321, in __init__
            dataset_ops.DatasetV2.from_tensors(inputs).repeat()
          File "C:\Users\bencu\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\data\ops\dataset_ops.py", line 414, in from_tensors
            return TensorDataset(tensors)
          File "C:\Users\bencu\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\data\ops\dataset_ops.py", line 2335, in __init__
            element = structure.normalize_element(element)
          File "C:\Users\bencu\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\data\util\structure.py", line 111, in normalize_element
            ops.convert_to_tensor(t, name="component_%d" % i))
          File "C:\Users\bencu\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\framework\ops.py", line 1184, in convert_to_tensor
            return convert_to_tensor_v2(value, dtype, preferred_dtype, name)
          File "C:\Users\bencu\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\framework\ops.py", line 1242, in convert_to_tensor_v2
            as_ref=False)
          File "C:\Users\bencu\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\framework\ops.py", line 1296, in internal_convert_to_tensor
            ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
          File "C:\Users\bencu\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\framework\tensor_conversion_registry.py", line 52, in _default_conversion_function
            return constant_op.constant(value, dtype, name=name)
          File "C:\Users\bencu\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\framework\constant_op.py", line 227, in constant
            allow_broadcast=True)
          File "C:\Users\bencu\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\framework\constant_op.py", line 235, in _constant_impl
            t = convert_to_eager_tensor(value, ctx, dtype)
          File "C:\Users\bencu\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\framework\constant_op.py", line 96, in 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).
    

    I've tried googling the error myself, I found something about using the tf.convert_to_tensor function. I tried passing my training and testing lists through this but the function won't take them.

  • Markus
    Markus about 4 years
    Yup this shorty answer was everything needed, thanks a lot! :)
  • mintra
    mintra almost 4 years
    I faced this issue when I inserted to a 3D numpy array to a pandas dataframe and then fetched numpy array from dataframe to create tensor object. To resolve this, I directly passed numpy array to create tensor object.
  • Long
    Long almost 4 years
    same here, I am trying to figure out what have been changed. Worked fine at version 1 too.
  • asmgx
    asmgx almost 4 years
    how can I fix that if my ytrain is an array of multi-label classes? (1000,20) 1000 records with 20 labels for each record?
  • Rotail
    Rotail over 3 years
    THIS DESERVE TO BE THE APPROVED ANSWER TO THIS TREAD
  • Azzurro94
    Azzurro94 over 3 years
    great answer, my data was object instead of float32
  • Kermit
    Kermit over 3 years
    np.nan is a float, so the nan is misleading you, not the error.
  • Pavel Fedotov
    Pavel Fedotov over 2 years
    Would numpy.float64 work as good in this case as well?
  • Shaina Raza
    Shaina Raza over 2 years
    it worked nicely and smooth