为什么 keras 使用“call”而不是 __call__?

and*_*ndy 5 keras tensorflow

我喜欢(https://www.tensorflow.org/tutorials/eager/custom_layers)中的以下代码

class MyDenseLayer(tf.keras.layers.Layer):
  def __init__(self, num_outputs):
    super(MyDenseLayer, self).__init__()
    self.num_outputs = num_outputs

  def build(self, input_shape):
    self.kernel = self.add_variable("kernel",
                                shape=[int(input_shape[-1]),
                                       self.num_outputs])

  def call(self, input):
    return tf.matmul(input, self.kernel)
Run Code Online (Sandbox Code Playgroud)

最后两行是调用方法,它不像通常的python类方法 调用有两个下划线。它们之间有什么区别吗?

小智 8

以下答案基于https://tf.wiki/zh/basic/models.html

基本上在Python中,当您从调用类的实例ClassA使用ClassA(),它等同于ClassA.__call__()。所以在这种情况下使用__call__()而不是使用似乎是合理的call(),对吧?

但是,我们使用的原因call()是,在tf.keras调用模型或层时,它有自己的内部操作,这些操作对于保持其内部结构至关重要。因此,它公开了一种call()用于客户重载的方法。__call()__调用call()以及一些内部操作,因此当我们重新加载call()继承自tf.keras.Modelor 时tf.keras.Layer,我们可以在保留tf.keras内部结构的同时调用我们的自定义代码。

例如,根据我的经验,如果您的输入是一个 numpy 数组而不是张量,那么如果您在其中编写客户代码,call()则不需要手动转换它,但是如果您覆盖__call__(),这将是一个问题,因为某些内部操作不是叫。