1

I have code as below.

x = inputs
if conv_first:
    x = conv(x)
    if batch_normalization:
        x = BatchNormalization()(x)
    if activation is not None:
        x = Activation(activation)(x)

Here, I dont understand how x = BatchNormalization()(x) works (just like the same, x = Activation(activation)(x) too). If it was BatchNormalization(x), it would have been easy.

Anyone can explain in a concise way what it is and how it works?

Thank you very much in advance.

junmouse
  • 155
  • 8
  • The term is parentheses. Please see https://stackoverflow.com/questions/6476825/what-do-double-parentheses-mean-in-a-function-call-e-g-funcstuffstuff – aris Mar 14 '19 at 01:59

2 Answers2

4

Both seem to be classes that implement __call__(). Then BatchNormalization() creates an instance and (x) calls .__call__(x) on the instance.

Klaus D.
  • 11,472
  • 3
  • 30
  • 43
3

Not sure that is the case, but the syntax is possible if the first called object returns another function.

Consider this code:

def f(arg):
    print(arg)

def g():
    return f

x = "hi"

g()(x)  # equivalent to f(x), since f is what g returns

Notice thay g() returns the function f without actually executing it, which is why there's no parentheses on g's return statement.

jfaccioni
  • 4,793
  • 1
  • 5
  • 21