tensorflow / decision-forests

A collection of state-of-the-art algorithms for the training, serving and interpretation of Decision Forest models in Keras.
Apache License 2.0
660 stars 110 forks source link

TF-DF Compatibility with Keras 3? #210

Open rlcauvin opened 9 months ago

rlcauvin commented 9 months ago

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 9 months ago

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.

woodwj commented 8 months ago

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 8 months ago

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).

woodwj commented 8 months ago

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

woodwj commented 8 months ago

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

achoum commented 7 months ago

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:

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.

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.