最近对公司一款产品做销量预测,选用LSTM时间序列模型来探索,时间维度以月为单位,已经尝试了一对一,多对一的探索,效果不理想,预测未来三个月的销量,结果容易拟合成一条曲线,现在尝试用多对多的方式测试一下,思路是用前三个月的销量预测后三个月的销量,例如用1,2,3月份的数据预测4,5,6月销量,2,3,4对5,6,7,以此类推,但是在训练模型的时候遇到了数据格式的问题,现在有三年多的销售数据,数据已经处理成3个月一组的格式,格式如下:
X_train = X_train.reshape((X_train.shape[0],1,X_train.shape[1]))
y_train = y_train.reshape((y_train.shape[0],1,y_train.shape[1]))
-->X_train.shape
(43, 1, 3)
--> y_train.shape
(43, 1, 3)
-->X_train
array([[[0. , 0.0040222 , 0.023007 ]],
[[0.0040222 , 0.023007 , 0.03394739]],
[[0.023007 , 0.03394739, 0.00530931]],
[[0.03394739, 0.00530931, 0.04529 ]],
[[0.00530931, 0.04529 , 0.0635508 ]],
[[0.04529 , 0.0635508 , 0.05301263]]......]
-->y_train
array([[[0.03394739, 0.00530931, 0.04529 ]],
[[0.00530931, 0.04529 , 0.0635508 ]],
[[0.04529 , 0.0635508 , 0.05301263]],
[[0.0635508 , 0.05301263, 0.10578393]],
[[0.05301263, 0.10578393, 0.08824712]].....]
模型信息如下:
from keras.models import Sequential
from keras.layers import Dense
import keras.backend as K
from keras.callbacks import EarlyStopping
from keras.layers import LSTM
model_lstm = Sequential()
model_lstm.add(LSTM(10,input_shape=(1, X_train.shape[2]),activation='sigmoid',kernel_initializer='lecun_uniform',return_sequences=True))
model_lstm.add(Dense(1))
model_lstm.compile(loss='mean_squared_error', optimizer='adam')
early_stop = EarlyStopping(monitor='loss', patience=5, verbose=1)
history_model_lstm = model_lstm.fit(X_train, y_train, epochs=500, batch_size=1, verbose=1, shuffle=False, callbacks=[early_stop])
最后提示目标值的格式貌似有问题,不知道怎么搞,求大神指点一二。。
ValueError Traceback (most recent call last)
<ipython-input-40-c6184d374dfd> in <module>
10 model_lstm.compile(loss='mean_squared_error', optimizer='adam')
11 early_stop = EarlyStopping(monitor='loss', patience=5, verbose=1)
---> 12 history_model_lstm = model_lstm.fit(X_train, y_train, epochs=500, batch_size=1, verbose=1, shuffle=False, callbacks=[early_stop])
c:\users\administrator\envs\shuju\lib\site-packages\keras\engine\training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, validation_freq, max_queue_size, workers, use_multiprocessing, **kwargs)
1152 sample_weight=sample_weight,
1153 class_weight=class_weight,
-> 1154 batch_size=batch_size)
1155
1156 # Prepare validation data.
c:\users\administrator\envs\shuju\lib\site-packages\keras\engine\training.py in _standardize_user_data(self, x, y, sample_weight, class_weight, check_array_lengths, batch_size)
619 feed_output_shapes,
620 check_batch_axis=False, # Don't enforce the batch size.
--> 621 exception_prefix='target')
622
623 # Generate sample-wise weight values given the `sample_weight` and
c:\users\administrator\envs\shuju\lib\site-packages\keras\engine\training_utils.py in standardize_input_data(data, names, shapes, check_batch_axis, exception_prefix)
143 ': expected ' + names[i] + ' to have shape ' +
144 str(shape) + ' but got array with shape ' +
--> 145 str(data_shape))
146 return data
147
ValueError: Error when checking target: expected dense_7 to have shape (1, 1) but got array with shape (1, 3)