Keras LSTM模型未能学习

几天前我编写了这段代码,遇到了一些错误,但在一些帮助下,我能够修复它们。然而,模型仍然未能学习。我尝试了不同的批次大小、不同的epoch数量、不同的激活函数,并且多次检查了数据以寻找缺陷,但没有发现任何问题。这是一个学校项目,大约一周后要交。任何帮助都将非常珍贵。

这是代码。

from keras.layers import Dense, Input, Concatenate, Dropoutfrom sklearn.preprocessing import MinMaxScalerfrom keras.models import Modelfrom keras.layers import LSTMimport tensorflow as tfimport NetworkRequest as NRimport ParseNetworkRequest as PNRimport numpy as npdef buildModel():    _Price = Input(shape=(1, 1))    _Volume = Input(shape=(1, 1))    PriceLayer = LSTM(128)(_Price)    VolumeLayer = LSTM(128)(_Volume)    merged = Concatenate(axis=1)([PriceLayer, VolumeLayer])    Dropout(0.2)    dense1 = Dense(128, input_dim=2, activation='relu', use_bias=True)(merged)    Dropout(0.2)    dense2 = Dense(64, input_dim=2, activation='relu', use_bias=True)(dense1)    Dropout(0.2)    output = Dense(1, activation='softmax', use_bias=True)(dense2)    opt = tf.keras.optimizers.Adam(learning_rate=1e-3, decay=1e-6)    _Model = Model(inputs=[_Price, _Volume], output=output)    _Model.compile(optimizer=opt, loss='mse', metrics=['accuracy'])    return _Modelif __name__ == '__main__':    api_key = "47BGPYJPFN4CEC20"    stock = "DJI"    Index = ['4. close', '5. volume']    RawData = NR.Initial_Network_Request(api_key, stock)    Closing = PNR.Parse_Network_Request(RawData, Index[0])    Volume = PNR.Parse_Network_Request(RawData, Index[1])    Length = len(Closing)    scalar = MinMaxScaler(feature_range=(0, 1))    Closing_scaled = scalar.fit_transform(np.reshape(Closing[:-1], (-1, 1)))    Volume_scaled = scalar.fit_transform(np.reshape(Volume[:-1], (-1, 1)))    Labels_scaled = scalar.fit_transform(np.reshape(Closing[1:], (-1, 1)))    Train_Closing = Closing_scaled[:int(0.9 * Length)]    Train_Closing = np.reshape(Train_Closing, (Train_Closing.shape[0], 1, 1))    Train_Volume = Volume_scaled[:int(0.9 * Length)]    Train_Volume = np.reshape(Train_Volume, (Train_Volume.shape[0], 1, 1))    Train_Labels = Labels_scaled[:int((0.9 * Length))]    Train_Labels = np.reshape(Train_Labels, (Train_Labels.shape[0], 1))    # -------------------------------------------------------------------------------------------#    Test_Closing = Closing_scaled[int(0.9 * Length):(Length - 1)]    Test_Closing = np.reshape(Test_Closing, (Test_Closing.shape[0], 1, 1))    Test_Volume = Volume_scaled[int(0.9 * Length):(Length - 1)]    Test_Volume = np.reshape(Test_Volume, (Test_Volume.shape[0], 1, 1))    Test_Labels = Labels_scaled[int(0.9 * Length):(Length - 1)]    Test_Labels = np.reshape(Test_Labels, (Test_Labels.shape[0], 1))    Predict_Closing = Closing_scaled[-1]    Predict_Closing = np.reshape(Predict_Closing, (Predict_Closing.shape[0], 1, 1))    Predict_Volume = Volume_scaled[-1]    Predict_Volume = np.reshape(Predict_Volume, (Predict_Volume.shape[0], 1, 1))    Predict_Label = Labels_scaled[-1]    Predict_Label = np.reshape(Predict_Label, (Predict_Label.shape[0], 1))    model = buildModel()    model.fit(        [            Train_Closing,            Train_Volume        ],        [            Train_Labels        ],        validation_data=(            [                Test_Closing,                Test_Volume            ],            [                Test_Labels            ]        ),        epochs=10,        batch_size=Length    )

这是运行时的输出。

Using TensorFlow backend.2020-01-01 16:31:47.905012: I tensorflow/core/platform/profile_utils/cpu_utils.cc:94] CPU Frequency: 2199985000 Hz2020-01-01 16:31:47.906105: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x49214f0 executing computations on platform Host. Devices:2020-01-01 16:31:47.906137: I tensorflow/compiler/xla/service/service.cc:175]   StreamExecutor device (0): Host, Default Version/home/martin/PycharmProjects/MarketPredictor/Model.py:26: UserWarning: Update your `Model` call to the Keras 2 API: `Model(inputs=[<tf.Tenso..., outputs=Tensor("de...)`  _Model = Model(inputs=[_Price, _Volume], output=output)Train on 4527 samples, validate on 503 samplesEpoch 1/104527/4527 [==============================] - 1s 179us/step - loss: 0.4716 - accuracy: 2.2090e-04 - val_loss: 0.6772 - val_accuracy: 0.0000e+00Epoch 2/104527/4527 [==============================] - 0s 41us/step - loss: 0.4716 - accuracy: 2.2090e-04 - val_loss: 0.6772 - val_accuracy: 0.0000e+00Epoch 3/104527/4527 [==============================] - 0s 42us/step - loss: 0.4716 - accuracy: 2.2090e-04 - val_loss: 0.6772 - val_accuracy: 0.0000e+00Epoch 4/104527/4527 [==============================] - 0s 42us/step - loss: 0.4716 - accuracy: 2.2090e-04 - val_loss: 0.6772 - val_accuracy: 0.0000e+00Epoch 5/104527/4527 [==============================] - 0s 43us/step - loss: 0.4716 - accuracy: 2.2090e-04 - val_loss: 0.6772 - val_accuracy: 0.0000e+00Epoch 6/104527/4527 [==============================] - 0s 39us/step - loss: 0.4716 - accuracy: 2.2090e-04 - val_loss: 0.6772 - val_accuracy: 0.0000e+00Epoch 7/104527/4527 [==============================] - 0s 42us/step - loss: 0.4716 - accuracy: 2.2090e-04 - val_loss: 0.6772 - val_accuracy: 0.0000e+00Epoch 8/104527/4527 [==============================] - 0s 39us/step - loss: 0.4716 - accuracy: 2.2090e-04 - val_loss: 0.6772 - val_accuracy: 0.0000e+00Epoch 9/104527/4527 [==============================] - 0s 42us/step - loss: 0.4716 - accuracy: 2.2090e-04 - val_loss: 0.6772 - val_accuracy: 0.0000e+00Epoch 10/104527/4527 [==============================] - 0s 38us/step - loss: 0.4716 - accuracy: 2.2090e-04 - val_loss: 0.6772 - val_accuracy: 0.0000e+00Process finished with exit code 0

损失值很高,准确率为0。请帮助我解决这个问题。


回答:

您使用的是分类任务的激活函数和评估指标,而不是适合股票预测任务(具有连续目标)的函数和指标。

对于连续目标,您的最终激活层应该使用linear。评估指标应为msemae,而不是accuracy

accuracy仅在dji预测值与实际价格完全相同时才满足。由于dji至少有7位数字,这几乎是不可能的。

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注