[파이썬] fastai Callbacks 시스템 이해

In the fastai library, callbacks are an essential part of the training process. They allow you to customize and extend the behavior of the training loop by injecting your own logic at specific points during training. Understanding how callbacks work in fastai can greatly enhance your ability to fine-tune and optimize your models.

What are Callbacks?

Callbacks in fastai are functions or objects that can be called at certain events during training, such as before or after each batch, epoch, or the entire training process. They provide hooks for you to add custom functionality that can modify the training behavior, monitor and adjust the learning process, or record intermediate results for further analysis.

How do Callbacks work in fastai?

The fastai Callbacks system is built around the Callback class. Each callback is an instance of this class and defines one or more methods that correspond to specific events during training. By implementing these methods, you can specify what action to take before or after those events occur.

For example, let’s say you want to print the loss and accuracy of your model after each epoch. You can create a callback class that defines a after_epoch method, which gets called at the end of each epoch.

from fastai.callback.core import Callback

class PrintMetricsCallback(Callback):
    def after_epoch(self):
        print(f"Loss: {self.learn.loss:.4f}, Accuracy: {self.learn.recorder.metrics[0]:.4f}")

In this example, the after_epoch method accesses the learn object, which provides access to the necessary information like loss and metrics. It then prints the loss and accuracy to the console.

To use this callback, you need to pass it to the Learner object using the cbs parameter:

learn = cnn_learner(dls, resnet34, cbs=PrintMetricsCallback())

Now, after each epoch, the PrintMetricsCallback will be called, and the loss and accuracy will be printed.

Predefined Callbacks in fastai

fastai provides several predefined callbacks that cover a wide range of functionalities. Some of the commonly used callbacks include:

You can find the complete list of predefined callbacks in the fastai documentation.

Creating Custom Callbacks

In addition to using predefined callbacks, you can create your own custom callbacks to add specific functionality to your training process.

To create a custom callback, subclass the Callback class and implement the necessary methods based on the events you want to hook into. Some of the commonly used methods include:

You can also modify the behavior of the training loop by modifying attributes of the self.learn object within your custom callback methods.

Conclusion

Fastai callbacks provide a powerful mechanism to customize and extend the training process. Whether you want to monitor metrics, apply custom logic, or modify the training loop, callbacks allow you to do so with ease. By understanding the callbacks system in fastai, you can unlock the full potential of the fastai library and effectively train and fine-tune your models.