【问题标题】:Array reshape issue while time series prediction with ConvLSTM in Python在 Python 中使用 ConvLSTM 进行时间序列预测时数组重塑问题
【发布时间】:2020-01-10 10:24:47
【问题描述】:

我正在使用 ConvLSTM 进行时间步长 = 4 的时间序列预测(单变量序列)。数据框有两列,即。时间戳和金额。数量列包含连续值,然后我们将数据框分为训练和测试。训练数据有 12777 个观察值,测试有 3181 个观察值。代码如下:

from numpy import array
from keras.models import Sequential
from keras.layers import LSTM
from keras.layers import Dense
from keras.layers import Flatten
from keras.layers import ConvLSTM2D

# data prep
def split_sequence(sequence, n_steps):
    X, y = list(), list()
    for i in range(len(sequence)):
        end_ix = i + n_steps
        if end_ix > len(sequence)-1:
            break
        seq_x, seq_y = sequence[i:end_ix], sequence[end_ix]
        X.append(seq_x)
        y.append(seq_y)
    return array(X), array(y)

raw_seq = train['amount'].values
n_steps = 4
# split into samples
X, y = split_sequence(raw_seq, n_steps)
# reshape from [samples, timesteps] into [samples, timesteps, rows, columns, features]
n_features = 1
n_seq = 2
n_steps = 2
X = X.reshape((X.shape[0], n_seq, 1, n_steps, n_features))
# define model
model = Sequential()
model.add(ConvLSTM2D(filters=64, kernel_size=(1,2), activation='relu', input_shape=(n_seq, 1, n_steps, n_features)))
model.add(Flatten())
model.add(Dense(1))
model.compile(optimizer='adam', loss='mse')
# fit model
model.fit(X, y, epochs=500, verbose=0)

#prediction
raw_seq2 = test['amount'].values 
X_input, y_input = split_sequence(raw_seq2, n_steps)
X_input = X_input.reshape((X_input.shape[0], n_seq, 1, n_steps, n_features))
yhat = model.predict(X_input, verbose=0)

mean_absolute_error(y_input, yhat)

但是在执行以下操作时:

X_input = X_input.reshape((X_input.shape[0], n_seq,1, n_steps, n_features))

我收到此错误:

ValueError: cannot reshape array of size 6358 into shape (3179,2,2,1)

知道如何解决这个问题吗?

【问题讨论】:

    标签: python numpy keras conv-neural-network lstm


    【解决方案1】:

    看起来像一个纯数学问题:您正试图将尺寸 6358 的尺寸适合尺寸 3179 * 2 * 2 * 1 = 12716 的多个尺寸。尝试设置n_seq = 1n_steps = 1,这样在reshaping 时数据将具有相同的大小。

    【讨论】:

      猜你喜欢
      • 2015-10-01
      • 2017-11-11
      • 2021-11-09
      • 2023-01-24
      • 2020-09-07
      • 2016-01-04
      • 1970-01-01
      • 2012-12-25
      • 2017-10-03
      相关资源
      最近更新 更多