-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpytorch_coms.py
166 lines (142 loc) · 5.96 KB
/
pytorch_coms.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
import numpy as np
import torch
import torch.nn as nn
import torch.optim as optim
from copy import deepcopy
def freeze(model):
assert isinstance(model, nn.Module)
for p in model.parameters():
p.requires_grad = False
def unfreeze(model):
assert isinstance(model, nn.Module)
for p in model.parameters():
p.requires_grad = True
class ConservativeObjectiveModel:
def __init__(
self,
forward_model,
forward_model_opt=optim.Adam,
forward_model_lr=0.001,
alpha=1.0,
alpha_opt=optim.Adam,
alpha_lr=0.01,
overestimation_limit=0.5,
particle_lr=0.05,
particle_gradient_steps=50,
entropy_coefficient=0.0,
noise_std=0.0,
):
super().__init__()
self.forward_model = forward_model
self.forward_model_opt = forward_model_opt(
self.forward_model.parameters(), lr=forward_model_lr
)
# lagrangian dual descent variable (controls the conservatism of the model)
self.log_alpha = nn.Parameter(torch.tensor(np.log(alpha).astype(np.float64)))
self.alpha_opt = alpha_opt([self.log_alpha], lr=alpha_lr)
self.overestimation_limit = overestimation_limit
self.particle_lr = particle_lr
self.particle_gradient_steps = particle_gradient_steps
self.entropy_coefficient = entropy_coefficient
self.noise_std = noise_std
def optimize(self, x, steps, allow_grad=False, **kwargs):
"""Generate adversarial examples from starting point x
used later for adversarial training.
"""
xt = nn.Parameter(deepcopy(x).to(x.device), requires_grad=True)
for _ in range(steps):
# shuffle the designs for calculating entropy
idx = torch.randperm(xt.shape[0])
shuffled_xt = xt[idx]
# entropy using the gaussian kernel
entropy = torch.mean((xt - shuffled_xt) ** 2)
score = self.forward_model(xt)
loss = self.entropy_coefficient * entropy + score
(grad,) = torch.autograd.grad(torch.sum(loss), xt)
xt = xt + self.particle_lr * grad
return xt.clone().detach()
def train_step(self, x, y):
# (potentially) corrupt the inputs with noise
# depends on the hyper-parameter `noise_std`
x = x + self.noise_std * torch.randn_like(x)
statistics = dict()
# freeze the model to not update the weights while generating
# adversarial examples
freeze(self.forward_model)
# calculate negative samples starting from the dataset
x_neg = self.optimize(x, self.particle_gradient_steps)
unfreeze(self.forward_model)
d_pos = self.forward_model(x)
loss_fn = nn.MSELoss()
mse = loss_fn(d_pos, y)
statistics[f"train/mse"] = mse.detach().clone()
d_neg = self.forward_model(x_neg)
overestimation = d_neg[:, 0] - d_pos[:, 0]
statistics[f"train/overestimation"] = torch.mean(overestimation.detach().clone())
# if statistics[f"train/overestimation"] >= 1:
# print(d_neg.squeeze()[:5])
# print(d_pos.squeeze()[:5])
alpha_loss = (
self.log_alpha.exp() * self.overestimation_limit
- self.log_alpha.exp() * overestimation
)
statistics[f"train/alpha"] = self.log_alpha.exp().detach().clone()
model_loss = mse + self.log_alpha.exp().detach().clone() * overestimation
total_loss = torch.mean(model_loss)
alpha_loss = torch.mean(alpha_loss)
statistics[f"train/alpha_loss"] = alpha_loss
self.alpha_opt.zero_grad()
alpha_loss.backward(retain_graph=True)
self.forward_model_opt.zero_grad()
total_loss.backward()
self.alpha_opt.step()
self.forward_model_opt.step()
return statistics
def validate_step(self, x, y):
statistics = dict()
with torch.no_grad():
# calculate the prediction error and accuracy of the model
d_pos = self.forward_model(x)
mse = torch.nn.functional.mse_loss(d_pos, y)
statistics[f"validate/mse"] = mse
return statistics
def train(self, dataset):
statistics = {}
for x, y in dataset:
for name, tensor in self.train_step(x, y).items():
if name not in statistics.keys():
statistics[name] = [tensor]
else:
statistics[name].append(tensor)
for name in statistics.keys():
statistics[name] = torch.cat([x.unsqueeze(0) for x in statistics[name]])
return statistics
def validate(self, dataset):
statistics = {}
for x, y in dataset:
for name, tensor in self.validate_step(x, y).items():
if name not in statistics.keys():
statistics[name] = [tensor]
else:
statistics[name].append(tensor)
for name in statistics.keys():
statistics[name] = torch.cat([x.unsqueeze(0) for x in statistics[name]])
return statistics
def launch(self, train_data, validate_data, epochs, logger=None):
"""Entry point for training the model.
"""
for e in range(epochs):
print(f'epoch {e}')
for name, value in self.train(train_data).items():
if name == "train/alpha":
alpha = value
print(f"alpha min {alpha.min()}, max {alpha.max()}")
if name == "train/overestimation":
print(f"overestimation min {value.min()}, max {value.max()}")
if name == "train/alpha_loss":
print(f"alpha loss: {value.mean()}")
if name == "train/mse":
print(f"train loss: {value.mean()}", end=" | ")
for name, loss in self.validate(validate_data).items():
print(f"validation loss: {loss.mean()}")
return alpha