Search code examples
pythontensorflowkerastensorflow2.0python-polars

TensorFlow model can't predict on polars dataframe


I trained a TensorFlow model for text classification, but I can't use a Polars DataFrame to make my predictions on it. However, I can use a Pandas DataFrame.

import pandas as pd
import polars as pl
import joblib
from tensorflow.keras.models import load_model

loaded_model =load_model('model.keras')
load_Le = joblib.load('label_encoder.joblib')

If i do:

text = "some example text"
df = pd.DataFrame({"Coment":[text]})
preddict = loaded_model.predict(df["Coment"])

I have no problems, but if I do:

text = "some example text"
df = pl.DataFrame({"Coment":[text]})
preddict = loaded_model.predict(df["Coment"])

I get TypeError: cannot convert the argument type_value: String to a TensorFlow Dtype.

Any advice?

Some extra info:

Before saving my model, I added this so I can predict on any text (Works fine on pandas)

inputs = keras.Input(shape=(1,), dtype="string")
processed_inputs = text_vectorization(inputs)
outputs = model(processed_inputs)
inference_model = keras.Model(inputs, outputs)
inference_model.save('model.keras')

Solution

  • As of late 2023, there were no plans to make Polars dataframes compatible with TensorFlow. In the meantime you can convert polar frames to pandas with the to_pandas() member and pass those to the models.