LSTM堆叠RNN中TimeseriesGenerator的输入形状错误

时间:2019-07-28 22:20:07

标签: python keras deep-learning time-series lstm

我正在尝试使用13种数据功能创建一个预测未来库存数据的模型。我正在使用TimeseriesGenerator,但是当我尝试拟合模型时,出现错误消息:

ValueError:检查输入时出错:预期lstm_1_input具有形状(529,13),但形状为(5,13)的数组

我的数据集有529行,我想用来预测未来5天。任何帮助,将不胜感激。

# Part 1 - Data Preprocessing

# Importing the Libraries
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from keras.preprocessing.sequence import TimeseriesGenerator


# Importing the Training Set
dataset_train = pd.read_csv('data.csv')
training_set_indicators = dataset_train.iloc[:, 1:14].values


# Feature Scaling
from sklearn.preprocessing import MinMaxScaler
sc = MinMaxScaler(feature_range = (0,1))
training_set_indicators_scaled = sc.fit_transform(training_set_indicators)
final_dataset = training_set_indicators_scaled


# Part 2 - Building the RNN

# Importing the Keras libraries and packages
from keras.models import Sequential
from keras.layers import LSTM
from keras.layers import Dense
from keras.layers import Dropout


n_features = final_dataset.shape[1]
n_lag = 5

generator = TimeseriesGenerator(final_dataset, final_dataset, length = n_lag, batch_size = 8)

# Initializing the RNN
regressor = Sequential()


# Adding the first LSTM layer and some Dropout Regularization
regressor.add(LSTM(units = 250, return_sequences = True, input_shape = (final_dataset.shape[0], final_dataset.shape[1])))
regressor.add(Dropout(0.2))


# Adding the second LSTM layer and some Dropout Regularization
regressor.add(LSTM(units = 250))
regressor.add(Dropout(0.2))


# Adding the Output Layer
regressor.add(Dense(units = 13))


# Compiling the RNN
regressor.compile(optimizer = 'adam', loss = 'mse')


# Fitting the RNN to the Training Set
regressor.fit_generator(generator, epochs = 100, verbose = 2)

1 个答案:

答案 0 :(得分:1)

length中的TimeseriesGenerator参数是指从序列中提取的时间步数。因此,在以length=n_lagn_lag=5的示例中,生成器正在生成长度为5的子序列。

因为已设置而引发了错误

input_shape = (final_dataset.shape[0], final_dataset.shape[1])

final_dataset.shape[0]=529,因此您的模型期望输入序列的长度为529。

至于

  

预测未来5天

您需要确定是要在选择当前序列中的子序列后预测5天,还是要预测完整序列后的5天(需要再加上5个标记的时间点进行训练)。在这两种情况下,您都应考虑将RNN的输出作为输入反馈。