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):
Activation('relu') creates
a new object of the class by calling the class__init__
method . This creates an object with “relu” as a parameter.- 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 withX
as a parameter. - But wait,
Activation
doesn’t implement it directly, it’s actually the base classLayer.__call__
called, and it does some checks like shape matching, etc. - And then
Layer.__call__
actually callsself.call(X)
then callsActivation.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.