-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheckpoint.py
More file actions
73 lines (61 loc) · 2.08 KB
/
Copy pathcheckpoint.py
File metadata and controls
73 lines (61 loc) · 2.08 KB
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
67
68
69
70
71
72
73
import os
import torch
from tensorboardX import SummaryWriter
use_cuda = torch.cuda.is_available()
default_checkpoint = {
"epoch": 0,
"train_losses": [],
"train_accuracy": [],
"validation_losses": [],
"validation_accuracy": [],
"lr": [],
"grad_norm": [],
"model": {},
}
def save_checkpoint(checkpoint, dir="./checkpoints", prefix=""):
# Padded to 4 digits because of lexical sorting of numbers.
# e.g. 0009.pth
filename = "{prefix}{num:0>4}.pth".format(num=checkpoint["epoch"], prefix=prefix)
if not os.path.exists(dir):
os.mkdir(dir)
torch.save(checkpoint, os.path.join(dir, filename))
def load_checkpoint(path, cuda=use_cuda):
if cuda:
return torch.load(path)
else:
# Load GPU model on CPU
return torch.load(path, map_location=lambda storage, loc: storage)
def init_tensorboard(name="", base_dir="./tensorboard"):
return SummaryWriter(os.path.join(base_dir, name))
def write_tensorboard(
writer,
epoch,
grad_norm,
train_loss,
train_accuracy,
validation_loss,
validation_accuracy,
encoder,
decoder,
):
writer.add_scalar("train_loss", train_loss, epoch)
writer.add_scalar("train_accuracy", train_accuracy, epoch)
writer.add_scalar("validation_loss", validation_loss, epoch)
writer.add_scalar("validation_accuracy", validation_accuracy, epoch)
writer.add_scalar("grad_norm", grad_norm, epoch)
for name, param in encoder.named_parameters():
writer.add_histogram(
"encoder/{}".format(name), param.detach().cpu().numpy(), epoch
)
if param.grad is not None:
writer.add_histogram(
"encoder/{}/grad".format(name), param.grad.detach().cpu().numpy(), epoch
)
for name, param in decoder.named_parameters():
writer.add_histogram(
"decoder/{}".format(name), param.detach().cpu().numpy(), epoch
)
if param.grad is not None:
writer.add_histogram(
"decoder/{}/grad".format(name), param.grad.detach().cpu().numpy(), epoch
)