Skip to content Skip to sidebar Skip to footer

Failed To Convert A NumPy Array To A Tensor (Unsupported Object Type Dict)

my method i thought that the problem from it is history = model.fit_generator(train_generator, epochs=epochs, steps_per_epoch=train_steps, verbose=1, callbacks=[checkpoint], valid

Solution 1:

This error indicates some values or all values in your data does not have a valid data type to convert.


Reason:

Common reason for this error is that values of the array are not of given dtype in graph mode. It may be because some values are NaN or None or all values are in a format which is not supported to convert to a tensor, such as a python dictionary.


Solution:

This issue would be fixed by converting the data to the expected dtype, for example applying methods such as x=np.asarray(x).astype(np.float32) to the input data before feeding to the model. It also supports the NaN values issue. Note that it's better to do some preprocessing to None data values, and replace them with methods like DataFrame.fillna().

But in scenarios where your data type is something unsupported (like python dictionary) you can not solve the problem with the above approach. You should change the data structure then feed it to the model. Note that even np.asarray can not change the structure. You may get the type of data numpy array, but the structure remains the same and can not be handled by network. So don't get the type as an evidence for not being dict. Here is some example:

x = {1:1,2:2,3:3,4:4,5:5}
print(type(x))      #<class 'dict'>
x = np.asarray(x)
print(type(x))      #<class 'numpy.ndarray'> #the type is changed
print(x)            #{1: 1, 2: 2, 3: 3, 4: 4, 5: 5} #the structure has not been changed
    

Your Scenario:

As your code reflects, you converted the data to the numpy array and float. So, even you have None variables, you won't get error. So, as the error reflects (Unsupported object type dict), one of your input variables ([in_img, in_seq], out_word) is a dictionary. Based on your code, in_seq and out_seq are lists. So, it should be in_img which initiate from photo variable. So, check this variable data. It is likely holds a dictionary like data. Don't pay attention to the type (print(type(photo))), because as my above code, it could be a numpy.ndarray, but hold a dictionary data.


Post a Comment for "Failed To Convert A NumPy Array To A Tensor (Unsupported Object Type Dict)"