简体   繁体   English

如何在 tf.keras 中屏蔽 lstm 的输入

[英]how do I mask the input of lstm in tf.keras

I am building a hybrid model (RNN on top of CNN) and I want to mask the input, the problem is我正在构建一个混合模型(基于 CNN 的 RNN)并且我想屏蔽输入,问题是
that mask_zero is not supported by conv layers. conv 层不支持 mask_zero。 I have tried to do masking and pass it to lstm like this:我试图做掩蔽并将其传递给 lstm 像这样:

inputs = tf.keras.layers.Input(shape=(100,))
     mask = tf.keras.layers.Masking().compute_mask(inputs) 
     embedding = tf.keras.layers.Embedding(self.preprocess["max_features"]+1, 300, input_length=100,
                 weights=[self.preprocess["matrix"]], trainable=True)(inputs) 
     lstm = tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(200,recurrent_dropout=0.2, dropout=0.2,return_sequences=True))(embedding,mask=mask)
     conv = tf.keras.layers.Conv1D(filters=200, kernel_size=3, padding='same', activation='relu')(lstm)

the 0 ind of matrix is vector of zeros.矩阵的 0 ind 是零向量。

I am getting the follwoing error from the lstm layer: IndexError: list assignment index out of range我从 lstm 层收到以下错误: IndexError: list assignment index out of range

Have you tried checking the Docs for Tensorflow ?你有没有试过检查Tensorflow的文档? Go to this link I think it will help you.转到此链接,我认为它会对您有所帮助。
In the above example, they add mask_zero=True在上面的例子中,他们添加了mask_zero=True

embedding = layers.Embedding(input_dim=5000, output_dim=16, mask_zero=True)
masked_output = embedding(padded_inputs)

print(masked_output._keras_mask)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM