-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
55 lines (36 loc) · 1.21 KB
/
utils.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
import os
import torch
def save_model(model, name):
checkpoint_path = f"{get_checkpoints_dir()}/checkpoint_{name}.pt"
torch.save(model.state_dict(), checkpoint_path)
def load_model(model, name):
checkpoint_path = f"{get_checkpoints_dir()}/checkpoint_{name}.pt"
checkpoint = torch.load(checkpoint_path)
model.load_state_dict(checkpoint)
def get_checkpoints_dir():
dir = os.path.join(os.path.dirname(__file__), "checkpoints")
if not os.path.exists(dir):
os.makedirs(dir)
return dir
def save_data(data, name):
torch.save(data, get_data_path(name))
def load_data(name):
return torch.load(get_data_path(name))
def get_data_path(name):
data_path = f"{get_data_dir()}/data_{name}.pt"
return data_path
def get_data_dir():
dir = os.path.join(os.path.dirname(__file__), "data")
if not os.path.exists(dir):
os.makedirs(dir)
return dir
class CustomLRScheduler:
def __init__(self, optimizer, my_rl):
self.optimizer = optimizer
self.my_rl = my_rl
self.step()
def set_rl(self, my_rl):
self.my_rl = my_rl
def step(self):
for param_group in self.optimizer.param_groups:
param_group["lr"] = self.my_rl