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
663 stars 110 forks source link

How to use keras_tuner with TFDF getting TypeError: Exception encountered when calling layer "gradient_boosted_trees_model_29" #104

Closed IMvision12 closed 2 years ago

IMvision12 commented 2 years ago
hp = keras_tuner.HyperParameters()

num_trees = hp.Int("num_tress", min_value=100, max_value=1000, step=20)
max_depth = hp.Int("max_depth", min_value=2, max_value=20, step=2)
model = tfdf.keras.GradientBoostedTreesModel(task=tfdf.keras.Task.REGRESSION,
                                                    num_trees = num_trees,
                                                    max_depth=max_depth,
                                                    verbose=1)
model.compile(metrics=["mse"])

tuner = keras_tuner.RandomSearch(
    hypermodel=model,
    objective="mse",
    max_trials=3,
    executions_per_trial=2,
    overwrite=True,
    directory="my_dir",
    project_name="helloworld",
)

Getting error :

Warning:  The model was called directly (i.e. using `model(data)` instead of using `model.predict(data)`) before being trained. The model will only return zeros until trained. The output shape might change after training <keras_tuner.engine.hyperparameters.HyperParameters object at 0x7fbf20e8d750>
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
/tmp/ipykernel_38/242452102.py in <module>
     18     overwrite=True,
     19     directory="my_dir",
---> 20     project_name="helloworld",
     21 )

/opt/conda/lib/python3.7/site-packages/keras_tuner/tuners/randomsearch.py in __init__(self, hypermodel, objective, max_trials, seed, hyperparameters, tune_new_entries, allow_new_entries, **kwargs)
    135             allow_new_entries=allow_new_entries,
    136         )
--> 137         super(RandomSearch, self).__init__(oracle, hypermodel, **kwargs)

/opt/conda/lib/python3.7/site-packages/keras_tuner/engine/tuner.py in __init__(self, oracle, hypermodel, max_model_size, optimizer, loss, metrics, distribution_strategy, directory, project_name, logger, tuner_id, overwrite, executions_per_trial)
    115             project_name=project_name,
    116             logger=logger,
--> 117             overwrite=overwrite,
    118         )
    119 

/opt/conda/lib/python3.7/site-packages/keras_tuner/engine/base_tuner.py in __init__(self, oracle, hypermodel, directory, project_name, logger, overwrite)
    101         self._display = tuner_utils.Display(oracle=self.oracle)
    102 
--> 103         self._populate_initial_space()
    104 
    105         if not overwrite and tf.io.gfile.exists(self._get_tuner_fname()):

/opt/conda/lib/python3.7/site-packages/keras_tuner/engine/base_tuner.py in _populate_initial_space(self)
    130 
    131         while True:
--> 132             self.hypermodel.build(hp)
    133 
    134             # Update the recored scopes.

/opt/conda/lib/python3.7/site-packages/keras/utils/traceback_utils.py in error_handler(*args, **kwargs)
     65     except Exception as e:  # pylint: disable=broad-except
     66       filtered_tb = _process_traceback_frames(e.__traceback__)
---> 67       raise e.with_traceback(filtered_tb) from None
     68     finally:
     69       del filtered_tb

/opt/conda/lib/python3.7/site-packages/tensorflow/python/framework/func_graph.py in autograph_handler(*args, **kwargs)
   1145           except Exception as e:  # pylint:disable=broad-except
   1146             if hasattr(e, "ag_error_metadata"):
-> 1147               raise e.ag_error_metadata.to_exception(e)
   1148             else:
   1149               raise

TypeError: Exception encountered when calling layer "gradient_boosted_trees_model_29" (type GradientBoostedTreesModel).

in user code:

    File "/opt/conda/lib/python3.7/site-packages/tensorflow_decision_forests/keras/core.py", line 789, in call  *
        return tf.zeros([_batch_size(inputs), 1])
    File "/opt/conda/lib/python3.7/site-packages/tensorflow_decision_forests/keras/core.py", line 1910, in _batch_size  *
        return tf.shape(inputs)[0]

    TypeError: Failed to convert elements of <keras_tuner.engine.hyperparameters.HyperParameters object at 0x7fbf20e8d750> to Tensor. Consider casting elements to a supported type. See https://www.tensorflow.org/api_docs/python/tf/dtypes for supported TF dtypes.

Call arguments received:
  • inputs=<keras_tuner.engine.hyperparameters.HyperParameters object at 0x7fbf20e8d750>
  • training=False