简体   繁体   English

Keras LSTM 模型未学习

[英]Keras LSTM Model not learning

I wrote this code a few days ago and I had a few bugs but with some help, I was able to fix them.几天前我写了这段代码,我有一些错误,但在一些帮助下,我能够修复它们。 The Model is not learning.模型不是在学习。 I tried different batch sizes, different amount of epochs, different activation functions, checked my data a few times for flaws I wasn't able to find any.我尝试了不同的批次大小、不同的时期数、不同的激活函数,并多次检查我的数据以查找我无法找到的缺陷。 It is due in a week or so for a school project.一个学校项目将在一周左右的时间内到期。 Any help will be very much valued.任何帮助都将受到高度重视。

Here is the code.这是代码。

from keras.layers import Dense, Input, Concatenate, Dropout
from sklearn.preprocessing import MinMaxScaler
from keras.models import Model
from keras.layers import LSTM
import tensorflow as tf
import NetworkRequest as NR
import ParseNetworkRequest as PNR
import numpy as np


def 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 _Model


if __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
    )

This is the output when I run it.这是我运行时的输出。

Using TensorFlow backend.
2020-01-01 16:31:47.905012: I tensorflow/core/platform/profile_utils/cpu_utils.cc:94] CPU Frequency: 2199985000 Hz
2020-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 samples
Epoch 1/10

4527/4527 [==============================] - 1s 179us/step - loss: 0.4716 - accuracy: 2.2090e-04 - val_loss: 0.6772 - val_accuracy: 0.0000e+00
Epoch 2/10

4527/4527 [==============================] - 0s 41us/step - loss: 0.4716 - accuracy: 2.2090e-04 - val_loss: 0.6772 - val_accuracy: 0.0000e+00
Epoch 3/10

4527/4527 [==============================] - 0s 42us/step - loss: 0.4716 - accuracy: 2.2090e-04 - val_loss: 0.6772 - val_accuracy: 0.0000e+00
Epoch 4/10

4527/4527 [==============================] - 0s 42us/step - loss: 0.4716 - accuracy: 2.2090e-04 - val_loss: 0.6772 - val_accuracy: 0.0000e+00
Epoch 5/10

4527/4527 [==============================] - 0s 43us/step - loss: 0.4716 - accuracy: 2.2090e-04 - val_loss: 0.6772 - val_accuracy: 0.0000e+00
Epoch 6/10

4527/4527 [==============================] - 0s 39us/step - loss: 0.4716 - accuracy: 2.2090e-04 - val_loss: 0.6772 - val_accuracy: 0.0000e+00
Epoch 7/10

4527/4527 [==============================] - 0s 42us/step - loss: 0.4716 - accuracy: 2.2090e-04 - val_loss: 0.6772 - val_accuracy: 0.0000e+00
Epoch 8/10

4527/4527 [==============================] - 0s 39us/step - loss: 0.4716 - accuracy: 2.2090e-04 - val_loss: 0.6772 - val_accuracy: 0.0000e+00
Epoch 9/10

4527/4527 [==============================] - 0s 42us/step - loss: 0.4716 - accuracy: 2.2090e-04 - val_loss: 0.6772 - val_accuracy: 0.0000e+00
Epoch 10/10

4527/4527 [==============================] - 0s 38us/step - loss: 0.4716 - accuracy: 2.2090e-04 - val_loss: 0.6772 - val_accuracy: 0.0000e+00

Process finished with exit code 0

The loss is high, and the accuracy is 0. Please help.损失高,准确率为0,请帮忙。

You're using activation functions and metrics made for a classification task, not a stock forecasting task (with a continuous target).您正在使用为分类任务创建的激活函数和指标,而不是股票预测任务(具有连续目标)。

For continuous targets, your final activation layer should be linear .对于连续目标,您的最终激活层应该是linear Metrics should be mse or mae , not accuracy .指标应该是msemae ,而不是accuracy

accuracy would only be satisfied is the dji prediction is exactly equal to the actual price.只有dji预测与实际价格完全相同,才能满足accuracy Since dji has at least 7 digits, it's nearly impossible.由于dji至少有 7 位数字,这几乎是不可能的。

Here's my suggestion:这是我的建议:

  1. Use a simpler network: Not sure how big is your dataset, but sometimes using dense.使用更简单的网络:不确定您的数据集有多大,但有时会使用密集网络。 layer isn't helpful.层没有帮助。 Looks like the weights of there intermediate layers are not changing at all.看起来中间层的权重根本没有变化。 Try the model with just one dense layer.尝试只有一个密集层的模型。
  2. Reduce dropout : Try with using one dropout layer with Dropout(0.1) .减少 dropout :尝试在Dropout(0.1)使用一个 dropout 层。
  3. Adam defaults : Start with using adam optimizer with its default parameters. Adam defaults :首先使用带有默认参数的adam优化器。
  4. Metric selection : As mentioned by Nicolas's answer, use a regression metric instead of accuracy.指标选择:正如 Nicolas 的回答所提到的,使用回归指标而不是准确性。

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

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