[英]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文档,不清楚应该做什么来支持屏蔽
How do I support masking?我如何支持屏蔽?
How do I access the mask from the past layer?如何从过去的图层访问蒙版?
Assuming input of (batch, time, channels)
or `(batch, time) would the masks look different?假设输入
(batch, time, channels)
或 `(batch, time) 掩码看起来会不同吗? What will be their shapes?它们的形状会是什么?
How do I pass it on to the next layer?如何将其传递到下一层?
To support masking one should implement the compute_mask
method inside the custom layer为了支持屏蔽,应该在自定义层中实现
compute_mask
方法
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)
)
This cannot be guessed, it is the layer's before responsible to calculate the mask这个猜不透,是图层之前负责计算遮罩的
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
以传递正确的蒙版
Now explanation is also included in thetf.keras
masking and padding documentation .现在解释也包含在
tf.keras
屏蔽和填充文档中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.