-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathcallbacks.py
33 lines (25 loc) · 1.05 KB
/
callbacks.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
from fastai.conv_learner import *
class LogResults(Callback):
"""
Callback to log all the results of the training:
- at the end of each epoch: training loss, validation loss and metrics
"""
def __init__(self, learn, fname):
super().__init__()
self.learn, self.fname = learn, fname
def on_train_begin(self):
self.logs, self.epoch, self.n = "", 0, 0
names = ["epoch", "trn_loss", "val_loss", "accuracy"]
layout = "{!s:10} " * len(names)
self.logs += layout.format(*names) + "\n"
def on_batch_end(self, metrics):
self.loss = metrics
def on_epoch_end(self, metrics):
self.save_stats(self.epoch, [self.loss] + metrics)
self.epoch += 1
def save_stats(self, epoch, values, decimals=6):
layout = "{!s:^10}" + " {!s:10}" * len(values)
values = [epoch] + list(np.round(values, decimals))
self.logs += layout.format(*values) + "\n"
def on_train_end(self):
with open(self.fname, 'a') as f: f.write(self.logs)