不同样本的多对多 lstm 模型
Many-to-many lstm model on varying samples
我最近开始学习如何为多变量时间序列数据构建 LSTM 模型。我看过 here and here 如何填充序列和实现多对多 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
、padded3
和 padded4
:
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]
所以我的问题是:
- 当然,必须有一种有效的方法来转换数据?
- 假设我想要对未来序列进行单个时间步预测,我有
first time-step
= array([[[0.5 , 0.9 , 2.5, 3.5]]], dtype=float32)
其中第一个时间步长是序列的单个 'frame'。
如何调整模型以纳入这一点?
要解决该错误,请从 LSTM 层参数中删除 return_sequence=True
(因为您已经定义了这个架构,您只需要最后一层的输出)并且也只需使用 train[:, -1]
和 test[:, -1]
(而不是 train[:, -1:]
和 test[:, -1:]
)来提取标签(即删除 :
会导致第二个轴被丢弃,因此使标签形状与输出形状一致模型)。
附带说明一下,将 Dense
层包裹在 TimeDistributed
层内是多余的,因为 .
更新:至于新问题,要么填充只有一个时间步长的输入序列使其具有(5,4)
的形状,要么设置第一层的输入形状(即 Masking
)到 input_shape=(None, train.shape[2])
,因此模型可以处理不同长度的输入。
我最近开始学习如何为多变量时间序列数据构建 LSTM 模型。我看过 here and here 如何填充序列和实现多对多 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
、padded3
和 padded4
:
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]
所以我的问题是:
- 当然,必须有一种有效的方法来转换数据?
- 假设我想要对未来序列进行单个时间步预测,我有
first time-step
= array([[[0.5 , 0.9 , 2.5, 3.5]]], dtype=float32)
其中第一个时间步长是序列的单个 'frame'。
如何调整模型以纳入这一点?
要解决该错误,请从 LSTM 层参数中删除 return_sequence=True
(因为您已经定义了这个架构,您只需要最后一层的输出)并且也只需使用 train[:, -1]
和 test[:, -1]
(而不是 train[:, -1:]
和 test[:, -1:]
)来提取标签(即删除 :
会导致第二个轴被丢弃,因此使标签形状与输出形状一致模型)。
附带说明一下,将 Dense
层包裹在 TimeDistributed
层内是多余的,因为
更新:至于新问题,要么填充只有一个时间步长的输入序列使其具有(5,4)
的形状,要么设置第一层的输入形状(即 Masking
)到 input_shape=(None, train.shape[2])
,因此模型可以处理不同长度的输入。