如何在 lstm 中使用卷积 1D? [英] How to use convolution 1D with lstm ?

查看:141
本文介绍了如何在 lstm 中使用卷积 1D?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有时间序列数据输入 72 个值,通过单独的最后 6 个值进行测试预测.我想在 LSTM 中使用 CONV1D.

I have time series data input 72 value by separate last 6 value for test prediction. I want to use CONV1D with LSTM.

这是我的代码.

df = pd.read_csv('D://data.csv',
             engine='python')

df['DATE_'] = pd.to_datetime(df['DATE_']) + MonthEnd(1)
df = df.set_index('DATE_')
df.head()

split_date = pd.Timestamp('03-01-2015')

train = df.loc[:split_date, ['COLUMN3DATA']]
test = df.loc[split_date:, ['COLUMN3DATA']]

sc = MinMaxScaler()

train_sc = sc.fit_transform(train)
test_sc = sc.transform(test)

X_train = train_sc[:-1]
y_train = train_sc[1:]

X_test = test_sc[:-1]
y_test = test_sc[1:]

###################  Convolution  #######################

X_train_t = X_train[None,:]
print(X_train_t.shape)
X_test_t = X_test[:, None]

K.clear_session()
model = Sequential()

model.add(Conv1D(6, 3, activation='relu', input_shape=(12,1)))
model.add(LSTM(6, input_shape=(1,3), return_sequences=True))
model.add(LSTM(3))
model.add(Dense(1))

model.compile(loss='mean_squared_error', optimizer='adam' )

model.summary()

model.fit(X_train_t, y_train, epochs=400, batch_size=10, verbose=1)

y_pred = model.predict(X_test_t)

当我运行它时显示这样的错误

When I run it show error like this

ValueError: Error when checking input: expected conv1d_1_input to have shape (None, 12, 1) but got array with shape (1, 64, 1)

如何在 lstm 中使用 conv1D

How to use conv1D with lstm

推荐答案

问题在于输入数据和输入形状之间.

The problem is between your input data and your input shape.

  • 你在模型中说你的输入形状是 (12,1) (= batch_shape=(None,12,1))
  • 但是您的数据 X_train_t 具有 (1,64,1) 形状.
  • You said in the model that your input shape is (12,1) (= batch_shape=(None,12,1))
  • But your data X_train_t has shape (1,64,1).

要么修复模型的输入形状,要么修复数据(如果这不是预期的形状).

Either you fix the input shape of the model, or you fix your data if this is not the expected shape.

对于可变长度/时间步长,您可以使用 input_shape=(None,1).

For variable lengths/timesteps, you can use input_shape=(None,1).

第二层不需要 input_shape.

You don't need an input_shape in the second layer.

这篇关于如何在 lstm 中使用卷积 1D?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆