Get leafs values in class inherits from tensorflow.keras.Model

Hi All,

I would like to be able to get the leaves values when training ensemble that uses tfdf model as a layer, in custom class I created.

Calling tfdf(inputs) works just fine, but when trying to use tfdf.call_get_leaves(inputs) I got a tensor with shape None, None that I can’t use.

I reproduces the example here:

my_ensemble

Will appreciate your help, thank you!

Hi @ahad ,

Here are some suggestions you can follow to resolve the issue,

  1. First, ensure you’re using the correct method to get leaf values. The method call_get_leaves is not a standard TFDF method. Instead, you can use get_leaves:
leaf_values = tfdf_model.get_leaves(inputs)
  1. If you’re still getting a tensor with shape (None, None), it might be due to the dynamic nature of the input. Try to use a concrete input shape:
leaf_values = tfdf_model.get_leaves(tf.keras.Input(shape=(your_input_shape,)))
  1. If you’re using a custom layer, make sure you’re properly implementing the call method:
class CustomTFDFLayer(tf.keras.layers.Layer):
    def __init__(self, tfdf_model):
        super().__init__()
        self.tfdf_model = tfdf_model

    def call(self, inputs):
        predictions = self.tfdf_model(inputs)
        leaf_values = self.tfdf_model.get_leaves(inputs)
        return predictions, leaf_values

Let us know if the problem still persists,

Thank You .