tensorflow/decision-forests

TF-DF Compatibility with Keras 3?

Opened this issue · 6 comments

According to the known issues, "TF-DF is not compatible with Keras 3 at this time." Are efforts underway to update TF-DF to be compatible with Keras 3?

rstz commented

Yes, we are planning to make TF-DF compatible with Keras 3, but there is no timeline yet, since it might be a non-trivial change.

Ive been trying and failing to tune hyperameters for tfdf.CartModels in line with the google developer course practice https://developers.google.com/machine-learning/decision-forests/practice

but i get a FatalTypeError after the keras_tuner builds the hypermodel as its not a keras.model.Model instance.

My guess is that now keras_tuner uses keras 3 (Release v1.4.6), it doesnt recognise these as keras modes?

Heres a notebook replicating the issue
https://colab.research.google.com/drive/183uPNtxGXn7TI8c9CDbrJ3sD0KWr-3ms#scrollTo=uO8SKW48MVPS

rstz commented

Hi, looks like we have to update this class - thank you for notifying us. Very likely, you only need to add

import os
# Keep using Keras 2
os.environ['TF_USE_LEGACY_KERAS'] = '1'

at the top of the colab (note that I cannot access yours, since it's not shared with the world).

Thank you for the quick reply, , ive updated access to the notebook, with that potential fix unfortunately not working https://colab.research.google.com/drive/183uPNtxGXn7TI8c9CDbrJ3sD0KWr-3ms?usp=sharing

Fixed with versioning keras_tuner AND setting legacy env variable. Il share this feedback this the course.

Im not really familiar with keras, but maybe the backward compatibility introduced here doesn't get the right backend? im not quite sure where to open a bug report, is a keras_tuner or tfdf issue?
keras_tuner 1.4.5 -> 1.4.6 [https://github.com/keras-team/keras-tuner/compare/v1.4.5...v1.4.6]

import os; os.environ['TF_USE_LEGACY_KERAS'] = '1' # Keep using Keras 2
!pip install keras_tuner==1.4.5 # use legacy keras_tuner

Sorry to hear about your issue. Let me explain the situation and the available solutions.

I hope this helps.

Keras in TF

Keras 3 is a rewrite from scratch of Keras 2. The previous version of Keras, previously called keras in PyPI, was renamed to tf_keras. The pip package keras now points to Keras 3. TensorFlow includes a copy of Keras i.e. tf.keras. Up to TensorFlow 2.15, tf.keras was an alias to Keras 2. Starting with TensorFlow 2.16, tf.keras is an alias to Keras 3.

There are two ways to force TensorFlow to use Keras 2:

  • Use TensorFlow 2.15.
  • With TensorFlow >=2.16, define the global variable TF_USE_LEGACY_KERAS=1 before any import.
  • Call the tf_keras PyPI package directly.

TF-DF with Keras

TensorFlow Decision Forests is, at the moment, only compatible with Keras 2 i.e. TF-DF is not compatible with Keras 3.

  • Versions of TF-DF <= 1.8 use the version of Keras provided by TensorFlow. You have to use the TF_USE_LEGACY_KERAS trick if using TF>=2.16.

  • Version of TF-DF >=1.9 uses tf_keras directly and works out of the box for TensorFlow >=2.16.

YDF

While we make TF-DF compatible with Keras 3, you can use the YDF library.
YDF is Google's new decision forest library. TF-DF and YDF use the same implementations but YDF has more features.
Importantly, YDF is compatible with Keras 2, Keras 3, and soon JAX.
Here is an example: https://ydf.readthedocs.io/en/latest/tutorial/compose_with_tf

The TF-DF and YDF APIs relatively similar. A few functions have changed names. You can learn about those difference here: https://ydf.readthedocs.io/en/latest/tutorial/migrating_to_ydf/

Tuner

YDF contains a specialized tuner which will be faster than Keras Tuner when using decision forests. See: https://ydf.readthedocs.io/en/latest/tutorial/tuning/

Class

The decision forest practice class is currently being updated to use YDF.