在 运行 用于测试分类的深度学习模型中增加 val-loss 并降低 val-accuracy

getting increase in val-loss and decrease in val-accuracy while running a deep learning model for test classification

我正在尝试对标签为 0,1 的文本进行分类,并使用 Bi-lstm 进行分类。它让我在训练时间上有一点不错的准确性,但在验证方面,损失会增加,验证准确性往往会下降。请给我一些解决方案,我可以如何改进它。 数据形状:(1043708, 2)

这是我的模型

model=tf.keras.Sequential([
    # add an embedding layer
    tf.keras.layers.Embedding(word_count, 16, input_length=max_len),
     # add dropout layer to prevent overfitting
    tf.keras.layers.Dropout(0.2),
    # add the bi-lstm layer
    tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(64,return_sequences=True)),
    # add a dense layer
    tf.keras.layers.Dense(32, activation=tf.keras.activations.relu),
    tf.keras.layers.Dense(32, activation=tf.keras.activations.relu),
    tf.keras.layers.Dense(32, activation=tf.keras.activations.softmax),
    # add the prediction layer
    tf.keras.layers.Dense(1, activation=tf.keras.activations.sigmoid),
])

model.compile(loss=tf.keras.losses.BinaryCrossentropy(), optimizer=tf.keras.optimizers.Adam(), metrics=['accuracy'])

model.summary()
history = model.fit(XPAD_train, Y_train, validation_data=(XPAD_test, Y_test), epochs = 10, batch_size=batch_size, callbacks = [callback_func], verbose=1)

如你评论所说cross-validation。您可以像下面这样使用 sklearn.model_selection.KFold 并在每个 X_train、y_train、X_test、y_test 上训练您的模型,如下所示:

from sklearn.model_selection import KFold
import numpy as np
X = np.array(["I'm good.", "I'm very good", "I'm bad", "I'm very bad"])
y = np.array(["pos", "pos", "neg", "neg"])
k_fold = KFold(n_splits=4)
for train_idx, test_idx in k_fold.split(X):
    print(f'Train_idx: {train_idx} | Test_idx: {test_idx}')
    print(f'X_train : {X[train_idx]}')
    print(f'y_train : {y[train_idx]}')
    print(f'X_test  : {X[test_idx]}')
    print(f'y_test  : {y[test_idx]}')
    print()

输出:

Train_idx: [1 2 3] | Test_idx: [0]
X_train : ["I'm very good" "I'm bad" "I'm very bad"]
y_train : ['pos' 'neg' 'neg']
X_test  : ["I'm good."]
y_test  : ['pos']

Train_idx: [0 2 3] | Test_idx: [1]
X_train : ["I'm good." "I'm bad" "I'm very bad"]
y_train : ['pos' 'neg' 'neg']
X_test  : ["I'm very good"]
y_test  : ['pos']

Train_idx: [0 1 3] | Test_idx: [2]
X_train : ["I'm good." "I'm very good" "I'm very bad"]
y_train : ['pos' 'pos' 'neg']
X_test  : ["I'm bad"]
y_test  : ['neg']

Train_idx: [0 1 2] | Test_idx: [3]
X_train : ["I'm good." "I'm very good" "I'm bad"]
y_train : ['pos' 'pos' 'neg']
X_test  : ["I'm very bad"]
y_test  : ['neg']