How objects are called in Keras

less than 1 minute read

This post elucidates how layers (for instance class MyLayer(Layer)) are called in Keras when we perform operations such as x_output = MyLayer( .... , ...)(x_input). In this example, the MyLayer class instance is called directly with input x_input. This action calls the def __call__() function, found in the parent of MyLayer which is class Layer(). The def __call__() function in the parent class, in-turn calls the def call() in MyLayer class which defines the logic of MyLayer layer.

I have given a simplified example below describing this operation flow.

class Layer():
    def __init__(self,x):
        print('Inside Layer -> __init__ :',x)

    def __call__(self, y):
        print('Inside Layer -> __call__ :',y)

    def call(self,z):
        print('Inside Layer -> call :',z)

class MyLayer(Layer):
    def __init__(self,k):
        print('Inside MyLayer -> __init__ :',k)
        super(MyLayer, self).__init__(k)

    def call(self, m):
        print('Inside MyLayer -> call :',m)    

if __name__ == '__main__':
    obj = MyLayer(3)

Upon running the above code, the output is as follows:

Inside MyLayer -> __init__ : 3
Inside Layer -> __init__ : 3
Inside Layer -> __call__ : 4
Inside MyLayer -> call : 4

Leave a comment