4

How objects are called in Keras

 3 years ago
source link: https://adaickalavan.github.io/tensorflow/how-objects-are-called-in-keras/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

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)
        self.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)
    obj(4)

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

</section


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK