gpt4 book ai didi

python - 不同样本上的多对多 lstm 模型

转载 作者:行者123 更新时间:2023-11-30 09:05:11 25 4
gpt4 key购买 nike

我最近开始学习如何为多元时间序列数据构建 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)

以类似的方式,我为每个功能创建了 pangled2plined3plined4:

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. 假设我想要对 future 序列进行单时间步预测,我有

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

最佳答案

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

顺便说一句,将 Dense 层包装在 TimeDistributed 层内是多余的,因为 the Dense layer is applied on the last axis .

<小时/>

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

关于python - 不同样本上的多对多 lstm 模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53873020/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com