-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
54 lines (41 loc) · 1.74 KB
/
utils.py
File metadata and controls
54 lines (41 loc) · 1.74 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
import torch
import torch.nn as nn
from config import CHECKPOINT_DIR
import os
def weights_init_unif(module, a, b):
"""
Initialize all weights in module to values within [a, b].
Args:
module (nn.Module): Target network
a (float): Lower bound
b (float): Upper bound
"""
for p in module.parameters():
nn.init.uniform_(p.data, a=a, b=b)
def load_from_checkpoint(model, optimizer, activation, lr, epoch, device, dataset):
# To start from epoch e, we need to load in a pretrained model up to epoch e-1
checkpoint_name = f"{dataset}-activation-{activation}_LR-{lr}_epoch-{epoch-1}.pt"
path = None
for root, dirs, files in os.walk(CHECKPOINT_DIR):
for filename in files:
if filename == checkpoint_name:
path = os.path.join(root, filename)
if not path:
raise FileNotFoundError("Desired checkpoint does not exist")
else:
checkpoint = torch.load(path, map_location=device)
model.load_state_dict(checkpoint['model_state_dict'])
optimizer.load_state_dict(checkpoint['optimizer_state_dict'])
epoch = checkpoint['epoch']
return model, optimizer
def save_checkpoint(model, optimizer, epoch, activation, lr, dataset):
dirname = f"{dataset}-activation-{activation}_LR-{lr}"
filename = f"{dataset}-activation-{activation}_LR-{lr}_epoch-{epoch}.pt"
checkpoint_dir = os.path.join(CHECKPOINT_DIR, dirname)
save_path = os.path.join(checkpoint_dir, filename)
if not os.path.exists(checkpoint_dir):
os.makedirs(checkpoint_dir)
torch.save({'epoch': epoch,
'model_state_dict': model.state_dict(),
'optimizer_state_dict': optimizer.state_dict()
}, save_path)