Search code examples
pythontensorflowkerasdeep-learning

In tensorflow Model class , how the call method works?


in this code i understand , init is used for defining the layers and inside the call i can see the how the feedforward works .

  1. i didnt understand how this call method working under the hood
  2. is this equal to dunder call method ?
  3. is this a normal method like all ,should we need to call like --> instance.call() ?
class WideAndDeepModel(tf.keras.Model):
    def __init__(self, units=30, activation="relu", **kwargs):
        super().__init__(**kwargs)  # needed to support naming the model
        self.norm_layer_wide = tf.keras.layers.Normalization()
        self.norm_layer_deep = tf.keras.layers.Normalization()
        self.hidden1 = tf.keras.layers.Dense(units, activation=activation)
        self.hidden2 = tf.keras.layers.Dense(units, activation=activation)
        self.main_output = tf.keras.layers.Dense(1)
        self.aux_output = tf.keras.layers.Dense(1)

    def call(self, inputs):
        input_wide, input_deep = inputs
        norm_wide = self.norm_layer_wide(input_wide)
        norm_deep = self.norm_layer_deep(input_deep)
        hidden1 = self.hidden1(norm_deep)
        hidden2 = self.hidden2(hidden1)
        concat = tf.keras.layers.concatenate([norm_wide, hidden2])
        output = self.main_output(concat)
        aux_output = self.aux_output(hidden2)
        return output, aux_output

model = WideAndDeepModel(30, activation="relu", name="my_cool_model")

Please any one clarify or provide any sufficient source for understanding it


Solution

  • Please take a look at below link: https://www.tensorflow.org/api_docs/python/tf/keras/layers/Layer#call

    The initial states of model sets by init and other specifications of layers could be set by call() method which could be used in instances and subclasses.

    In init you specify your layers and their propereties like the activation function of layer and number of nodes in each layer (unit argument). Then in call() method you specify the flow of layer that means output of which layer should be input of which one. As you see in below lines of your code:

    hidden1 = self.hidden1(norm_deep)
    hidden2 = self.hidden2(hidden1)
    concat = tf.keras.layers.concatenate([norm_wide, hidden2])
    

    you also specify the output in call(). Input defines the shape of your input layer. for example, if you are working with images as input and you have 20 images of 30x30 pixels in RGB (3 channels), the shape of your input data is (20,30,30,3) and your input layer tensor could be defined like below:

    inputs = tf.keras.Input(shape=(20,30,30,3))
    

    of course the above is not your input shape, as you have in call() method: input_wide, input_deep = inputs