Python – How does it actually work when calling a class like Activation (‘relu’)(X)?

How does it actually work when calling a class like Activation (‘relu’)(X)?… here is a solution to the problem.

How does it actually work when calling a class like Activation (‘relu’)(X)?

When I learn Keras, I always see syntax like Activation('relu')(X). I looked at the source code and found that Activation is a class, so for me, like Class(...) (...) Such syntax is meaninglessly valid.

This is a example and its use case: A = Add()([A1, A2])

Solution

In Keras, it’s a bit more complex than vanilla Python. Let’s break down what happens when you call Activation('relu')(X):

  1. Activation('relu') creates a new object of the class by calling the class __init__ method . This creates an object with “relu” as a parameter.
  2. All objects in Python can be called by implementing __call__, allowing you to call it like a function. Activation('relu')(X) now calls the function with X as a parameter.
  3. But wait, Activation doesn’t implement it directly, it’s actually the base class Layer.__call__ called, and it does some checks like shape matching, etc.
  4. And then Layer.__call__ actually calls self.call(X) then calls Activation.call method It applies activation to the tensor and returns the result.

Hopefully clarifying that line of code, a similar process occurs when additional layers are created and called using a functional API.

Related Problems and Solutions