Keras LSTM模型无法学习

时间:2020-01-01 14:35:16

标签: python machine-learning keras lstm

几天前,我编写了这段代码,虽然遇到了一些错误,但是在一些帮助下,我得以将其修复。该模型没有学习。我尝试了不同的批处理大小,不同的时期数,不同的激活功能,并多次检查了我的数据以发现我找不到的缺陷。对于一个学校项目,大约需要一周的时间。任何帮助都将非常有价值。

这是代码。

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
    )

这是我运行时的输出。

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

损耗高,精度为0。 请帮忙。

2 个答案:

答案 0 :(得分:1)

您使用的是针对分类任务而非库存预测任务(具有连续目标)的激活功能和指标。

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

accuracy仅在dji预测准确等于实际价格的情况下才能满足。由于dji至少有7位数字,因此几乎是不可能的。

答案 1 :(得分:0)

这是我的建议:

  1. 使用更简单的网络:不确定数据集的大小,但有时使用密集的数据集。层没有帮助。看起来中间层的权重根本没有改变。尝试只有一层致密层的模型。
  2. 减少辍学:尝试在Dropout(0.1)中使用一个辍学图层。
  3. Adam默认值:首先使用adam优化器及其默认参数。
  4. 指标选择:如Nicolas的答案所述,请使用回归指标代替准确性。