python中的不同变量赋值

在查看Python中的一些深度学习代码时,我遇到了一些下面的代码行。我不确定它会做什么。直到现在我都没有在python中看到过这样的任务。有人可以帮我理解吗?

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?

评论
  • rqui
    rqui 回复

    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

  • Kennedy
    Kennedy 回复

    这与

    gap2d = GlobalAveragePooling2D()
    top_model = gap2d(x)
    

    (尽管没有额外的变量)。

    它的作用取决于您使用的框架。