【问题标题】:Time Series Prediction with LSTM in Keras在 Keras 中使用 LSTM 进行时间序列预测
【发布时间】:2017-07-31 07:24:00
【问题描述】:

我是深度学习的初学者。我正在使用 keras 库实现 LSTM,以预测天气数据,我有训练和测试数据。删除一些变量后,我的输入数据具有以下形状。

('X_train', (117, 22))
('y_train', (117,))
('X_test', (13, 22))
('y_test', (13,))

现在,我将这些数据输入到下面的 LSTM 代码中,但无法弄清楚我遇到问题的输入形状。下面给出了我正在应用的 LSTM 的完整代码。

import os
print os.getcwd()
import pandas
import numpy
import matplotlib.pyplot as plt
import math
from sklearn.metrics import mean_squared_error


train = pandas.read_excel('./data/train.xlsx', sheetname = 'temp4')
print train.head()
print train.shape

test = pandas.read_excel('./data/test.xlsx', sheetname = 'temp4')
print test.head()
print test.shape

# lagsp has 7 misssing values in train data and rest is tha in all entries and also drop un-necessary variable
train = train.drop(['WEEK_NBR', 'DOS_YEAR', 'sorted row','lagsp'], axis = 1)
test = test.drop(['WEEK_NBR', 'DOS_YEAR', 'sorted row','lagsp'], axis = 1)

print train.shape
print test.shape

train = train.values
test = test.values

X_train = train[:,0:22]
y_train = train[:,22]

X_test = test[:,0:22]
y_test = test[:,22]

print("X_train", X_train.shape)
print("y_train", y_train.shape)
print("X_test", X_test.shape)
print("y_test", y_test.shape)




timesteps = X_train.shape[0]
features = X_train.shape[1]

X_train = X_train.reshape(1, timesteps, features)
X_test = X_test.reshape(1, X_test.shape[0], X_test.shape[1])



print 'timesteps', timesteps
print 'features', features



numpy.random.seed(7)


from keras.models import Sequential
from keras.layers import Dense, Dropout


###########################
#   RNN
###########################
from keras.layers.recurrent import LSTM

d = 0.2
rnn_model = Sequential()
rnn_model.add(LSTM(64, input_shape= (117,22), return_sequences=True))
rnn_model.add(Dropout(d))
rnn_model.add(Dense(16,kernel_initializer='uniform',activation='relu'))
rnn_model.add(Dense(1,kernel_initializer='uniform',activation='linear'))
rnn_model.compile(loss='mse',optimizer='rmsprop',metrics=['accuracy'])

#batch_input_shape=(batch_size, timesteps, data_dim)

rnn_model.fit(
    X_train,
    numpy.array(y_train),
    batch_size=10,
    epochs=10)

# make predictions
trainPredict_rnn = rnn_model.predict(X_train)
testPredict_rnn = rnn_model.predict(X_test)
# calculate root mean squared error
trainScore_rnn = math.sqrt(mean_squared_error(y_train, trainPredict_rnn))
print('Train Score: %.2f RMSE' % (trainScore_rnn))
testScore_rnn = math.sqrt(mean_squared_error(y_test, testPredict_rnn))
print('Test Score: %.2f RMSE' % (testScore_rnn))

# plot predictions

plt.figure(figsize=(20,10))
plt.plot(y_train) # blue # orange
plt.plot(trainPredict_rnn)
plt.show()

plt.plot(y_test) # blue # orange
plt.plot(testPredict_rnn)
plt.show()

这是错误,我在运行代码后得到,模型拟合 X_train 和 y_train,具有上述输入形状。

Traceback (most recent call last):
  File "/home/shivampanchal/PycharmProjects/WeatherPrediction/try.py", line 81, in <module>
    epochs=10)
  File "/usr/local/lib/python2.7/dist-packages/keras/models.py", line 856, in fit
    initial_epoch=initial_epoch)
  File "/usr/local/lib/python2.7/dist-packages/keras/engine/training.py", line 1429, in fit
    batch_size=batch_size)
  File "/usr/local/lib/python2.7/dist-packages/keras/engine/training.py", line 1309, in _standardize_user_data
    exception_prefix='target')
  File "/usr/local/lib/python2.7/dist-packages/keras/engine/training.py", line 127, in _standardize_input_data
    str(array.shape))
ValueError: Error when checking target: expected dense_2 to have 3 dimensions, but got array with shape (117, 1)

【问题讨论】:

  • 如果您在 SO 上环顾四周,您会发现很多问题的答案。你的问题也不是很清楚。代码中断是因为您使用了错误的输入形状(提示,您的输入形状错误),还是您想澄清其他内容?阅读here,了解如何提出一个好的问题
  • 这是错误,我得到了。
  • 文件 "/home/shivampanchal/PycharmProjects/WeatherPrediction/try.py",第 66 行,在 epochs=10) 文件 "/usr/local/lib/python2.7/dist- packages/keras/engine/training.py”,第 1305 行,在 _standardize_user_data exception_prefix='input') 文件“/usr/local/lib/python2.7/dist-packages/keras/engine/training.py”,第 127 行, in _standardize_input_data str(array.shape)) ValueError: Error when checks input: expected lstm_1_input to have 3 dimensions, but got array with shape (117, 22)
  • Keras 中的 LSTM 采用 3d 张量,因此您需要添加另一个维度(1、117、22)。

标签: python time-series keras lstm rnn


【解决方案1】:

X_train 需要是三维的。在您的第一层中指定input_shape 时,您指定的是(timesteps, features)。然后,在将实际数据传递给fit() 时,需要传入一个三维数组,其中每个样本的形状为(timesteps, features)

timesteps = X_train.shape[0]
features = X_train.shape[1]

X_train = X_train.reshape(1, timesteps, features)

不过,这会给您留下一个训练样本,我怀疑这是否是您想要的。在不知道您的数据实际上是什么样子的情况下,很难提供进一步的帮助!您似乎更有可能希望将数据集分解为一些固定时间步长的序列。此外,您还需要对X_test 应用类似的转换。

【讨论】:

  • 谢谢,我会试试这个。数据集包含 22 个变量,我需要预测一个变量,它已经在 y-train 中。经过这样的处理,应该提供什么输入形状。
  • 你当前的输入形状没问题。
  • 妮可,我已经更改了代码。给你,但在 y_train 中出现错误。训练时。
猜你喜欢
  • 1970-01-01
  • 2021-11-09
  • 1970-01-01
  • 2017-10-03
  • 2021-09-15
  • 2018-09-04
  • 1970-01-01
  • 2019-12-26
相关资源
最近更新 更多