源代码
TerminateOnNaNCallback
TerminateOnNaNCallback (after_create=None, before_fit=None,
before_epoch=None, before_train=None,
before_batch=None, after_pred=None,
after_loss=None, before_backward=None,
after_cancel_backward=None, after_backward=None,
before_step=None, after_cancel_step=None,
after_step=None, after_cancel_batch=None,
after_batch=None, after_cancel_train=None,
after_train=None, before_validate=None,
after_cancel_validate=None, after_validate=None,
after_cancel_epoch=None, after_epoch=None,
after_cancel_fit=None, after_fit=None)
一个如果损失为 NaN 则终止训练的 Callback。
learn = synth_learner()
learn.fit(10, lr=100, cbs=TerminateOnNaNCallback())
assert len(learn.recorder.losses) < 10 * len(learn.dls.train)
for l in learn.recorder.losses:
assert not torch.isinf(l) and not torch.isnan(l)
源代码
TrackerCallback
TrackerCallback (monitor='valid_loss', comp=None, min_delta=0.0,
reset_on_fit=True)
一个用于跟踪 monitor 中最佳值的 Callback。
| monitor |
str |
验证损失 |
被监控的值(通常是损失或指标)。 |
| comp |
NoneType |
None |
numpy 比较运算符;如果 monitor 是损失,则为 np.less;如果 monitor 是指标,则为 np.greater。 |
| min_delta |
float |
0.0 |
上次 monitor 值与最佳 monitor 值之间的最小差值。 |
| reset_on_fit |
bool |
True |
在模型拟合之前,将被监控值重置为负无穷(如果 monitor 是指标)或正无穷(如果 monitor 是损失)。 |
当实现一个行为取决于指标或损失最佳值的 Callback 时,继承此类 Callback 并使用其 best(目前最佳值)和 new_best(本周期有新的最佳值)属性。如果您想在随后的 fit 调用中(例如 Learner.fit_one_cycle)保持 best,请将 reset_on_fit 设置为 True。
comp 是用于确定一个值是否比另一个值更好的比较运算符(如果 monitor 参数名称中包含 'loss',则默认为 np.less,否则为 np.greater);min_delta 是一个可选的浮点数,要求新值比当前最佳值(取决于 comp)至少改进该量。
源代码
EarlyStoppingCallback
EarlyStoppingCallback (monitor='valid_loss', comp=None, min_delta=0.0,
patience=1, reset_on_fit=True)
一个当被监控指标停止改善时终止训练的 TrackerCallback。
| monitor |
str |
验证损失 |
被监控的值(通常是损失或指标)。 |
| comp |
NoneType |
None |
numpy 比较运算符;如果 monitor 是损失,则为 np.less;如果 monitor 是指标,则为 np.greater。 |
| min_delta |
float |
0.0 |
上次 monitor 值与最佳 monitor 值之间的最小差值。 |
| patience |
int |
1 |
训练未改进模型时等待的周期数。 |
| reset_on_fit |
bool |
True |
在模型拟合之前,将被监控值重置为负无穷(如果 monitor 是指标)或正无穷(如果 monitor 是损失)。 |
comp 是用于确定一个值是否比另一个值更好的比较运算符(如果 monitor 参数名称中包含 'loss',则默认为 np.less,否则为 np.greater);min_delta 是一个可选的浮点数,要求新值比当前最佳值(取决于 comp)至少改进该量。patience 是您愿意在没有改善的情况下等待的周期数。
learn = synth_learner(n_trn=2, metrics=F.mse_loss)
learn.fit(n_epoch=200, lr=1e-7, cbs=EarlyStoppingCallback(monitor='mse_loss', min_delta=0.1, patience=2))
| 0 |
20.437918 |
26.406773 |
26.406773 |
00:00 |
| 1 |
20.418514 |
26.406715 |
26.406715 |
00:00 |
| 2 |
20.410892 |
26.406639 |
26.406639 |
00:00 |
No improvement since epoch 0: early stopping
(#2) [26.406639099121094,26.406639099121094]
learn = synth_learner(n_trn=2)
learn.fit(n_epoch=200, lr=1e-7, cbs=EarlyStoppingCallback(monitor='valid_loss', min_delta=0.1, patience=2))
| 0 |
13.408870 |
19.617222 |
00:00 |
| 1 |
13.403553 |
19.617184 |
00:00 |
| 2 |
13.403143 |
19.617126 |
00:00 |
No improvement since epoch 0: early stopping
源代码
SaveModelCallback
SaveModelCallback (monitor='valid_loss', comp=None, min_delta=0.0,
fname='model', every_epoch=False, at_end=False,
with_opt=False, reset_on_fit=True)
一个在训练期间保存模型最佳状态并在结束时加载它的 TrackerCallback。
| monitor |
str |
验证损失 |
被监控的值(通常是损失或指标)。 |
| comp |
NoneType |
None |
numpy 比较运算符;如果 monitor 是损失,则为 np.less;如果 monitor 是指标,则为 np.greater。 |
| min_delta |
float |
0.0 |
上次 monitor 值与最佳 monitor 值之间的最小差值。 |
| fname |
str |
模型 |
保存模型时使用的模型名称。 |
| every_epoch |
bool |
False |
如果为 true,则在每个周期后保存模型;否则仅在模型优于现有最佳模型时保存。 |
| at_end |
bool |
False |
如果为 true,则在训练结束时保存模型;否则如果只保存了一个模型,则加载最佳模型。 |
| with_opt |
bool |
False |
如果为 true,则在保存模型时保存优化器状态(如果可用)。 |
| reset_on_fit |
bool |
True |
在模型拟合之前,将被监控值重置为负无穷(如果 monitor 是指标)或正无穷(如果 monitor 是损失)。 |
comp 是用于确定一个值是否比另一个值更好的比较运算符(如果 monitor 参数名称中包含 'loss',则默认为 np.less,否则为 np.greater);min_delta 是一个可选的浮点数,要求新值比当前最佳值(取决于 comp)至少改进该量。模型将保存在 learn.path/learn.model_dir/name.pth 中,如果 every_epoch 为 True 则每个周期保存一次,如果向 every_epoch 传递整数则每 n 个周期保存一次,或者在被监控指标每次改进时保存。
learn = synth_learner(n_trn=2, path=Path.cwd()/'tmp')
learn.fit(n_epoch=2, cbs=SaveModelCallback())
assert (Path.cwd()/'tmp/models/model.pth').exists()
learn = synth_learner(n_trn=2, path=Path.cwd()/'tmp')
learn.fit(n_epoch=2, cbs=SaveModelCallback(fname='end',at_end=True))
assert (Path.cwd()/'tmp/models/end.pth').exists()
learn.fit(n_epoch=2, cbs=SaveModelCallback(every_epoch=True))
for i in range(2): assert (Path.cwd()/f'tmp/models/model_{i}.pth').exists()
shutil.rmtree(Path.cwd()/'tmp')
learn.fit(n_epoch=4, cbs=SaveModelCallback(every_epoch=2))
for i in range(4):
if not i%2: assert (Path.cwd()/f'tmp/models/model_{i}.pth').exists()
else: assert not (Path.cwd()/f'tmp/models/model_{i}.pth').exists()
shutil.rmtree(Path.cwd()/'tmp')
| 0 |
19.453270 |
12.539286 |
00:00 |
| 1 |
19.248507 |
12.123456 |
00:00 |
Better model found at epoch 0 with valid_loss value: 12.539285659790039.
Better model found at epoch 1 with valid_loss value: 12.123456001281738.
| 0 |
5.197007 |
5.579152 |
00:00 |
| 1 |
5.154862 |
5.445522 |
00:00 |
Better model found at epoch 0 with valid_loss value: 5.5791521072387695.
Better model found at epoch 1 with valid_loss value: 5.445522308349609.
| 0 |
4.982775 |
5.264440 |
00:00 |
| 1 |
4.887252 |
5.038480 |
00:00 |
| 0 |
4.578584 |
4.781651 |
00:00 |
| 1 |
4.454868 |
4.507101 |
00:00 |
| 2 |
4.322047 |
4.232390 |
00:00 |
| 3 |
4.186467 |
3.957614 |
00:00 |
ReduceLROnPlateau
源代码
ReduceLROnPlateau
ReduceLROnPlateau (monitor='valid_loss', comp=None, min_delta=0.0,
patience=1, factor=10.0, min_lr=0, reset_on_fit=True)
一个当指标停止改善时降低学习率的 TrackerCallback。
| monitor |
str |
验证损失 |
被监控的值(通常是损失或指标)。 |
| comp |
NoneType |
None |
numpy 比较运算符;如果 monitor 是损失,则为 np.less;如果 monitor 是指标,则为 np.greater。 |
| min_delta |
float |
0.0 |
上次 monitor 值与最佳 monitor 值之间的最小差值。 |
| patience |
int |
1 |
训练未改进模型时等待的周期数。 |
| factor |
float |
10.0 |
降低学习率时,用作学习率除数的因子。 |
| min_lr |
int |
0 |
允许的最小学习率;学习率不能低于此最小值。 |
| reset_on_fit |
bool |
True |
在模型拟合之前,将被监控值重置为负无穷(如果 monitor 是指标)或正无穷(如果 monitor 是损失)。 |
learn = synth_learner(n_trn=2)
learn.fit(n_epoch=4, lr=1e-7, cbs=ReduceLROnPlateau(monitor='valid_loss', min_delta=0.1, patience=2))
| 0 |
6.122743 |
7.348515 |
00:00 |
| 1 |
6.119377 |
7.348499 |
00:00 |
| 2 |
6.125790 |
7.348477 |
00:00 |
| 3 |
6.131386 |
7.348475 |
00:00 |
Epoch 2: reducing lr to 1e-08
learn = synth_learner(n_trn=2)
learn.fit(n_epoch=6, lr=5e-8, cbs=ReduceLROnPlateau(monitor='valid_loss', min_delta=0.1, patience=2, min_lr=1e-8))
| 0 |
16.747515 |
15.265999 |
00:00 |
| 1 |
16.725756 |
15.265974 |
00:00 |
| 2 |
16.735016 |
15.265943 |
00:00 |
| 3 |
16.733360 |
15.265934 |
00:00 |
| 4 |
16.733513 |
15.265925 |
00:00 |
| 5 |
16.730352 |
15.265915 |
00:00 |
Epoch 2: reducing lr to 1e-08
这三个派生的 TrackerCallback(SaveModelCallback、ReduceLROnPlateu 和 EarlyStoppingCallback)都经过调整的顺序,以便它们可以相互运行而不会互相干扰。该顺序如下
TrackerCallback (60)
SaveModelCallback (61)
ReduceLrOnPlateu (62)
EarlyStoppingCallback (63)