简体   繁体   English

如何在自定义 tf.keras.layers.Layer 中支持遮罩

[英]How to support masking in custom tf.keras.layers.Layer

I'm implementing a custom tf.keras.layers.Layer that needs to support masking.我正在实现一个需要支持遮罩的自定义tf.keras.layers.Layer

Consider the following scenario考虑以下场景

embedded = tf.keras.layer.Embedding(input_dim=vocab_size + 1, 
                                    output_dim=n_dims, 
                                    mask_zero=True)
x = MyCustomKerasLayers(embedded)

Now per the documentation现在根据文档

mask_zero : Whether or not the input value 0 is a special "padding" value that should be masked out. mask_zero :输入值 0 是否是应屏蔽的特殊“填充”值。 This is useful when using recurrent layers which may take variable length input.这在使用可能需要可变长度输入的循环层时很有用。 If this is True then all subsequent layers in the model need to support masking or an exception will be raised .如果为 True,则模型中的所有后续层都需要支持掩码,否则将引发异常 If mask_zero is set to True, as a consequence, index 0 cannot be used in the vocabulary (input_dim should equal size of vocabulary + 1).如果 mask_zero 设置为 True,结果,索引 0 不能在词汇表中使用(input_dim 应等于词汇表的大小 + 1)。

I wonder, what does that mean?我想知道,这是什么意思? Looking through TensorFlow's custom layers guide and the tf.keras.layer.Layer documentation it is not clear what should be done to support masking查看TensorFlow 的自定义层指南tf.keras.layer.Layer文档,不清楚应该做什么来支持屏蔽

  1. How do I support masking?我如何支持屏蔽?

  2. How do I access the mask from the past layer?如何从过去的图层访问蒙版?

  3. Assuming input of (batch, time, channels) or `(batch, time) would the masks look different?假设输入(batch, time, channels)或 `(batch, time) 掩码看起来会不同吗? What will be their shapes?它们的形状会是什么?

  4. How do I pass it on to the next layer?如何将其传递到下一层?

  1. To support masking one should implement the compute_mask method inside the custom layer为了支持屏蔽,应该在自定义层中实现compute_mask方法

  2. To access the mask, simply add as the second positional argument in the call method the argument mask , and it will be accessible (ex. call(self, inputs, mask=None) )要访问掩码,只需在call方法中添加参数mask作为第二个位置参数,它就可以访问(例如call(self, inputs, mask=None)

  3. This cannot be guessed, it is the layer's before responsible to calculate the mask这个猜不透,是图层之前负责计算遮罩的

  4. Once you implemented the compute_mask passing the mask to the next layer happens automatically - excluding the case of model subclassing, which in this case it is up to you to calculate masks and pass them on.一旦你实现了compute_mask掩码,将掩码传递给下一层会自动发生 - 不包括模型子类化的情况,在这种情况下,由你来计算掩码并传递它们。

Example:例子:

class MyCustomKerasLayers(tf.keras.layers.Layer):
    def __init__(self, .......):
        ...

    def compute_mask(self, inputs, mask=None):
        # Just pass the received mask from previous layer, to the next layer or 
        # manipulate it if this layer changes the shape of the input
        return mask

    def call(self, input, mask=None):
        # using 'mask' you can access the mask passed from the previous layer

Notice that this example just passes on the mask, if the layer will output a shape different than the one received, you should change the mask accordingly in compute_mask to pass on the correct one请注意,此示例仅传递蒙版,如果图层将输出与接收到的形状不同的形状,则应相应地在compute_mask更改蒙compute_mask以传递正确的蒙版

EDIT编辑

Now explanation is also included in thetf.keras masking and padding documentation .现在解释也包含在tf.keras屏蔽和填充文档中

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在不初始化的情况下将 tf.keras.layers.layer 分配给一个类? - How to assign a tf.keras.layers.layer to a class without initializing it? Tensorflow build() 如何从 tf.keras.layers.Layer 工作 - How does Tensorflow build() work from tf.keras.layers.Layer TensorFlow - tf.keras.layers.Layer 与 tf.keras.Model 之间的区别 - TensorFlow - Difference between tf.keras.layers.Layer vs tf.keras.Model InaccessibleTensorError - 在另一层的循环条件下使用 `tf.keras.layers.Layer` output 时 - InaccessibleTensorError - When using `tf.keras.layers.Layer` output in loop condition of another layer 就优化损失函数而言,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? 当指南说不要将两者混合时,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.concatenate() 作为 tensorflow 中的自定义层 - Use tf.keras.layers.concatenate() as custom layer in tensorflow 如何在自定义训练循环中使用 tf.keras.layers.BatchNormalization()? - How to use the tf.keras.layers.BatchNormalization() in custom training loop? 计算掩蔽层并在 Keras 中稍后使用几层 - Computing Masking layer and using a few layers later in Keras 注意图层抛出 TypeError: Permute layer does not support masking in Keras - Attention Layer throwing TypeError: Permute layer does not support masking in Keras
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM