我正在研究一个基于 this 的模型纸,由于 GlobalMaxPooling1D 层不支持 mask ,我收到异常。
我有一个 Embedding
层,其 mask_zero
参数设置为 True
。但是,由于后续的 GlobalMaxPooling1D 层不支持屏蔽,因此我收到了异常。异常是预料之中的,正如 the documentation of the Embedding layer 中实际所述的那样。 带有 mask_zero = True
的 Embedding
层之后的任何后续层都应支持屏蔽。
但是,由于我正在处理单词数量可变的句子,因此我确实需要在 Embedding
层中进行屏蔽。 (即由于输入长度不同)我的问题是,我应该如何改变我的模型,使掩蔽仍然是模型的一部分,并且不会在 GlobalMaxPooling1D
层引起问题?
下面是模型的代码。
model = Sequential()
embedding_layer = Embedding(dictionary_size, num_word_dimensions,
weights=[embedding_weights], mask_zero=True,
embeddings_regularizer=regularizers.l2(0.0001))
model.add(TimeDistributed(embedding_layer,
input_shape=(max_conversation_length, timesteps)))
model.add(TimeDistributed(Bidirectional(LSTM(m // 2, return_sequences=True,
kernel_regularizer=regularizers.l2(0.0001)))))
model.add(TimeDistributed(Dropout(0.2)))
model.add(TimeDistributed(GlobalMaxPooling1D()))
model.add(Bidirectional(LSTM(h // 2, return_sequences = True,
kernel_regularizer=regularizers.l2(0.0001)),
merge_mode='concat'))
model.add(Dropout(0.2))
crf = CRF(num_tags, sparse_target=False, kernel_regularizer=regularizers.l2(0.0001))
model.add(crf)
model.compile(optimizer, loss = crf.loss_function, metrics=[crf.accuracy])
最佳答案
However, as I am processing sentences with variable number of words in them, I do need the masking in the Embedding layer.
您是否填充了句子以使它们具有相等的长度?如果是这样,那么您可以让模型自行发现 0 是填充,因此应该被忽略,而不是屏蔽。因此,您不需要显式屏蔽。此方法还用于处理数据中的缺失值,如本 answer 中所建议的那样。 .
关于python - keras - 嵌入层 mask_zero 导致后续层出现异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55716755/