lishiyun_9527 2020-06-19 14:52 采纳率: 0%
浏览 645

LSTM时间序列模型做销量预测多对多预测时的数据格式错误

最近对公司一款产品做销量预测,选用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)
  • 写回答

1条回答 默认 最新

  • 关注
    评论

报告相同问题?

悬赏问题

  • ¥15 opencv 无法读取视频
  • ¥15 用matlab 实现通信仿真
  • ¥15 按键修改电子时钟,C51单片机
  • ¥60 Java中实现如何实现张量类,并用于图像处理(不运用其他科学计算库和图像处理库))
  • ¥20 5037端口被adb自己占了
  • ¥15 python:excel数据写入多个对应word文档
  • ¥60 全一数分解素因子和素数循环节位数
  • ¥15 ffmpeg如何安装到虚拟环境
  • ¥188 寻找能做王者评分提取的
  • ¥15 matlab用simulink求解一个二阶微分方程,要求截图