简体   繁体   English

Tensorflow build() 如何从 tf.keras.layers.Layer 工作

[英]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.

相关问题 如何在自定义 tf.keras.layers.Layer 中支持遮罩 - How to support masking in custom tf.keras.layers.Layer 如何在不初始化的情况下将 tf.keras.layers.layer 分配给一个类? - How to assign a tf.keras.layers.layer to a class without initializing it? TensorFlow - tf.keras.layers.Layer 与 tf.keras.Model 之间的区别 - TensorFlow - Difference between tf.keras.layers.Layer vs tf.keras.Model 就优化损失函数而言,tf.keras.layers.Layer中的kernel_regularizer参数实际上实现了什么? - What does the kernel_regularizer parameter in a tf.keras.layers.Layer actually implement in terms of the loss function being optimized? InaccessibleTensorError - 在另一层的循环条件下使用 `tf.keras.layers.Layer` output 时 - InaccessibleTensorError - When using `tf.keras.layers.Layer` output in loop condition of another layer 使用 tf.keras.layers.concatenate() 作为 tensorflow 中的自定义层 - Use tf.keras.layers.concatenate() as custom layer in tensorflow 当指南说不要将两者混合时,tf.keras.layers.Layer 的`trainable_variables` 仍然包括 tf.Module 的那些? - The `trainable_variables` of tf.keras.layers.Layer still include those of tf.Module when guide says don't mix the two? tf.keras.layers.pop()不起作用,但是tf.keras._layers.pop()没有 - tf.keras.layers.pop() doesn't work, but tf.keras._layers.pop() does TensorFlow 2.0 如何从 tf.keras.layers 层获取可训练变量,如 Conv2D 或 Dense - TensorFlow 2.0 How to get trainable variables from tf.keras.layers layers, like Conv2D or Dense 为什么 tf.keras 不起作用,但 tensorflow.keras 是否有效? - Why tf.keras dosen't work but tensorflow.keras does even though tensorflow is imported as tf?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM