Python中的LSTM生成平面预测

2024-04-16 06:56:07 发布

您现在位置:Python中文网/ 问答频道 /正文

enter image description hereenter image description here我有2017年7月至2018年12月的每日数据,这在性质上是非平稳的,我正试图生成未来六个月的预测,即2019年1月至2019年7月。我尝试过使用SARIMAX&LSTM,但我得到的预测是平的。这是我第一次使用LSTM,所以我尝试了RELU和Sigmoid作为激活函数,但是预测值是平的

SARIMA
SARIMAX
LSTM

以下是一个月的数据:

^{pr2}$
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)

预测是一条直线。在


Tags: fromtestinputmodelevalbatchtraincurrent
2条回答

我也有类似的问题。尝试在第一层设置“relu”作为激活函数。Here是一个伟大博客的链接。它包含了很多非常有用的细节,特别是如果你从机器学习开始。在

以下是我的旧模型的时代数对结果的影响。 5000 epochs25 epochs 我也有点担心你的培训数据量。我用18000条记录训练了我的模型来预测未来24小时,但我的模型分析了相当复杂的系统。我不知道什么描述了您的数据,但您必须考虑系统中可能存在的依赖项的数量,以及您的培训数据可以为它们准备多好的模型。 我刚开始学习机器,但学到的是模型准备的最大部分是试错法。尤其是刚开始的时候。 我的回答开头的博客对我有很大的帮助,我建议你读一下。在

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

Here是一篇关于不合身和过度合身的帖子。在

几个问题:

  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变量进行试验吗?这可能会对您的模型产生影响。在

相关问题 更多 >