Tensorflow build() 如何从 tf.keras.layers.Layer 工作 [英] How does Tensorflow build() work from tf.keras.layers.Layer

查看:46
本文介绍了Tensorflow build() 如何从 tf.keras.layers.Layer 工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有人知道 build() 函数是如何在 tf.keras.layers.Layer 类中工作的.根据文档:

I was wondering if anyone knew how the build() function works from the tf.keras.layers.Layer class under the hood. According to the documentation:

build 当你知道输入张量的形状并且可以做剩下的初始化

所以在我看来,这个类的行为与此类似:

so to me it seems like the class is behaving similar to this:

class MyDenseLayer:
  def __init__(self, num_outputs):
    self.num_outputs = num_outputs

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

  def __call__(self, input):
    self.build(input.shape) ## build is called here when input shape is known
    return tf.matmul(input, self.kernel)

我无法想象 build() 会永远被调用 __call__,但它是唯一传入输入的地方.有谁知道具体是怎么调用的这在幕后有效吗?

I can't imagine build() would be called for ever __call__, but it is the only place where the input is passed in. Does anyone know how exactly this works under the hood?

推荐答案

Layer.build() 方法通常用于实例化层的权重.请参阅源代码tf.keras.layers.Dense 例如,请注意权重和偏置张量是在该函数中创建的.Layer.build() 方法接受一个 input_shape 参数,权重和偏差的形状通常取决于输入的形状.

The Layer.build() method is typically used to instantiate the weights of the layer. See the source code for tf.keras.layers.Dense for an example, and note that the weight and bias tensors are created in that function. The Layer.build() method takes an input_shape argument, and the shape of the weights and biases often depend on the shape of the input.

另一方面,Layer.call() 方法实现了层的前向传递.您不想覆盖 __call__,因为它是在基类 tf.keras.layers.Layer 中实现的.在自定义层中,您应该实现 call().

The Layer.call() method, on the other hand, implements the forward-pass of the layer. You do not want to overwrite __call__, because that is implemented in the base class tf.keras.layers.Layer. In a custom layer, you should implement call().

Layer.call() 不调用 Layer.build().但是,如果层尚未构建,Layer().__call__() 确实调用它(source),这将设置一个属性= Trueself./code> 防止 Layer.build() 再次被调用.换句话说,Layer.__call__() 只在第一次调用时调用 Layer.build().

Layer.call() does not call Layer.build(). However, Layer().__call__() does call it if the layer has not been built yet (source), and that will set an attribute self.built = True to prevent Layer.build() from being called again. In other words, Layer.__call__() only calls Layer.build() the first time it is called.

这篇关于Tensorflow build() 如何从 tf.keras.layers.Layer 工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆