[英]How does Tensorflow build() work from 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.我想知道是否有人知道
build()
function 如何在引擎盖下从tf.keras.layers.Layer
class 工作。 According to the documentation :根据文档:
build is called when you know the shapes of the input tensors and can do the rest of the initialization
当您知道输入张量的形状并且可以执行初始化的 rest 时调用 build
so to me it seems like the class is behaving similar to this:所以对我来说,class 的行为似乎与此类似:
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)
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?我无法想象
build()
会永远被调用__call__
,但它是唯一传入输入的地方。有谁知道这到底是如何工作的?
The Layer.build()
method is typically used to instantiate the weights of the layer. Layer.build()
方法通常用于实例化层的权重。 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.有关示例,请参见
tf.keras.layers.Dense
的源代码,并注意在 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.build()
方法采用input_shape
参数,权重和偏差的形状通常取决于输入的形状。
The Layer.call()
method, on the other hand, implements the forward-pass of the layer.另一方面,
Layer.call()
方法实现了层的前向传递。 You do not want to overwrite __call__
, because that is implemented in the base class tf.keras.layers.Layer
.您不想覆盖
__call__
,因为这是在基础 class tf.keras.layers.Layer
中实现的。 In a custom layer, you should implement call()
.在自定义层中,您应该实现
call()
。
Layer.call()
does not call Layer.build()
. Layer.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.但是,如果层尚未构建( source ),
Layer().__call__()
确实会调用它,这将设置一个属性self.built = True
以防止再次调用Layer.build()
。 In other words, Layer.__call__()
only calls Layer.build()
the first time it is called.换句话说,
Layer.__call__()
仅在第一次调用时调用Layer.build()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.