Tensorflow LSTM:损失值没有改变

Tensorflow LSTM:损失值没有改变,tensorflow,keras,lstm,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

我正在努力预测股票走势(上涨或下跌)

下面是我如何处理我的预处理

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()
以下是损耗_值的输出:

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

我已经尝试更改Optimizer、loss_函数和其他修改。正如您所料,由于损失函数没有改变,所有预测都是相同的。

您的第一个LSTM层中的输入形状有问题。Keras输入将(无,您的_形状)作为其输入,因为您对模型的输入可能会有所不同。您可以有1个输入、2个输入或无穷多个输入。表示动态的唯一方法是使用
None
作为第一个输入。最快的方法是将输入更改为
(无,*input\u 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
作为输入形状,这意味着
自动填充
,但您只能使用一次。

您的问题是什么?你还没问呢。@gobrewers14确实在标题里。