![](/img/trans.png)
[英]Keras Neural Network model for binary classification with 3D tensor time series as input
[英]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.