【问题标题】:Many-to-many lstm model on varying samples不同样本上的多对多 lstm 模型
【发布时间】:2018-12-20 16:58:22
【问题描述】:

我最近开始学习如何为多元时间序列数据构建 LSTM 模型。我看过herehere 来了解如何填充序列和实现多对多LSTM 模型。我创建了一个数据框来测试模型,但我不断收到错误(如下)。

d = {'ID':['a12', 'a12','a12','a12','a12','b33','b33','b33','b33','v55','v55','v55','v55','v55','v55'], 'Exp_A':[2.2,2.2,2.2,2.2,2.2,3.1,3.1,3.1,3.1,1.5,1.5,1.5,1.5,1.5,1.5], 
     'Exp_B':[2.4,2.4,2.4,2.4,2.4,1.2,1.2,1.2,1.2,1.5,1.5,1.5,1.5,1.5,1.5], 
     'A':[0,0,1,0,1,0,1,0,1,0,1,1,1,0,1], 'B':[0,0,1,1,1,0,0,1,1,1,0,0,1,0,1],
     'Time_Interval': ['11:00:00', '11:10:00', '11:20:00', '11:30:00', '11:40:00',
                '11:00:00', '11:10:00', '11:20:00', '11:30:00',
                '11:00:00', '11:10:00', '11:20:00', '11:30:00', '11:40:00', '11:50:00']}

df = pd.DataFrame(d)
df.set_index('Time_Interval', inplace=True)

我尝试使用蛮力填充:

from keras.preprocessing.sequence import pad_sequences

x1 = df['A'][df['ID']== 'a12']
x2 = df['A'][df['ID']== 'b33']
x3 = df['A'][df['ID']== 'v55']

mx = df['ID'].size().max() # Find the largest group
seq1 = [x1, x2, x3]
padded1 = np.array(pad_sequences(seq1, maxlen=6, dtype='float32')).reshape(-1,mx,1)

我以类似的方式为每个功能创建了padded2padded3padded4

padded_data = np.dstack((padded1, padded1, padded3, padded4))
padded_data.shape = (3, 6, 4)

padded_data

array([[[0. , 0. , 0. , 0. ],
        [0. , 0. , 2.2, 2.4],
        [0. , 0. , 2.2, 2.4],
        [1. , 1. , 2.2, 2.4],
        [0. , 0. , 2.2, 2.4],
        [1. , 1. , 2.2, 2.4]],

       [[0. , 0. , 0. , 0. ],
        [0. , 0. , 0. , 0. ],
        [0. , 0. , 3.1, 1.2],
        [1. , 1. , 3.1, 1.2],
        [0. , 0. , 3.1, 1.2],
        [1. , 1. , 3.1, 1.2]],

       [[0. , 0. , 1.5, 1.5],
        [1. , 1. , 1.5, 1.5],
        [1. , 1. , 1.5, 1.5],
        [1. , 1. , 1.5, 1.5],
        [0. , 0. , 1.5, 1.5],
        [1. , 1. , 1.5, 1.5]]], dtype=float32)

编辑

#split into train/test
train = pad_1[:2]   # train on the 1st two samples.
test = pad_1[-1:]    

train_X = train[:,:-1]  # one step ahead prediction.
train_y = train[:,1:]

test_X = test[:,:-1]  # test on the last sample
test_y = test[:,1:]
# check shapes
print(train_X.shape, train_y.shape, test_X.shape, test_y.shape)
#(2, 5, 4) (2, 5, 4) (1, 5, 4) (1, 5, 4)

# design network
model = Sequential()
model.add(Masking(mask_value=0., input_shape=(train.shape[1], train.shape[2])))
model.add(LSTM(32, input_shape=(train.shape[1], train.shape[2]), return_sequences=True))
model.add(Dense(4))
model.compile(loss='mae', optimizer='adam', metrics=['accuracy'])
model.summary()

# fit network
history = model.fit(train, test, epochs=300, validation_data=(test_X, test_y), verbose=2, shuffle=False)

[![在此处输入图片描述][3]][3]

所以我的问题是:

  1. 当然,必须有一种有效的方法来转换数据?
  2. 假设我想要对未来序列的单个时间步长预测,我有

first time-step = array([[[0.5 , 0.9 , 2.5, 3.5]]], dtype=float32) 其中第一个时间步是序列的单个“帧”。 如何调整模型以纳入这一点?

【问题讨论】:

    标签: python machine-learning keras lstm recurrent-neural-network


    【解决方案1】:

    要解决该错误,请从 LSTM 层参数中删除 return_sequence=True(由于您已定义此架构,因此您只需要最后一层的输出)并且还只需使用 train[:, -1]test[:, -1](而不是 @ 987654325@ 和test[:, -1:]) 来提取标签(即移除: 会导致第二个轴被丢弃,从而使标签形状与模型的输出形状一致)。

    附带说明,将Dense 层包裹在TimeDistributed 层内是多余的,因为the Dense layer is applied on the last axis


    更新:对于新问题,要么填充只有一个时间步长的输入序列,使其形状为(5,4),要么设置第一层的输入形状(即Masking) 到input_shape=(None, train.shape[2]),因此模型可以处理不同长度的输入。

    【讨论】:

    • 谢谢。我觉得我在这里忘记了。我理解这一点的方式,因为它是多对多的,这里的输入维度是(X_train,1,4),输出维度=(y_train,1,4)。致密层是(Dense(4))。所以我选择我的训练数据作为前 2 个样本。所以 X_train(11, 1, 4) 和 y_train(1,1,4)。然后对第三个样品进行测试。 x_test=(5,1,4) 和 y_test=(1,1,4)。我想在回复您之前对此进行测试,但我无法创建这些形状。
    • @A.Abs 我根据您的实现理解这一点的方式:您为模型提供形状为(5, 4) 的输入,即长度为 4 的 5 个时间步长,并且您希望获得输出形状(1, 4),即长度为 4 的一个时间步长,实际上是未来的下一个时间步长。现在,告诉我我错过了什么?
    • 是的,您理解我并感谢您的努力,只是我没有将问题解释得足够清楚。请查看修改后的版本。
    猜你喜欢
    • 2021-07-07
    • 2021-10-31
    • 1970-01-01
    • 1970-01-01
    • 2016-07-12
    • 2019-02-23
    • 2020-01-08
    • 1970-01-01
    相关资源
    最近更新 更多