-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcallbacks.py
66 lines (53 loc) · 1.86 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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
""" Callback classes for versatile behavior in the Trainer object at specified checkpoints.
This program uses the NeuroMANCER framework <https://github.com/pnnl/neuromancer> which comes with BSD license.
It's modified to save data every 100 epochs.
"""
import os
from copy import deepcopy
import pickle
import torch
import dill
class Callback:
"""
Callback base class which allows for bare functionality of Trainer
"""
def __init__(self):
pass
def begin_train(self, trainer):
pass
def begin_epoch(self, trainer, output):
pass
def begin_eval(self, trainer, output):
pass
def end_batch(self, trainer, output):
pass
def end_eval(self, trainer, output):
pass
def end_epoch(self, trainer, output):
print("epoch: "+str(trainer.current_epoch), flush=True)
savedir = trainer.logger.savedir
savepath = os.path.join(savedir, 'loss.dat')
loss = ('y_loss', 'onestep_loss', 'x_loss', 'reconstruct_loss', 'train_loss', 'dev_loss')
f = open(savepath, 'ab')
entries = []
for k, v in output.items():
try:
if k in loss:
entries.append(v.item())
except (ValueError, AttributeError) as e:
pass
pickle.dump(entries , f)
f.close()
if (trainer.current_epoch % 100 == 0):
savepath = os.path.join(savedir, 'best_model_epoch.pth')
torch.save(trainer.best_model, savepath, pickle_module=dill)
savepath = os.path.join(savedir, 'model_epoch.pth')
torch.save(trainer.model, savepath, pickle_module=dill)
return True
def end_train(self, trainer, output):
print("end of training")
return True
def begin_test(self, trainer):
pass
def end_test(self, trainer, output):
pass