我需要将可变长度序列提供给我的模型。
我的模特是Embedding + LSTM + Conv1d + Maxpooling + softmax
。
当我在mask_zero = True
中设置Embedding
时,我无法在Conv1d
编译。
如何在Conv1d
中输入掩码值或是否有其他解决方案?
Conv1D层此时不支持屏蔽。这是keras repo上的open issue。
根据任务,你可以通过嵌入mask_value
就像序列中的其他值一样,并应用全局池(正如你现在所做的那样)。
Masking
层期望每个下游层都支持掩蔽,而Conv1D
层不是这种情况。幸运的是,使用Functional API还有另一种应用掩码的方法:
inputs = Input(...)
mask = Masking().compute_mask(inputs) # <= Compute the mask
embed = Embedding(...)(inputs)
lstm = LSTM(...)(embed, mask=mask) # <= Apply the mask
conv = Conv1D(...)(lstm)
...
model = Model(inputs=[inputs], outputs=[...])