gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 19:24:09 24 4
gpt4 key购买 nike

当我训练模型时,损失在 2500 个时期内从 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

最佳答案

考虑更新 ReduceLROnPlateau 参数,如 TensorFlow documentation .因子应该更大,耐心应该是更小的数字

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: quantity to be monitored.
  • factor: factor by which the learning rate will be reduced. new_lr = lr * factor
  • patience: number of epochs with no improvement after which learning rate will be reduced.
  • verbose: int. 0: quiet, 1: update messages.
  • mode: one of {auto, min, max}. In min mode, lr will be reduced when the quantity monitored has stopped decreasing; in max mode it will be reduced when the quantity monitored has stopped increasing; in auto mode, the direction is automatically inferred from the name of the monitored quantity.
  • min_delta: threshold for measuring the new optimum, to only focus on significant changes.
  • cooldown: number of epochs to wait before resuming normal operation after lr has been reduced.
  • min_lr: lower bound on the learning rate.

关于tensorflow - 我的模型的损失值缓慢下降。如何在训练时更快地减少我的损失?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58114417/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com