Why in Keras subclassing API, the call method is never called and as an alternative the input is passed by calling the object of this class?

Actually __call__ method is implemented in the Layer class, which is inherited by Network class, which is inherited by Model class:

class Layer(module.Module):
    def __call__(self, inputs, *args, **kwargs):

class Network(base_layer.Layer):

class Model(network.Network):

So MyClass will inherit this __call__ method.

Additional info:

So actually what we do is overriding the inherited call method, which new call method will be then called from the inherited __call__ method. That is why we don't need to do a model.call(). So when we call our model instance, it's inherited __call__ method will be executed automatically, which calls our own call method.


Occam's razor says that the __call__ method is implemented in the Model class, so your subclass will inherit this method, which is why the call works. The __call__ in the Model class just forwards parameters to your call method and does some bookkeeping.