|
| 1 | +# How to log to TensorBoard |
| 2 | + |
| 3 | +TensorBoard is a format and viewer for logs of model training and can be used to inspect and compare the results of training runs. We can log step and epoch metrics, hyperparameters and even visualizations to TensorBoard using FluxTraining.jl's logging callbacks. |
| 4 | + |
| 5 | +## Generating logs |
| 6 | + |
| 7 | +To use logging callbacks, we need to pass a log backend, here [`TensorBoardBackend`](#). This design allows flexibly supporting other logging backends like Weights and Biases or neptune.ai in the future. |
| 8 | + |
| 9 | +{cell=main} |
| 10 | +```julia |
| 11 | +using FastAI |
| 12 | + |
| 13 | +dir = mktempdir() |
| 14 | +backend = TensorBoardBackend(dir) |
| 15 | +``` |
| 16 | + |
| 17 | +Then we create callbacks for logging metrics and hyperparameters to that backend: |
| 18 | + |
| 19 | +{cell=main} |
| 20 | +```julia |
| 21 | +metricscb = LogMetrics(backend) |
| 22 | +hparamscb = LogHyperParams(backend) |
| 23 | +``` |
| 24 | + |
| 25 | +Like any other callbacks, these can then be passed to a `Learner` along with other callbacks and we can start training. By using the [`Metrics`](#) callback we can log metrics other than the loss. |
| 26 | + |
| 27 | +```julia |
| 28 | + |
| 29 | +callbacks = [ |
| 30 | + metricscb, |
| 31 | + hparamscb, |
| 32 | + ToGPU(), |
| 33 | + Metrics(accuracy) |
| 34 | +] |
| 35 | + |
| 36 | +data = Datasets.loadtaskdata(Datasets.datasetpath("imagenette2-160"), ImageClassificationTask) |
| 37 | +method = ImageClassification(Datasets.getclassesclassification("imagenette2-160"), (160, 160)) |
| 38 | +learner = methodlearner(method, data, Models.xresnet18(), callbacks...) |
| 39 | +fitonecycle!(learner, 5) |
| 40 | +``` |
| 41 | + |
| 42 | +## Inspecting logs |
| 43 | + |
| 44 | +To inspect the logs you will have to install the `tensorboard` command-line using `pip` (you can access the command-line in the Julia REPL by pressing `;`). |
| 45 | + |
| 46 | +``` |
| 47 | +shell> pip install tensorboard |
| 48 | +``` |
| 49 | + |
| 50 | +After this one-time installation, you can run it by pointing it to the log directory created above: |
| 51 | + |
| 52 | +``` |
| 53 | +shell> tensorboard --logdir $dir |
| 54 | +``` |
| 55 | + |
| 56 | +This should give you an URL that you can open in a browser which should look like this: |
| 57 | + |
| 58 | + |
| 59 | + |
| 60 | +Note that you can also open TensorBoard and it will update as the training progresses. |
0 commit comments