繁体   English   中英

使用深度神经网络对时间序列数据进行 Keras 回归 model

[英]Making Keras regression model with time series data with Deep Neural Network

我想用其中包含时间序列的 Keras DNN 进行回归。 是股价预测。 我的 dataframe 有 1300 行。 这是样本:

        Date    Open    Close   High    Low     High-Low Day    Price
0   2016-04-11  94.19   93.95   94.34   93.75   0.59    0       422.7
1   2016-04-12  93.97   93.96   94.39   93.63   0.76    1       427.3
2   2016-04-13  94.05   94.74   94.85   94.00   0.85    2       426.7
3   2016-04-14  94.81   94.90   95.20   94.68   0.52    3       425.4
4   2016-04-15  94.94   94.70   95.05   94.51   0.54    4       429.9

我制作了从 0 到len(df)的“日”列,因为我不知道如何处理日期。 我的 Keras model 看起来像这样:

model = Sequential()
model.add(Dense(200, input_dim = features.shape[1], activation = 'relu')) # input layer requires input_dim param
model.add(Dense(400, activation = 'relu'))
model.add(Dense(200, activation = 'relu'))
model.add(Dropout(0.2))
model.add(Dense(1, activation='softmax'))

我使用的是平均绝对平方误差。

opt = SGD(lr = 0.00025, momentum = 0.01)
model.compile(loss="mean_absolute_percentage_error", optimizer = 'adam', metrics=['mape'])

es = EarlyStopping(monitor='val_loss', min_delta = 0.02, patience = 300, verbose=1, mode='auto')
history = model.fit(features, results, validation_split = 0.1, shuffle = False, epochs = 750, batch_size=50, verbose=2, callbacks=[es])

score = model.evaluate(x_test, y_test, batch_size=50)
print()
print(history.history.keys())
print()
print(score) #[99.96269438797283, 99.9627]

print('Test loss:', score[0],  'Test accuracy:', score[1])

问题是我的损失没有改变,它总是一样的。 所以我想问你,我做错了什么? 为什么 my.network 没有学到任何东西。

如何检查准确性和损失? 我将其用于 plot 学习曲线:

fig = plt.figure(figsize=(12,8), dpi= 100, facecolor='w', edgecolor='k')

plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('model accuracy')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()


# summarize history for loss
fig=plt.figure(figsize=(13,9), dpi= 100, facecolor='w', edgecolor='k')

plt.plot(history.history['mean_absolute_percentage_error'])
plt.plot(history.history['val_mean_absolute_percentage_error'])
plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()

但我总是得到两条扁平(水平)线。

好的,所以如果我将激活 function 更改为“relu”,它会工作得更好,但我的MAPE大约是 50,而且很多。 有没有我错过的步骤? 我应该使用“Adam”还是“SGD”作为优化器,我应该使用任何其他激活 function,shuffle 应该是 True 还是 False? 1300行够吗?

您应该将最后一层激活从softmax更改为linear 试试这个

model.add(Dense(1, activation=None))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM