【问题标题】:LSTM in Python generating flat forecastsPython 中的 LSTM 生成平面预测
【发布时间】:2019-09-25 02:23:50
【问题描述】:

我有 2017 年 7 月至 2018 年 12 月的每日数据,这些数据本质上是非平稳的,我正在尝试生成未来六个月的预测,即;从 2019 年 1 月到 2019 年 7 月。我尝试使用 SARIMAX 和 LSTM,但我得到的预测持平。这是我第一次使用 LSTM,所以我尝试了 RELU 和 Sigmoid 作为激活函数,但预测结果很平

SARIMA
SARIMAX
LSTM

以下是一个月的数据:

           values
X_Date  
2017-07-01  15006.17
2017-07-02  15125.35
2017-07-03  13553.20
2017-07-04  14090.07
2017-07-05  14341.84
2017-07-06  15037.23
2017-07-07  15588.56
2017-07-08  16592.55
2017-07-09  16851.91
2017-07-10  15630.53
2017-07-11  15501.26
2017-07-12  15852.34
2017-07-13  15020.60
2017-07-14  17115.26
2017-07-15  17668.73
2017-07-16  17604.95
2017-07-17  16686.89
2017-07-18  16523.80
2017-07-19  17642.11
2017-07-20  17803.65
2017-07-21  18756.53
2017-07-22  19220.46
2017-07-23  18876.94
2017-07-24  18103.97
2017-07-25  18034.74
2017-07-26  16650.10
2017-07-27  17247.02
2017-07-28  17620.62
2017-07-29  18210.39
2017-07-30  17015.64
scaler = MinMaxScaler()
train = daily_data.iloc[:365]
test = daily_data.iloc[365:]

scaler.fit(train)


scaled_train = scaler.transform(train)
scaled_test = scaler.transform(test)

from keras.preprocessing.sequence import TimeseriesGenerator

scaled_train
# define generator
n_input = 7
n_features = 1
generator = TimeseriesGenerator(scaled_train, scaled_train,    
length=n_input, batch_size=1)


from keras.models import Sequential
from keras.layers import Dense
from keras.layers import LSTM


# define model
model = Sequential()
model.add(LSTM(200, activation='sigmoid', input_shape=(n_input,     
n_features)))
model.add(Dense(1))
model.compile(optimizer='adam', loss='mse')

model.summary()

# fit model
model.fit_generator(generator,epochs=25)

model.history.history.keys()

loss_per_epoch = model.history.history['loss']
plt.plot(range(len(loss_per_epoch)),loss_per_epoch)

first_eval_batch = scaled_train[-7:]
first_eval_batch = first_eval_batch.reshape((1,n_input,n_features))

model.predict(first_eval_batch)

test_predictions = []

first_eval_batch = scaled_train[-n_input:]
current_batch = first_eval_batch.reshape((1, n_input, n_features))


np.append(current_batch[:,1:,:],[[[99]]],axis=1)



test_predictions = []

first_eval_batch = scaled_train[-n_input:]
current_batch = first_eval_batch.reshape((1, n_input, n_features))

for i in range(len(test)):

 # get prediction 1 time stamp ahead ([0] is for grabbing just the  
 number instead of [array])
 current_pred = model.predict(current_batch)[0]

 # store prediction
 test_predictions.append(current_pred) 

 # update batch to now include prediction and drop first value
 current_batch = np.append(current_batch[:,1:,:],                        

 [[current_pred]],axis=1)

预测是一条直线。

【问题讨论】:

    标签: python keras time-series lstm


    【解决方案1】:

    我遇到了类似的问题。尝试将“relu”设置为第一层的激活函数。 Here 是一个很棒的博客的链接。它包含许多非常有用的细节,尤其是如果您从机器学习开始。

    以下是我的旧模型的时期数对结果的影响。 5000 epochs 25 epochs 我也有点担心你拥有的训练数据量。我在 18,000 条记录上训练了我的模型以预测接下来的 24 小时,但我的模型分析了相当复杂的系统。我不知道什么描述了您的数据,但您必须考虑系统中可能的依赖项的数量以及您的训练数据可以为它们准备模型的程度。 我是机器学习的新手,但学到的是模型准备的最大部分是试错法。尤其是一开始。 我的答案开头的博客对我帮助很大,我建议你阅读它。

    我记得在我的例子中,我几乎在所有地方都使用了错误的激活函数。

    Here 是一篇关于欠拟合和过拟合的帖子。

    【讨论】:

    • 你是怎么解决的?我从 relu 开始,但这没有帮助
    • 你能发一张你的结果图吗?
    • 添加了每日和每月预测图
    【解决方案2】:

    几个问题:

    1. 18 个月的日常数据对于神经网络构建未来的准确预测可能并不重要。
    2. 您的模型只有 1 个 LSTM 层,添加第二个以受益于它的“记忆”:
    from keras.layers import Dropout
    
    # define model
    model = Sequential()
    model.add(LSTM(50, activation='relu', input_shape=(n_input,n_features), return_sequences=True))
    model.add(Dropout(.4))
    model.add(LSTM(100, activation='relu', return_sequences=False))
    model.add(Dropout(.4))
    model.add(Dense(1))
    model.compile(optimizer='adam', loss='mse')
    

    如果您提供数据,我可以仔细查看。您是否尝试过使用 n_input 变量?这可能会对您的模型产生影响。

    【讨论】:

    • 非常感谢,所以如果我打破数据@每日水平 tehre 超过 500 个数据点。共享数据的最佳方式是什么?
    • 大家好,谢谢大家。我想我已经能够解决我遇到的问题。我现在面临的挑战是做出时间预测。谁能帮我提前一个月做出预测。我有 2018 年 12 月之前的数据我想预测到 2019 年 3 月。有人可以帮忙吗?
    【解决方案3】:

    这是一个较旧的线程,但我遇到了类似的问题,实际上它只是欠拟合。显然,这可能有很多原因,但 OP 在他的脚本中有 25 个 epoch,这通常是不够的。当我对数据进行缩放时,我运行了 30 个 epoch 并得到了这个直线预测(我的计算机不是最好的,所以我认为我可以用更少的 epochs 侥幸逃脱):

    在增加 epoch 之后,我得到了更好的测试和训练结果。预测结果会像直线一样有很多其他原因,但希望提高时代对您有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-08-25
      • 2018-11-29
      • 2015-01-17
      • 1970-01-01
      • 2021-12-22
      • 1970-01-01
      • 2020-08-21
      • 2023-03-03
      相关资源
      最近更新 更多