0

I meet some Keras functions that look like the following : Function(1*List of parameters separated by commas)(2*A parameter). But I don't understand what does Function do on the second list of parameters. I have never met this kind of functions in Python. The usual type of functions' prototypes I see are of the following : Function(List of parameters)

Example of these functions met in Keras :

x = Dense(128, activation='relu')(x) x = Dropout(0.35)(x) out = Dense(num_classes, activation='softmax')(x)

In this case, it looks like that Function takes into account what was done precedently on x before applying on it new changes.

  1. Is this kind of function writing backed by any Python manual ?
  2. Is it a new type of function writing in Python ?
  3. How does it work ?
YellowishLight
  • 238
  • 3
  • 21

1 Answers1

3

It's just a function that returns a function which you immediately call. You can do the same:

def add(x):
    def add_x(y):
        return x + y
    return add_x

This function can now be called like this:

>>> add(4)(7)
11

This works because

Functions are first-class objects. A “def” statement executed inside a function definition defines a local function that can be returned or passed around. Free variables used in the nested function can access the local variables of the function containing the def. [1]

See also: Naming and Binding.

L3viathan
  • 26,748
  • 2
  • 58
  • 81