keras 我的模型的损失值下降缓慢,如何在训练时更快地减少我的损失?

wqsoz72f  于 12个月前  发布在  其他
关注(0)|答案(1)|浏览(120)

当我训练模型时,损失在2500个epoch中从0.9减少到0.5,这正常吗?
我的模型:

model = Sequential()

    model.add(Embedding(vocab_size , emd_dim, weights=[emd_matrix], input_length=maxLen,trainable=False))

    model.add(LSTM(256,return_sequences=True,activation="relu",kernel_regularizer=regularizers.l2(0.01),kernel_initializer=keras.initializers.glorot_normal(seed=None)))
    model.add(LSTM(256,return_sequences=True,activation="relu",kernel_regularizer=regularizers.l2(0.01),kernel_initializer=keras.initializers.glorot_normal(seed=None)))

    model.add(LSTM(256,return_sequences=False,activation="relu",kernel_regularizer=regularizers.l2(0.01),kernel_initializer=keras.initializers.glorot_normal(seed=None)))
    model.add(Dense(l_h2i,activation='softmax'))
    model.compile(loss='categorical_crossentropy', optimizer="adam", metrics=['accuracy'])
    filepath = "F:/checkpoints/"+modelname+"/lstm-{epoch:02d}-{loss:0.3f}-{acc:0.3f}-{val_loss:0.3f}-{val_acc:0.3f}.hdf5"
    checkpoint = ModelCheckpoint(filepath, monitor="loss", verbose=1, save_best_only=True, mode='min')
    reduce_lr = ReduceLROnPlateau(monitor='loss', factor=0.5, patience=2, min_lr=0.000001)
    print(model.summary())
    history=model.fit(X_train_indices, Y_train_oh, batch_size=batch_size ,
                      epochs=epochs , validation_split=0.1, shuffle=True,
                      callbacks=[checkpoint, reduce_lr])

字符串
部分结果如下所示:

loss improved from 0.54275 to 0.54272
loss: 0.5427 - acc: 0.8524 - val_loss: 1.1198 - val_acc: 0.7610

loss improved from 0.54272 to 0.54268
loss: 0.5427 - acc: 0.8525 - val_loss: 1.1195 - val_acc: 0.7311

loss improved from 0.54268 to 0.54251
loss: 0.5425 - acc: 0.8519 - val_loss: 1.1218 - val_acc: 0.7420

loss improved from 0.54251 to 0.54249
loss: 0.5425 - acc: 0.8517 - val_loss: 1.1210 - val_acc: 0.7518

e4eetjau

e4eetjau1#

考虑像TensorFlow documentation中那样更新ReduceLROnPlateau参数。因子应该更大,耐心应该更小

reduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.2,
                              patience=5, min_lr=0.001)
model.fit(X_train, Y_train, callbacks=[reduce_lr])

字符串

参数:

  • monitor:要监控的数量。
    *factor:学习率降低的因子。new_lr = lr * factor
    *patience:没有改善的epoch数量,之后学习率将降低。
  • verbose:int. 0:quiet,1:update messages.
  • 模式:{auto,min,max}中的一个。在min模式下,当监测的量停止减少时,lr将减小;在max模式下,当监测的量停止增加时,lr将减小;在auto模式下,方向从监测的量的名称自动推断。
  • min_delta:用于测量新的最佳值的阈值,仅关注显著的变化。
  • 冷却时间:lr降低后恢复正常操作所需的时间。
  • min_lr:学习率的下限。

相关问题