【问题标题】:Multivariate and multistep LSTM多变量和多步 LSTM
【发布时间】:2020-09-17 00:28:08
【问题描述】:

给出this example(部分:Train On Multiple Lag Timesteps Example),为了根据前 2 年的数据预测接下来 6 小时的污染,我是否应该设置 n_hours=17520,以及未来的步数我想预测 (set n_out=6) ?

否则,我在某处读到我还应该将密集层单元修改为要预测的未来步骤数(这里是 6),但是,它总是返回错误。会有什么问题?

谢谢

修改后的代码:

from math import sqrt
from numpy import concatenate
from matplotlib import pyplot
from pandas import read_csv
from pandas import DataFrame
from pandas import concat
from sklearn.preprocessing import MinMaxScaler
from sklearn.preprocessing import LabelEncoder
from sklearn.metrics import mean_squared_error
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import LSTM

# convert series to supervised learning
def series_to_supervised(data, n_in=1, n_out=1, dropnan=True):
    n_vars = 1 if type(data) is list else data.shape[1]
    df = DataFrame(data)
    cols, names = list(), list()
    # input sequence (t-n, ... t-1)
    for i in range(n_in, 0, -1):
        cols.append(df.shift(i))
        names += [('var%d(t-%d)' % (j+1, i)) for j in range(n_vars)]
    # forecast sequence (t, t+1, ... t+n)
    for i in range(0, n_out):
        cols.append(df.shift(-i))
        if i == 0:
            names += [('var%d(t)' % (j+1)) for j in range(n_vars)]
        else:
            names += [('var%d(t+%d)' % (j+1, i)) for j in range(n_vars)]
    # put it all together
    agg = concat(cols, axis=1)
    agg.columns = names
    # drop rows with NaN values
    if dropnan:
        agg.dropna(inplace=True)
    return agg

# load dataset
dataset = read_csv('pollution.csv', header=0, index_col=0)
values = dataset.values
# integer encode direction
encoder = LabelEncoder()
values[:,4] = encoder.fit_transform(values[:,4])
# ensure all data is float
values = values.astype('float32')
# normalize features
scaler = MinMaxScaler(feature_range=(0, 1))
scaled = scaler.fit_transform(values)
# specify the number of lag hours
n_hours = 17520
n_features = 8
# frame as supervised learning
reframed = series_to_supervised(scaled, n_hours, 6) # predict the next 6 hours
print(reframed.shape)

# split into train and test sets
values = reframed.values
n_train_hours = 365 * 24 *2
train = values[:n_train_hours, :]
test = values[n_train_hours:, :]
# split into input and outputs
n_obs = n_hours * n_features
train_X, train_y = train[:, :n_obs], train[:, -n_features]
test_X, test_y = test[:, :n_obs], test[:, -n_features]
print(train_X.shape, len(train_X), train_y.shape)
# reshape input to be 3D [samples, timesteps, features]
train_X = train_X.reshape((train_X.shape[0], n_hours, n_features))
test_X = test_X.reshape((test_X.shape[0], n_hours, n_features))
print(train_X.shape, train_y.shape, test_X.shape, test_y.shape)

# design network
model = Sequential()
model.add(LSTM(50, input_shape=(train_X.shape[1], train_X.shape[2])))
model.add(Dense(1))
model.compile(loss='mae', optimizer='adam')
# fit network
history = model.fit(train_X, train_y, epochs=50, batch_size=72, validation_data=(test_X, test_y), verbose=2, shuffle=False)
# plot history
pyplot.plot(history.history['loss'], label='train')
pyplot.plot(history.history['val_loss'], label='test')
pyplot.legend()
pyplot.show()

# make a prediction
yhat = model.predict(test_X)
test_X = test_X.reshape((test_X.shape[0], n_hours*n_features))
# invert scaling for forecast
inv_yhat = concatenate((yhat, test_X[:, -7:]), axis=1)
inv_yhat = scaler.inverse_transform(inv_yhat)
inv_yhat = inv_yhat[:,0]
# invert scaling for actual
test_y = test_y.reshape((len(test_y), 1))
inv_y = concatenate((test_y, test_X[:, -7:]), axis=1)
inv_y = scaler.inverse_transform(inv_y)
inv_y = inv_y[:,0]
# calculate RMSE
rmse = sqrt(mean_squared_error(inv_y, inv_yhat))
print('Test RMSE: %.3f' % rmse)

编辑: 我已将 Dense layer units 的值更改为 6,将 train_y.shape[1]test_y.shape[1] 更改为 6,如下所示:

# load dataset
dataset = read_csv('pollution.csv', header=0, index_col=0)
values = dataset.values
# integer encode direction
encoder = LabelEncoder()
values[:,4] = encoder.fit_transform(values[:,4])
# ensure all data is float
values = values.astype('float32')
# normalize features
scaler = MinMaxScaler(feature_range=(0, 1))
scaled = scaler.fit_transform(values)
# specify the number of lag hours
n_hours =48
n_out=6
n_features = 8
# frame as supervised learning
reframed = series_to_supervised(scaled, n_hours, n_out)
print(reframed.shape)

# split into train and test sets
values = reframed.values
n_train_hours = 365 * 24 * 2
train = values[:n_train_hours, :]
test = values[n_train_hours:, :]

# split into input and outputs
n_obs = n_hours * n_features
train_X, train_y = train[:, :n_obs], train[:, :6]#I put 6 instead of -n_features
print(train_X.shape, len(train_X), train_y.shape)
test_X, test_y = test[:, :n_obs], test[:,:6] # I put 6 instead of -n_features
print(test_X.shape, len(test_X), test_y.shape)

# reshape input to be 3D [samples, timesteps, features]
train_X = train_X.reshape((train_X.shape[0], n_hours, n_features))
train_y = train_y
test_X = test_X.reshape((test_X.shape[0], n_hours, n_features))
print(train_X.shape, train_y.shape, test_X.shape, test_y.shape)
train_y.shape

# design network
model = Sequential()
model.add(LSTM(5, input_shape=(train_X.shape[1], train_X.shape[2])))
model.add(Dense(6))
model.compile(loss='mae', optimizer='adam')
# fit network
history = model.fit(train_X, train_y, epochs=5, batch_size=72, validation_data=(test_X, test_y), verbose=2, shuffle=False) 
# plot history
pyplot.plot(history.history['loss'], label='train')
pyplot.plot(history.history['val_loss'], label='test')
pyplot.legend()
pyplot.show()

# make a prediction
yhat = model.predict(test_X)
test_X = test_X.reshape((test_X.shape[0], n_hours*n_features))

# invert scaling for forecast
inv_yhat = concatenate((yhat, test_X[:, -7:]), axis=1)
inv_yhat = scaler.inverse_transform(inv_yhat)
inv_yhat = inv_yhat[:,0]
# invert scaling for actual
test_y = test_y.reshape((len(test_y), 1))
inv_y = concatenate((test_y, test_X[:, -7:]), axis=1)
inv_y = scaler.inverse_transform(inv_y)
inv_y = inv_y[:,0]
# calculate RMSE
rmse = sqrt(mean_squared_error(inv_y, inv_yhat))
print('Test RMSE: %.3f' % rmse)

我遇到的错误:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-57-8e17d1d76420> in <module>
      9 # invert scaling for forecast
     10 inv_yhat = concatenate((yhat, test_X[:, -7:]), axis=1)
---> 11 inv_yhat = scaler.inverse_transform(inv_yhat)
     12 inv_yhat = inv_yhat[:,0]
     13 # invert scaling for actual

~\AppData\Local\Continuum\anaconda3\lib\site-packages\sklearn\preprocessing\data.py in inverse_transform(self, X)
    404                         force_all_finite="allow-nan")
    405 
--> 406         X -= self.min_
    407         X /= self.scale_
    408         return X

ValueError: operands could not be broadcast together with shapes (26227,13) (8,) (26227,13) 

【问题讨论】:

  • 正如您已经阅读的那样,您必须将输出层(Dense)设置为有 6 个神经元(每个预测小时一个),它应该与 train_y 的 shape[1] 匹配。如果还是不行,一定是数据有错误,请添加你得到的错误信息。
  • 好的@Pedrolarben 我已经更改了代码并添加了错误,请再次与我核对。谢谢

标签: python machine-learning deep-learning lstm forecasting


【解决方案1】:

正如错误输出所示,当您尝试反转 min-max 比例操作时会出现问题。问题是您已经为所有列安装了缩放器,现在您只需要反转数据集第一列的缩放。为了解决这个问题,本教程的作者将预测的列连接到其余属性,但您不能这样做,因为您要为每一行预测多个值。一个可能的解决方案可能是这个:

改变这个

# invert scaling for forecast
inv_yhat = concatenate((yhat, test_X[:, -7:]), axis=1)
inv_yhat = scaler.inverse_transform(inv_yhat)
inv_yhat = inv_yhat[:,0]
# invert scaling for actual
test_y = test_y.reshape((len(test_y), 1))
inv_y = concatenate((test_y, test_X[:, -7:]), axis=1)
inv_y = scaler.inverse_transform(inv_y)
inv_y = inv_y[:,0]

有了这个

# invert scaling for forecast
pred_scaler = MinMaxScaler(feature_range=(0, 1)).fit(dataset.values[:,0].reshape(-1, 1))
inv_yhat = pred_scaler.inverse_transform(yhat)
# invert scaling for actual
inv_y = pred_scaler.inverse_transform(test_y)

【讨论】:

  • 你好@Pedrolarben,谢谢你或你的回答它确实解决了广播问题谢谢。我试图绘制结果,我有多个图表合二为一,这正常吗?我用过这段代码: Ps:我是 python pyplot.title("Actual vs Prediction", fontsize=14) pyplot.plot(inv_y, label='Actual') pyplot.plot(inv_yhat, label='预测') pyplot.legend(loc="upper left") pyplot.xlabel("Time Periods") pyplot.ylabel(dataset.columns.values[0].title()) pyplot.show()
  • 获得多个图表是正常的。您应该使用 for 循环来迭代验证数据集。像这样:for i in range(10): pyplot.plot(inv_y[i], label='Actual') pyplot.plot(inv_yhat[i], label='Predicted') pyplot.show()。这将创建 10 个图来比较前 10 个示例的实际值和预测值。
  • 再次感谢您的回答@Pedrolarben。但是,我猜即使使用了“inverse.transform”函数,使用代码的“#invert scaling for forecast”部分也可以将值保持在 0 和 1 之间。通常,值应大于 1 !
  • 有一个错误,我使用的值认为它是数据框的值,但实际上,它是已经预处理的数据。请改用dataset.values,它应该可以工作
  • 我想通了,非常感谢@Pedrolarben :DI 最后一个问题:由于我正在进行 6 步预测,我认为我将只有 6 个未来值(基于最后 48 个值 (n_hours)),绘制预测值和实际值,如您所见,我注意到我有很多图表和值。我很困惑,请您向我解释一下。我会很感激的。
猜你喜欢
  • 2018-11-29
  • 1970-01-01
  • 2022-06-30
  • 1970-01-01
  • 2021-08-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-22
相关资源
最近更新 更多