首页 > 解决方案 > 不同样本上的多对多 lstm 模型

问题描述

我最近开始学习如何为多元时间序列数据构建 LSTM 模型。我在这里这里查看了如何填充序列和实现多对多 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)

以我创建的类似方式 ,padded2对于每个功能:padded3padded4

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) 其中第一个时间步是序列的单个“帧”。如何调整模型以纳入这一点?

标签: pythonmachine-learningkeraslstmrecurrent-neural-network

解决方案


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

作为旁注,将层包裹在Dense层内TimeDistributed是多余的,因为 Dense 层应用于最后一个 axis


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


推荐阅读