【问题标题】:Merging sequence embedding with Time Series Features将序列嵌入与时间序列特征合并
【发布时间】:2019-04-06 21:46:39
【问题描述】:

我在 LSTMKeras 实现的某些方面遇到了问题。这是对我的问题的描述:

我正在尝试训练一个单词正确性预测模型。我的模型有两种类型的输入:

  1. 一个词序列(句)
  2. 以及一系列特征向量(对于每个单词,我计算一个特征 victor 为 6)。

例如

input_1 = ['we', 'have', 'two', 'review'] 
input_2 = [
           [1.25, 0.01, 0.000787, 5.235, 0.0, 0.002091], 
           [ 0.0787, 0.02342, 5.4595, 0.002091, 0.003477, 0.0], 
           [0.371533, 0.529893, 0.371533, 0.6, 0.0194156, 0.003297],
           [0.471533, 0.635, 0.458, 0.7, 0.0194156, 0.0287]
          ] 

 gives output = [1, 1, 2, 1]

由于我的训练集中的每个句子都有不同的长度,我应该对我的所有句子进行零填充,以使它们都具有相同的长度。

我的问题是第二个输入怎么样,我应该做填充!如何?因为它们是向量。

模型架构:

input1 = Input(shape=(seq_length,), dtype='int32')
emb = Embedding(input_dim=num_words, output_dim = num_dimension, 
input_length=seq_length, weights=[embeddings], mask_zero=True,trainable=False)(input_layer)

input2 = Input(shape=(seq_length,6 ))
x = keras.layers.concatenate([emb, input2],axis=2)

lstm = LSTM(64, return_sequences=True)(x)
ackwards = LSTM(128, return_sequences=True, go_backwards=True)(x)

common = merge([forwards, backwards], mode='concat', concat_axis=-1)
out = TimeDistributed(Dense(no_targets, activation='softmax'))(lstm)

【问题讨论】:

    标签: keras lstm word-embedding sequence-to-sequence


    【解决方案1】:

    您在正确的轨道上,是的,您需要用零行填充您的第二个输入以匹配句子长度。基本上它看起来像这样:

    # Input 1
    X1 = [[12, 34, 3], [6, 7, 0]] # where numbers are word indices and 0 is padding
    # Input 2
    X2 = [[[1.23,...,2.4], [1.24, ...], [0.6, ...]], [[3.25, ...], [2.4, ...], [0,0,0,0,0]]]
    # So the padded words get zero feature vectors as well and the shapes match
    

    但不要害怕,因为您将 embinput2 连接起来,mask_zero=True 也会传播到连接的向量,因此 LSTM 实际上也忽略了来自第二个输入的填充。

    【讨论】:

    • 谢谢,虽然我不确定,但我已经尝试过了。在 1 个 epoch 后,我得到了 75% 的准确率。随着训练准确度开始攀升,更多的训练使验证准确度开始下降——这是过度拟合的明显迹象。有什么想法吗?
    • 嗯,是的,您可以将dropout=0.2 添加到 LSTM 以减少嵌入的过度拟合甚至丢失。另一种选择是减少每个单元的数量以减少模型的容量。
    猜你喜欢
    • 2021-07-18
    • 2020-04-30
    • 2023-02-22
    • 2022-12-14
    • 2018-05-11
    • 2019-12-12
    • 2018-07-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多