Apr 17, 2025
This lecture focuses on making predictions using TensorFlow Decision Forests (TF-DF) with the Python API. It covers various methods to generate predictions and benchmarks for model inference speed.
model.predict() with pd_dataframe_to_tf_datasettfdf.keras.pd_dataframe_to_tf_dataset().pd_dataset = pd.DataFrame({
"feature_1": [1,2,3],
"feature_2": ["a", "b", "c"],
"label": [0, 1, 0],
})
tf_dataset = tfdf.keras.pd_dataframe_to_tf_dataset(pd_dataset, label="label")
model.predict() with Manual TF Datasetstf.data.Dataset.from_tensor_slices() and batch them.features, label or features, label, weights.tf_dataset = tf.data.Dataset.from_tensor_slices((
({"feature_1": [1,2,3], "feature_2": [4,5,6]}, [0,1,0])
)).batch(2)
model.predict(...) and model.predict_on_batch() on Dictionariespredict_on_batch to control batch processing.model.predict({"feature_1": np.random.rand(100), "feature_2": np.random.rand(100)}, verbose=0)[:10]
model.save("my_model")
pd_serving_dataset.to_csv("dataset.csv")
./predict --model=my_model/assets --dataset=csv:dataset.csv --output=csv:predictions.csv
pip install tensorflow_decision_forests
import tensorflow_decision_forests as tfdf
import numpy as np
import pandas as pd
import tensorflow as tf
benchmark_inference tool.!./benchmark_inference \
--model=my_model/assets \
--dataset=csv:dataset.csv \
--batch_size=100 \
--warmup_runs=10 \
--num_runs=50
This lecture provides a comprehensive guide to using TensorFlow Decision Forests for making predictions. Understanding different data input methods and prediction functions can optimize model performance and speed, especially critical for large datasets or production environments.