快速回调

Callbacks in Fastai

我正在 Fastai 进行深度学习项目,希望在其中使用 EarlyStoppingCallback 和 ReduceLROnPlateauCallback。阅读 callbacks.fastai 但难以理解如何实现这两者并且找不到任何相关示例。任何帮助将不胜感激。

learn = cnn_learner(data, models.resnet50, metrics = [accuracy,quadratic_kappa])
learn.fit(50,2e-6)

我平时的做法是这样的....

首先创建learner对象

    learn = Learner(data, 
                    model, 
                    loss_func=...., 
                    opt_func=...., 
                    metrics=..... )

    learn.unfreeze()

然后你在 fit_one_cycle

上调用任何回调
    learn.fit_one_cycle(16, 
                        max_lr=1e-3, 
                        div_factor=100,
                        pct_start=0.0, 
                        callbacks = [SaveModelCallback(learn,
                                     name=f'model',monitor='kappa_score')])

也许你也可以这样做-:

from fastai.callbacks import *
from fastai.callback import Callback
from fastai.vision import partial

learner = cnn_learner(data, models.resnet50, metrics=[accuracy], pretrained=True, callback_fns=[partial(EarlyStoppingCallback, monitor='valid_loss', min_delta=0.01, patience=3)])

# or 

learner = cnn_learner(data, models.resnet101, metrics=[accuracy], pretrained=True, callbacks=EarlyStoppingCallback(monitor='valid_loss', min_delta=0.01, patience=3))


learner.fit(150)

# it will stop if "valid_loss" will not decrease (patience=3)