不同样本上的多对多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)

我以类似的方式创建了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)第一个时间步是序列的单个“帧”。如何调整模型以纳入此范围?

今天

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

附带说明一下,将Dense一层包裹在一层内部TimeDistributed是多余的,因为在最后一个轴上施加了稠密层


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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章