tensorflow/decision-forests

Unable to save GradientBoostedTrees model

paolocambria opened this issue · 1 comments

Hello, I am trying to train a basic GBTree Model as follows:

model = tfdf.keras.GradientBoostedTreesModel(task=tfdf.keras.Task.REGRESSION)
model.compile(
                    metrics=[
                        tf.keras.metrics.MeanAbsoluteError(), 
                        tf.keras.metrics.MeanAbsolutePercentageError()
                    ])

model.fit(..)
model.save(FILEPATH)

But I'm not able to correctly save it.
I managed to save the weights correctly with
model.save_weights(FILEPATH)
I've seen a lot of examples of GBTrees model saved and I think I shouldn't get the error below:

Skipping registering GPU devices...
Use /tmp/tmp3_rsnkvv as temporary training directory
2022-12-23 12:45:40,612 [INFO] [MainThread]: Use /tmp/tmp3_rsnkvv as temporary training directory
Reading training dataset...
2022-12-23 12:45:40,695 [INFO] [MainThread]: Reading training dataset...
Training dataset read in 0:00:03.763728. Found 1795 examples.
2022-12-23 12:45:44,459 [INFO] [MainThread]: Training dataset read in 0:00:03.763728. Found 1795 examples.
Reading validation dataset...
2022-12-23 12:45:44,459 [INFO] [MainThread]: Reading validation dataset...
Num validation examples: tf.Tensor(317, shape=(), dtype=int32)
2022-12-23 12:45:44,833 [INFO] [MainThread]: Num validation examples: tf.Tensor(317, shape=(), dtype=int32)
Validation dataset read in 0:00:00.374263. Found 317 examples.
2022-12-23 12:45:44,834 [INFO] [MainThread]: Validation dataset read in 0:00:00.374263. Found 317 examples.
Training model...
2022-12-23 12:45:44,834 [INFO] [MainThread]: Training model...
2022-12-23 12:45:44.838135: W external/ydf/yggdrasil_decision_forests/learner/gradient_boosted_trees/gradient_boosted_trees.cc:1765] Subsample hyperparameter given but sampling method does not match.
2022-12-23 12:45:44.838219: W external/ydf/yggdrasil_decision_forests/learner/gradient_boosted_trees/gradient_boosted_trees.cc:1778] GOSS alpha hyperparameter given but GOSS is disabled.
2022-12-23 12:45:44.838266: W external/ydf/yggdrasil_decision_forests/learner/gradient_boosted_trees/gradient_boosted_trees.cc:1787] GOSS beta hyperparameter given but GOSS is disabled.
2022-12-23 12:45:44.838306: W external/ydf/yggdrasil_decision_forests/learner/gradient_boosted_trees/gradient_boosted_trees.cc:1799] SelGB ratio hyperparameter given but SelGB is disabled.
[INFO 2022-12-23T12:45:47.869198389+01:00 kernel.cc:1175] Loading model from path /tmp/tmp3_rsnkvv/model/ with prefix da4f25d5a8604b93
[INFO 2022-12-23T12:45:47.883184066+01:00 abstract_model.cc:1306] Engine "GradientBoostedTreesQuickScorerExtended" built
[INFO 2022-12-23T12:45:47.883270999+01:00 kernel.cc:1021] Use fast generic engine
Model trained in 0:00:03.056428
2022-12-23 12:45:47,890 [INFO] [MainThread]: Model trained in 0:00:03.056428
Compiling model...
2022-12-23 12:45:47,892 [INFO] [MainThread]: Compiling model...
Model compiled.
2022-12-23 12:45:49,280 [INFO] [MainThread]: Model compiled.
Traceback (most recent call last):
  File "/usr/lib/python3.10/runpy.py", line 196, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/usr/lib/python3.10/runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "/mnt/c/Users/Documents/dev/src/utils/train.py", line 263, in <module>
    train_model(p)
  File "/mnt/c/Users/Documents/dev/src/utils/train.py", line 154, in train_model
    model.save(SAVEPATH)
  File "/usr/local/lib/python3.10/dist-packages/tensorflow_decision_forests/keras/core.py", line 1678, in save
    super(CoreModel, self).save(
  File "/usr/local/lib/python3.10/dist-packages/keras/utils/traceback_utils.py", line 70, in error_handler
    raise e.with_traceback(filtered_tb) from None
  File "/usr/local/lib/python3.10/dist-packages/keras/saving/legacy/save.py", line 154, in save_model
    raise NotImplementedError(
NotImplementedError: Saving the model to HDF5 format requires the model to be a Functional model or a Sequential model. It does not work for subclassed models, because such models are defined via the body of a Python method, which isn't safely serializable. Consider saving to the Tensorflow SavedModel format (by setting save_format="tf") or using `save_weights`.
rstz commented

I cannot reproduce this on a fresh instance, but going from the error message, it sounds like you're attempting to save a model in the older Keras H5 format. This is not supported by TF-DF. You can force saving in the SavedModel format by specifying save_format, e.g. model.save('/tmp/my_model', save_format='tf') AFAICT, you also need to make sure the model path does not end in .h5 for this to work.

I'll close this for now, feel free to reopen with a colab repro if you need additional assistance.