Zhi Lu Zhi Lu - 1 year ago 72
Python Question

Meaning of function calling in Python?

I saw a Python function calling way in Keras' tutorial like this:

from keras.layers import Input, Dense
from keras.models import Model

# this returns a tensor
inputs = Input(shape=(784,))

# a layer instance is callable on a tensor, and returns a tensor
x = Dense(64, activation='relu')(inputs)

However, I have no idea what is the meaning of the function calling form:

x = Dense(64, activation='relu')(inputs)

Why is there a "(inputs)" outside the bracket of the function "Dense"'s parameters list?

Answer Source

Because Dense(...) returns a callable (basically, a function), so it can be called in turn. Here's a simple example:

def make_adder(a):
    def the_adder(b):
        return a + b
    return the_adder

add_three = make_adder(3)
# => 8

# => 8

Here, make_adder(3) returns the function that is defined as

def the_adder(b)
    return 3 + b

then calling that function with argument 5 returns 8. If you skip the step of assigning the return value of make_adder(3) to a separate variable, you get the form that you were asking about: make_adder(3)(5) is the same thing as Dense(64, activation='relu')(inputs) from your question.

EDIT: Technically, Dense is not classified as a function in Python, but as a class; Dense(...) is thus an invocation of a constructor. The class in question defines the __call__ method, which makes objects of this class "callable". Both functions and callable objects can be called by invoking them with an argument list, and the difference between the two does not impact the explanation at all. However, here's an example of a simple callable, that more closely parallels Dense:

class Adder:
    def __init__(self, a):
        self.a = a
    def __call__(self, b):
        return self.a + b

adder_of_three = Adder(3)
# => 8

# => 8
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download