{"id":5517,"library":"tensorflow-decision-forests","title":"TensorFlow Decision Forests","description":"TensorFlow Decision Forests (TF-DF) is a Python library that integrates state-of-the-art decision forest algorithms (such as Random Forests and Gradient Boosted Trees) directly into TensorFlow and Keras. It enables training, serving, and interpreting these models for classification, regression, and ranking tasks. Built on the highly optimized C++ Yggdrasil Decision Forests (YDF) library, it is actively maintained with frequent releases, typically every few months.","status":"active","version":"1.12.0","language":"en","source_language":"en","source_url":"https://github.com/tensorflow/decision-forests","tags":["tensorflow","machine-learning","decision-forests","random-forest","gradient-boosted-trees","tabular-data"],"install":[{"cmd":"pip install tensorflow_decision_forests","lang":"bash","label":"Basic Installation"},{"cmd":"pip install tensorflow_decision_forests wurlitzer","lang":"bash","label":"Installation with verbose logs (e.g., Colab)"}],"dependencies":[{"reason":"TF-DF relies on TensorFlow's C++ ABI and Keras APIs; each TF-DF version is compatible with a specific TensorFlow version.","package":"tensorflow"}],"imports":[{"symbol":"tensorflow_decision_forests","correct":"import tensorflow_decision_forests as tfdf"},{"note":"All TF-DF models are Keras models and reside under `tfdf.keras`.","wrong":"tfdf.RandomForestModel","symbol":"RandomForestModel","correct":"tfdf.keras.RandomForestModel"},{"note":"Data utility functions are also part of the `tfdf.keras` submodule.","wrong":"tfdf.pd_dataframe_to_tf_dataset","symbol":"pd_dataframe_to_tf_dataset","correct":"tfdf.keras.pd_dataframe_to_tf_dataset"}],"quickstart":{"code":"import os\nimport tensorflow as tf\nimport tensorflow_decision_forests as tfdf\nimport pandas as pd\n\n# Ensure Keras 2 compatibility, often needed in TF-DF environments\nos.environ['TF_USE_LEGACY_KERAS'] = '1'\n\n# Create a dummy dataset (replace with your actual data loading)\ndata = {\n    'feature_1': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],\n    'feature_2': ['A', 'B', 'A', 'C', 'B', 'A', 'C', 'B', 'A', 'C'],\n    'label': [0, 1, 0, 1, 0, 1, 0, 1, 0, 1]\n}\ndf = pd.DataFrame(data)\n\n# Convert the Pandas DataFrame to a TensorFlow Dataset\ntrain_ds = tfdf.keras.pd_dataframe_to_tf_dataset(df, label='label')\n\n# Create and train a Random Forest model\nmodel = tfdf.keras.RandomForestModel()\nmodel.fit(train_ds)\n\n# (Optional) Evaluate the model\n# model.evaluate(train_ds) # Use a separate test_ds for proper evaluation\n\nprint(\"Model trained successfully!\")\nmodel.summary()","lang":"python","description":"This quickstart demonstrates how to train a basic RandomForestModel using TF-DF. It covers importing necessary libraries, preparing data using `pd_dataframe_to_tf_dataset`, and training the model. Ensure `pandas` is installed for this example."},"warnings":[{"fix":"Always check the official TF-DF documentation's compatibility table for the exact TensorFlow version required for your TF-DF version. For TF-DF 1.12.0, TensorFlow 2.19.0 is required.","message":"TensorFlow Version Compatibility: Each TF-DF version is strictly tied to a specific TensorFlow version due to ABI compatibility. Using incompatible versions will lead to cryptic C++ runtime errors (e.g., 'undefined symbol').","severity":"breaking","affected_versions":"All versions"},{"fix":"Update your code to use `LAMBDA_MART_NDCG`. The old name is still available for backward compatibility but is deprecated.","message":"Loss function `LAMBDA_MART_NDCG5` has been renamed to `LAMBDA_MART_NDCG`.","severity":"deprecated","affected_versions":">=1.11.0"},{"fix":"Use `tf_keras` instead of `tf.keras`, or ensure your TensorFlow version is prior to 2.16. Many examples include `os.environ['TF_USE_LEGACY_KERAS'] = '1'` to force Keras 2 compatibility.","message":"Keras 3 Incompatibility: TF-DF is not yet compatible with Keras 3.","severity":"gotcha","affected_versions":"All versions with TensorFlow >= 2.16 (where Keras 3 is default)"},{"fix":"Windows users should utilize Windows Subsystem for Linux (WSL) and follow the Linux installation instructions.","message":"Windows Support Limitations: A native Windows Pip package is not available.","severity":"gotcha","affected_versions":"All versions"},{"fix":"TF-DF is designed for CPU-based training, which is often efficient for tabular data. If GPU acceleration is critical, consider alternative libraries or migrating to YDF which may offer different integration points.","message":"No GPU/TPU Support: TF-DF models currently do not leverage GPUs or TPUs for training or inference.","severity":"gotcha","affected_versions":"All versions"},{"fix":"The TF-DF team recommends YDF for new projects as it offers more features, a simplified API, and faster training times. TF-DF models are compatible with YDF models.","message":"Recommendation to migrate to Yggdrasil Decision Forests (YDF) for new projects.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-13T00:00:00.000Z","next_check":"2026-07-12T00:00:00.000Z"}