【问题标题】:Validation accuracy fluctuating while training accuracy increase?验证准确度在训练准确度增加时波动?
【发布时间】:2020-03-29 06:57:22
【问题描述】:

我有一个依赖于历史数据的多分类问题。我正在尝试使用 loss='sparse_categorical_crossentropy' 的 LSTM。训练准确率和损失分别增加和减少。但是,我的测试准确度开始大幅波动。

我做错了什么?

输入数据:

X = np.reshape(X, (X.shape[0], X.shape[1], 1))
X.shape
(200146, 13, 1)

我的模型

# fix random seed for reproducibility
seed = 7
np.random.seed(seed)

# define 10-fold cross validation test harness
kfold = StratifiedKFold(n_splits=10, shuffle=False, random_state=seed)
cvscores = []
for train, test in kfold.split(X, y):
    regressor = Sequential()

    # Units = the number of LSTM that we want to have in this first layer -> we want very high dimentionality, we need high number
    # return_sequences =  True because we are adding another layer after this
    # input shape = the last two dimensions and the indicator
    regressor.add(LSTM(units=50, return_sequences=True, input_shape=(X[train].shape[1], 1)))
    regressor.add(Dropout(0.2))

    # Extra LSTM layer
    regressor.add(LSTM(units=50, return_sequences=True))
    regressor.add(Dropout(0.2))
    # 3rd
    regressor.add(LSTM(units=50, return_sequences=True))
    regressor.add(Dropout(0.2))

    #4th
    regressor.add(LSTM(units=50))
    regressor.add(Dropout(0.2))

    # output layer
    regressor.add(Dense(4, activation='softmax', kernel_regularizer=regularizers.l2(0.001)))

    # Compile the RNN
    regressor.compile(optimizer='adam', loss='sparse_categorical_crossentropy',metrics=['accuracy'])

    # Set callback functions to early stop training and save the best model so far
    callbacks = [EarlyStopping(monitor='val_loss', patience=9),
             ModelCheckpoint(filepath='best_model.h5', monitor='val_loss', save_best_only=True)]


    history = regressor.fit(X[train], y[train], epochs=250, callbacks=callbacks, 
                        validation_data=(X[test], y[test]))

    # plot train and validation loss
    pyplot.plot(history.history['loss'])
    pyplot.plot(history.history['val_loss'])
    pyplot.title('model train vs validation loss')
    pyplot.ylabel('loss')
    pyplot.xlabel('epoch')
    pyplot.legend(['train', 'validation'], loc='upper right')
    pyplot.show()


    # evaluate the model
    scores = regressor.evaluate(X[test], y[test], verbose=0)
    print("%s: %.2f%%" % (regressor.metrics_names[1], scores[1]*100))
    cvscores.append(scores[1] * 100)
print("%.2f%% (+/- %.2f%%)" % (np.mean(cvscores), np.std(cvscores)))

结果:

trainingmodel

Plot

【问题讨论】:

    标签: machine-learning keras deep-learning lstm


    【解决方案1】:

    你在这里描述的是过度拟合。这意味着您的模型会不断学习您的训练数据并且不会泛化,或者说它正在学习您的训练集的确切特征。这是您在深度学习中可以处理的主要问题。本身没有解决方案。你必须尝试不同的架构、不同的超参数等等。

    您可以尝试使用欠拟合的小模型(即训练 acc 和验证的百分比很低)并不断增加您的模型,直到它过拟合。然后,您可以使用优化器和其他超参数。

    我所说的更小的模型是指具有更少隐藏单元或更少层的模型。

    【讨论】:

    • 感谢您的回答!对于要预测的变量取决于过去事件的多分类问题,您会尝试采用不同的方法吗?
    【解决方案2】:

    你似乎有太多的 LSTM 层一次又一次地堆叠,最终导致过度拟合。可能应该减少层数。

    【讨论】:

      【解决方案3】:

      您的模型似乎过度拟合,因为训练错误不断减少,而验证错误却失败了。总的来说,它无法概括。

      您应该尝试通过删除一些 LSTM 层来降低模型复杂性。此外,尝试改变批量大小,这将减少损失波动的数量。 您还可以考虑改变学习率。

      【讨论】:

        猜你喜欢
        • 2021-07-22
        • 2020-07-08
        • 2021-12-17
        • 2017-12-21
        • 2020-10-16
        • 2018-07-28
        • 2022-01-15
        • 2020-11-02
        • 1970-01-01
        相关资源
        最近更新 更多