keras - 嵌入层mask_zero在后续层中导致异常

问题描述 投票:1回答:1

我正在研究基于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])
python machine-learning keras keras-layer word-embedding
1个回答
1
投票

但是,当我处理其中包含可变数量的单词的句子时,我确实需要在嵌入层中进行遮罩。

你是否填充句子以使它们具有相同的长度?如果是这样,那么您可以让模型自己找出0是填充的,而不是屏蔽,因此应该忽略。因此,您不需要显式屏蔽。此方法还用于处理此answer中建议的数据中的缺失值。

© www.soinside.com 2019 - 2024. All rights reserved.