lvfmc85
lvfmc85

Reputation: 9

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

I'm looking for a solution to my issue, and I tried several suggestions with no success. I hope someone can help me with this.

I used Autokeras to train and save a model. I used the following code to save the best model.

try:
    model.save("model_autokeras", save_format="tf")
except Exception:
    model.save("model_autokeras.h5")

It was saved as tf (a folder).

I loaded the model and tried to predict the target using a new CSV file.

I imported the CSV with pandas (read_csv), the file hasn't the target attribute (the one I one to predict), and loaded the model with this code:

loaded_model = load_model("model_autokeras", custom_objects=ak.CUSTOM_OBJECTS)

I tried to use the model to predict the target with this code:

predicted = loaded_model.predict(df_from_csv_file)

And I got this error:

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

The df_from_csv_file dtypes are: "int64, int64, int64, int64, int64, object, object"

Transforming the objects to strings didn't solve the problem. Transform int64 to int32, float32, or float64 also didn't work. I also tried to use a numpy arrays with no success.

Any suggestion on how to make this work?

Thank you so much!

Upvotes: 0

Views: 134

Answers (1)

Azhar Khan
Azhar Khan

Reputation: 4098

The last 2 object columns can be the issue. Convert them to numeric type.

For me, the root cause of the problem was that some of columns in input dataset were not numeric (they were string).

Converting the respective column(s) or the entire dataframe to numeric type using any of the following solution solved the issue:

df = df.apply(pd.to_numeric) 

df["my_col"] = df[["my_col"]].apply(pd.to_numeric)

df = df.to_numpy().astype(np.float32)

df = df.to_numpy().astype("float")

Also make sure there are no NaN, na or null values in column.

The issue can also be with the shape of the input or shape of data elements in the input. Make sure the shapes are consistent.

Upvotes: 0

Related Questions