-
Notifications
You must be signed in to change notification settings - Fork 0
/
trainer_clean_spo.py
179 lines (146 loc) · 6.38 KB
/
trainer_clean_spo.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
"""
clean train nn with spo loss
"""
from utils.dataset import MyDataset
from torch.utils.data import DataLoader
from utils.net import NN_SPO
import torch
import numpy as np
import random
import os
from tqdm import tqdm
class Trainer_SPO:
def __init__(self, net, optimizer, train_loader, test_loader, operator, clip_norm):
"""
b_default: default susceptance for the power grid
"""
self.net = net
assert self.net.name == 'NN_SPO'
self.optimizer = optimizer
self.trainloader = train_loader
self.testloader = test_loader
self.first_coeff = torch.tensor(operator.first_coeff, dtype=torch.float)
self.load_shed_coeff = torch.tensor(operator.load_shed_coeff, dtype = torch.float)
self.gen_storage_coeff = torch.tensor(operator.gen_storage_coeff, dtype = torch.float)
self.b_default = torch.from_numpy(operator.b).float()
self.clip_norm = clip_norm
def loss(self, pg, ls, gs):
loss = pg @ self.first_coeff + ls @ self.load_shed_coeff + gs @ self.gen_storage_coeff
return loss
def train(self):
self.net.train()
loss_sum = 0.
for feature, target in tqdm(self.trainloader, total = len(self.trainloader)):
self.optimizer.zero_grad()
forecast_load, pg, ls, gs = self.net(feature, target, self.b_default.repeat(len(target), 1))
loss = self.loss(pg, ls, gs)
loss = loss.mean()
loss.backward()
# gradient clip
if self.clip_norm != 0:
torch.nn.utils.clip_grad_norm_(self.net.parameters(), norm_type = 1, max_norm = self.clip_norm)
self.optimizer.step()
loss_sum += loss.item() * len(target)
return loss_sum / len(self.trainloader.dataset)
def eval(self):
self.net.eval()
loss_sum = 0.
with torch.no_grad():
for feature, target in self.testloader:
forecast_load, pg, ls, gs = self.net(feature, target, self.b_default.repeat(len(target), 1))
loss = self.loss(pg, ls, gs)
loss = loss.mean()
loss_sum += loss.item() * len(target)
return loss_sum / len(self.testloader.dataset)
if __name__ == '__main__':
import json
import argparse
from helper import return_nn_model, return_operator
import time
parser = argparse.ArgumentParser()
parser.add_argument('-c', '--case_name', type = str, default = 'case14')
parser.add_argument('-p', '--pre_train', default = False, action = 'store_true')
args = parser.parse_args()
with open("config.json") as f:
config = json.load(f)
random_seed = config['random_seed']
batch_size = config['nn']['batch_size_spo']
batch_size_eval = config['nn']['batch_size_spo']
lr = config['nn']['lr_spo']
epoch = config['nn']['epoch_spo']
model_dir = config['nn']['model_dir']
watch = config['nn']['watch_spo']
fix_first_b = config['fix_first_b']
is_scale = config['is_scale']
gradient_clip_norm = config['nn']['gradient_clip_norm']
T_max = config['nn']['T_max']
min_lr_ratio = config['nn']['min_lr_ratio']
train_with_test = config['nn']['train_with_test_spo']
solver_args = config['nn']['solver_args']
if watch == 'test':
assert train_with_test == True
torch.manual_seed(random_seed)
np.random.seed(random_seed)
random.seed(random_seed)
# data
train_dataset = MyDataset(case_name = args.case_name, mode = "train")
test_dataset = MyDataset(case_name = args.case_name, mode = "test")
trainloader = DataLoader(train_dataset, batch_size = batch_size, shuffle = True)
testloader = DataLoader(test_dataset, batch_size = batch_size_eval, shuffle = False)
print("Training on {} with SPO loss".format(args.case_name))
print("Size of train dataset: {}".format(len(train_dataset)))
print("Shape of feature: {}".format(train_dataset[0][0].shape))
print("epoch: ", epoch)
# net
operator = return_operator(args.case_name)
# pack the optimization layers
net = return_nn_model(case_name = args.case_name, is_load = args.pre_train, train_method = f'mse_warm')
assert net.name == 'NN'
if is_scale:
mean = train_dataset.target_mean
std = train_dataset.target_std
else:
mean = 0
std = 1
net = NN_SPO(model = net, operator=operator, mean = mean, std = std,
fix_first_b = fix_first_b,
solver_args=solver_args) # construct the spo model
print('nn structure')
print(net)
assert net.name == 'NN_SPO'
is_small_size = config['is_small_size']
if is_small_size:
sample_size = len(train_dataset)
else:
sample_size = 'full'
# optimizer = torch.optim.Adam(net.parameters(), lr = lr)
optimizer = torch.optim.SGD(net.parameters(), lr = lr)
trainer = Trainer_SPO(net, optimizer, trainloader, testloader, operator, gradient_clip_norm)
if not os.path.exists(model_dir):
os.makedirs(model_dir)
save_path = f'{model_dir}/{sample_size}/spo_clean.pth'
best_loss = 1e5
for i in range(1, epoch+1):
start_time = time.time()
train_loss = trainer.train()
if train_with_test:
test_loss = trainer.eval()
print("Epoch {}: train loss-{:.4f}, test loss-{:.4f}".format(i, train_loss, test_loss))
else:
print("Epoch {}: train loss-{:.4f}".format(i, train_loss))
print("Time: {:.2f}s".format(time.time() - start_time))
for param_group in trainer.optimizer.param_groups:
print("LR: {:.6f}".format(param_group['lr']))
# reduce the learning rate
# if i == int((epoch+1)/2):
# for param_group in trainer.optimizer.param_groups:
# param_group['lr'] *= 0.2
if watch == 'train' and train_loss < best_loss:
best_loss = train_loss
torch.save(trainer.net.state_dict(), save_path)
print("Best model saved by train!")
if watch == 'test' and test_loss < best_loss:
best_loss = test_loss
torch.save(trainer.net.state_dict(), save_path)
print("Best model saved by test!")
print("==============================================")