LSTM:损失值没有变化

时间:2021-03-13 15:36:41

标签: tensorflow keras lstm

我正在预测股票趋势(上涨或下跌)。

以下是我处理预处理的方式。

index_ = len(df.columns) - 1
x = df.iloc[:,:index_]
x = x[['Relative_Volume', 'CurrentPrice', 'MarketCap']]
x = x.values.astype(float)
# x = x.reshape(len(x), 1, x.shape[1]).astype(float)
x = x.reshape(*x.shape, 1)
y = df.iloc[:,index_:].values.astype(float)

# x.shape = (44930, 3, 1)
# y.shape = (44930, 1)


x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.20, random_state=98 )

然后我正在构建我的 BILSTM 模型:

def build_nn():
    model = Sequential()    
    model.add(Bidirectional(LSTM(128, return_sequences=True, input_shape = (x_train.shape[0], 1) , name="one")))
    model.add(Dropout(0.20))
    model.add(Bidirectional(LSTM(128, return_sequences=True , name="two")))
    model.add(Dropout(0.20))
    model.add(Bidirectional(LSTM(64, return_sequences=False , name="three")))
    model.add(Dropout(0.20))
    model.add(Dense(1,activation='sigmoid'))
    # opt = Adam(lr=0.0001, beta_1=0.9, beta_2=0.999, decay=0.01)
    opt = SGD(lr=0.01)

    model.compile(loss='binary_crossentropy', optimizer=opt, metrics=['accuracy'])
    return model

filepath = "bilstmv1.h5"
chkp = ModelCheckpoint(monitor = 'val_accuracy', mode = 'auto', filepath=filepath, verbose = 1, save_best_only=True)


model = build_nn()
# model.summary()
model.fit(x_train, y_train,
                epochs=3,
                batch_size=256,
                validation_split=0.1, callbacks=[chkp])
model.summary()

下面是 loss_value 的输出:

Epoch 1/3
127/127 [==============================] - 27s 130ms/step - loss: 0.6829 - accuracy: 0.5845 - val_loss: 0.6797 - val_accuracy: 0.5803

Epoch 00001: val_accuracy improved from -inf to 0.58025, saving model to bilstmv1.h5
Epoch 2/3
127/127 [==============================] - 14s 112ms/step - loss: 0.6788 - accuracy: 0.5851 - val_loss: 0.6798 - val_accuracy: 0.5803

Epoch 00002: val_accuracy did not improve from 0.58025
Epoch 3/3
127/127 [==============================] - 14s 112ms/step - loss: 0.6800 - accuracy: 0.5822 - val_loss: 0.6798 - val_accuracy: 0.5803

Epoch 00003: val_accuracy did not improve from 0.58025

我尝试过更改优化器、loss_function 和其他修改。正如您所料,所有预测都是相同的,因为损失函数没有改变。

1 个答案:

答案 0 :(得分:1)

您的第一个 LSTM 层的输入形状有问题。 Keras 输入将 (None, Your_Shape) 作为其输入,因为您对模型的输入可能会有所不同。您可以有 1 个输入、2 个输入或无穷大输入。表示动态的唯一方法是使用 None 作为第一个输入。执行此操作的最快方法是将输入更改为 (None, *input_shape),因为 * 会扩展您的输入形状。

您的构建函数将变为:

def build_nn():
    model = Sequential()    
    model.add(Bidirectional(LSTM(128, return_sequences=True, input_shape = (None, *x_train.shape) , name="one")))
    model.add(Dropout(0.20))
    model.add(Bidirectional(LSTM(128, return_sequences=True , name="two")))
    model.add(Dropout(0.20))
    model.add(Bidirectional(LSTM(64, return_sequences=False , name="three")))
    model.add(Dropout(0.20))
    model.add(Dense(1,activation='sigmoid'))
    # opt = Adam(lr=0.0001, beta_1=0.9, beta_2=0.999, decay=0.01)
    opt = SGD(lr=0.01)

    model.compile(loss='binary_crossentropy', optimizer=opt, metrics=['accuracy'])
    return model

尽管我仍然建议查看您的优化器,因为这可能会影响您的结果。您还可以使用 -1 作为输入形状,这意味着 auto fill,但您只能使用一次。