0

I have problem in understanding this code in Python

x = layers.Flatten()(last_output)

Since Flatten is a function, how does the function get the data from last_output written outside the function call parenthesis. Don't remember seeing this kind of code in Java.

Thanks and Regards

Noufal Ibrahim
  • 71,383
  • 13
  • 135
  • 169
user121
  • 15
  • 3
  • Does this answer your question? https://stackoverflow.com/questions/44176982/how-does-the-flatten-layer-work-in-keras – fmarm May 26 '20 at 05:19

2 Answers2

1

Flatten() is the class instantiation (which is probably clear to you) and the second calls the instance with that parameter. For this to work the class must have a __call__ function defined.

Example:

class Sum:
    def __call__(self, a, b, c):
        return a + b + c

s = Sum()
print(s(3, 4, 5))
print(Sum()(3,4,5))

Also same behavior can be obtained with a function that returns another function with arguments:

def Sum2():
    def Sum3(a, b, c):
        return a + b + c
    return Sum3

s2 = Sum2()
print(s2(3, 4, 5))
print(Sum2()(3, 4, 5))
Gungnir
  • 231
  • 2
  • 5
0

Consider this

def outer():
    def print_thrice(string):
          for _ in range(3):
              print (string)
    return print_thrice

If you call outer, it will return the print_thrice function which you can then call. So you'd use it like this

x = outer()
x("hello")

Or more compactly, outer()("hello") which is what's going on here.

Noufal Ibrahim
  • 71,383
  • 13
  • 135
  • 169