1

While having a look at some deep-learning code in Python, I came across some below lines of code. I am not sure what it does. I have never saw such assignments in python untill now. Can someone help me understand it?

top_model = GlobalAveragePooling2D()(x)

What does the above line of code do? Why the 2 () concatenated? It is very similar to object casting in Java. What are such assignments called in Python?

1
  • This is not specifically a special assignment. It is just a "special" expression used in assignment. Using () is to call stuff. This means that GlobalAveragePooling2D() in itself returns something that is callable, which gets called with the x argument Commented May 17, 2020 at 14:16

2 Answers 2

2

It's simply the same thing as

gap2d = GlobalAveragePooling2D()
top_model = gap2d(x)

(though without the extra variable).

What it does depends on the framework you're using.

Sign up to request clarification or add additional context in comments.

Comments

2

GlobalAveragePooling2D is a class.

GlobalAveragePooling2D() creates an instance of that class.

This class happens to be callable (i.e. has a __call__ method defined). So it can behave like a function.

GlobalAveragePooling2D()(x) calls this newly created object with x as an argument.

The result of this call is then assigned (just a normal assignment) to top_model

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.