python - Keras LSTM 模型无法学习

标签 python machine-learning keras lstm

我几天前编写了这段代码,遇到了一些错误,但在一些帮助下,我能够修复它们。模型不是在学习。我尝试了不同的批量大小、不同的周期数、不同的激活函数,多次检查我的数据是否有缺陷,但我无法找到任何缺陷。学校项目将在一周左右到期。任何帮助都将非常有值(value)。

这是代码。

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。 请帮忙。

最佳答案

您正在使用为分类任务而不是库存预测任务(具有连续目标)创建的激活函数和指标。

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

仅当 dji 预测完全等于实际价格时,

准确性才会得到满足。由于 dji 至少有 7 位数字,这几乎是不可能的。

关于python - Keras LSTM 模型无法学习,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59553390/

相关文章:

python - 使用子进程在python中执行一个linux命令,然后抓取结果提示

python - 为什么在sklearn线性回归中intercept_是一个数组?

python - 使用批归一化层创建顺序模型会卡住程序

python - 通过相关对象权限的 Django 权限

python - Kivy - 在屏幕中添加元素

php - 确定文本可能语言的算法

tensorflow - 不平衡图像数据集 (Tensorflow2)

tensorflow - 带有图像和标量的 keras 生成器

python - 使用 Python 和 Beautiful Soup 仅从页面上的 div 标签中提取文本

python - 渐变剪裁似乎在 None 上窒息