我正在研究基于this纸张的模型,由于GlobalMaxPooling1D
层不支持遮蔽,我得到了一个例外。
我有一个Embedding
图层,mask_zero
参数设置为True
。但是,由于后续的GlobalMaxPooling1D
层不支持屏蔽,我得到一个例外。例外情况是预期的,因为在the documentation of the Embedding layer中实际上已经说过Embedding
层与mask_zero = True
之后的任何后续层应该支持掩蔽。
但是,当我处理其中包含可变数量的单词的句子时,我确实需要在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])
但是,当我处理其中包含可变数量的单词的句子时,我确实需要在嵌入层中进行遮罩。
你是否填充句子以使它们具有相同的长度?如果是这样,那么您可以让模型自己找出0是填充的,而不是屏蔽,因此应该忽略。因此,您不需要显式屏蔽。此方法还用于处理此answer中建议的数据中的缺失值。