forked from desi-ivanova/idad
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pendulum.py
337 lines (284 loc) · 11.8 KB
/
pendulum.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
import os
import mlflow
import mlflow.pytorch
import pandas as pd
import pyro
import pyro.distributions as dist
import torch
import torch.nn as nn
from pyro.infer.util import torch_item
from tqdm import trange
from estimators.mi import PriorContrastiveEstimation
from experiment_tools.pyro_tools import auto_seed
from neural.aggregators import LSTMImplicitDAD
from neural.modules import Mlp
from oed.design import OED
from oed.primitives import compute_design, latent_sample, observation_sample
class SimplePendulum(nn.Module):
def __init__(self, design_net, device, T):
super(SimplePendulum, self).__init__()
self.design_net = design_net
self.T = T
self.dt = 0.05
self.scale = 1.0
self.shift = 0.0
self.log_theta_prior = dist.MultivariateNormal(
torch.tensor([0.0, 0.0], device=device),
torch.diag(torch.tensor([0.01, 0.01], device=device)),
)
self.init_state = torch.tensor([0.0, 0.0], device=device)
self.diffusion_vector = torch.tensor([0.0, 1e-1], device=device)
self.cov = torch.diag(self.diffusion_vector**2 * self.dt + 1e-8)
def ode(self, x, u, theta):
"""
ODE of the simple pendulum.
Should be able to deal with any permutation of
single or batched `x`, `u` and `theta`.
"""
m, l = [theta[..., [i]] for i in range(2)]
g, d = 9.81, 1e-1
q, dq = x[..., [0]], x[..., [1]]
ddq = -3.0 * g / (2.0 * l) * torch.sin(q) + (u - d * dq) * 3.0 / (m * l**2)
if dq.size() != ddq.size():
# Add dimensions to dq
dq = dq.expand(ddq.size())
return torch.cat([dq, ddq], dim=-1)
def _simulator(self, x, u, theta):
euler_mean = x + self.dt * self.ode(x, u, theta)
return dist.MultivariateNormal(euler_mean, scale_tril=torch.sqrt(self.cov))
def _transform_design(self, xi_untransformed):
return self.shift + self.scale * nn.Tanh()(xi_untransformed)
def model(self):
# Not sure what this does.
if hasattr(self.design_net, "parameters"):
pyro.module("design_net", self.design_net)
################################################################################
# Sample theta
################################################################################
# sample log-theta and exponentiate
theta = latent_sample("log_theta", self.log_theta_prior).exp()
y_outcomes = []
xi_designs = []
y = self.init_state
for t in range(self.T):
####################################################################
# Get a design xi
####################################################################
xi_untransformed = compute_design(
f"xi{t + 1}", self.design_net.lazy(*zip(xi_designs, y_outcomes))
)
xi = self._transform_design(xi_untransformed)
####################################################################
# Sample y
####################################################################
_sim = self._simulator(x=y, u=xi, theta=theta)
y = observation_sample(f"y{t + 1}", _sim)
# y = observation_sample(f"y{t + 1}", self.simulator, xi, theta)
####################################################################
# Update history
####################################################################
y_outcomes.append(y)
xi_designs.append(xi_untransformed) #! work with untransformed designs
# T-steps experiment
return xi_designs, y_outcomes, theta
def forward(self, log_theta):
"""Run the policy"""
self.design_net.eval()
def model():
with pyro.plate_stack("expand_theta_test", [log_theta.shape[0]]):
# condition on theta
return pyro.condition(self.model, data={"log_theta": log_theta})()
with torch.no_grad():
designs, outcomes, theta = model()
self.design_net.train()
return designs, outcomes
def eval(self, n_trace=3, log_theta=None, verbose=True):
self.design_net.eval()
if log_theta is None:
log_theta = self.log_theta_prior.sample(torch.Size([n_trace]))
else:
log_theta = log_theta.unsqueeze(0).expand(n_trace, *log_theta.shape)
# dims: [n_trace * number of thetas given, shape of theta]
log_theta = log_theta.reshape(
-1,
*log_theta.shape[2:],
)
output = []
designs, outcomes = self.forward(log_theta)
theta = log_theta.exp()
for i in range(n_trace):
run_xis = []
run_ys = []
if verbose:
print("Example run {}".format(i))
print(f"*True Theta: {theta[i].cpu()}*")
for t in range(self.T):
xi_untransformed = designs[t][i].detach()
xi = self._transform_design(xi_untransformed).cpu().reshape(-1)
run_xis.append(xi)
y = outcomes[t][i].detach().cpu().reshape(-1)
run_ys.append(y)
if verbose:
print(f"xi{t + 1}: {run_xis[-1]}, y{t + 1}: {run_ys[-1]}")
run_df = pd.DataFrame(torch.stack(run_xis).numpy())
run_df.columns = [f"xi_{i}" for i in range(xi.shape[0])]
run_df["observations"] = run_ys
run_df["order"] = list(range(1, self.T + 1))
run_df["run_id"] = i + 1
output.append(run_df)
self.design_net.train()
return pd.concat(output), theta.cpu().numpy()
def train_model(
num_steps,
batch_size,
num_negative_samples,
seed,
lr,
gamma,
device,
T,
hidden_dim,
encoding_dim,
mlflow_experiment_name,
):
pyro.clear_param_store()
### Set up Mlflow logging ### ------------------------------------------------------
mlflow.set_experiment(mlflow_experiment_name)
seed = auto_seed(seed)
#####
n = 2 # Output dim
design_dim = 1
observation_dim = n
mlflow.log_param("seed", seed)
mlflow.log_param("num_experiments", T)
mlflow.log_param("lr", lr)
mlflow.log_param("gamma", gamma)
mlflow.log_param("num_steps", num_steps)
mlflow.log_param("hidden_dim", hidden_dim)
mlflow.log_param("encoding_dim", encoding_dim)
# ----------------------------------------------------------------------------------
###################################################################################
### Setup design and critic networks
###################################################################################
### DESIGN NETWORK ###
history_encoder = Mlp(
input_dim=[design_dim, observation_dim],
hidden_dim=[hidden_dim, hidden_dim], # hidden_dim,
output_dim=encoding_dim,
activation=nn.ReLU(),
name="policy_history_encoder",
)
design_emitter = Mlp(
# iDAD only -> options are sum or cat
input_dim=encoding_dim,
hidden_dim=[hidden_dim, hidden_dim],
output_dim=design_dim,
activation=nn.ReLU(),
name="policy_design_emitter",
)
# iDAD LSTM aggregator
design_net = LSTMImplicitDAD(
history_encoder,
design_emitter,
empty_value=torch.zeros(design_dim, device=device),
num_hidden_layers=2,
).to(device)
#######################################################################
# print("initilize net")
# design_net.apply(init_weights)
pendulum = SimplePendulum(design_net, device, T=T)
def separate_learning_rate(module_name, param_name):
if module_name == "critic_net":
return {"lr": lr}
elif module_name == "design_net":
return {"lr": lr}
else:
raise NotImplementedError()
optimizer = torch.optim.Adam
patience = 5
annealing_freq = 400
mlflow.log_param("annealing_scheme", [annealing_freq, patience, gamma])
scheduler = pyro.optim.ReduceLROnPlateau(
{
"optimizer": optimizer,
"optim_args": separate_learning_rate,
"factor": gamma,
"patience": patience,
"verbose": False,
}
)
mi_loss_instance = PriorContrastiveEstimation(
model=pendulum.model,
batch_size=batch_size,
num_negative_samples=num_negative_samples,
)
mlflow.log_param("num_negative_samples", mi_loss_instance.num_negative_samples)
mlflow.log_param("num_batch_samples", mi_loss_instance.batch_size)
oed = OED(optim=scheduler, loss=mi_loss_instance)
loss_history = []
outputs_history = []
# num_steps_range = trange(1, num_steps + 1, desc="Loss: 0.000 ")
num_steps_range = trange(0, num_steps + 0, desc="Loss: 0.000 ")
# Evaluate model and store designs for this latent:
test_log_theta = torch.tensor([1.0, 1.5], device=device).log().unsqueeze(0)
### Log params:
def count_parameters(model):
return sum(p.numel() for p in model.parameters() if p.requires_grad)
mlflow.log_param("num_params_designnet", count_parameters(design_net))
for i in num_steps_range:
pendulum.train()
loss = oed.step()
loss = torch_item(loss)
loss_history.append(loss)
num_steps_range.set_description("Loss: {:.3f} ".format(loss))
if i % annealing_freq == 0:
# Log the loss
loss_eval = oed.evaluate_loss()
mlflow.log_metric("loss", loss_eval, step=i)
# Check if lr should be decreased.
scheduler.step(loss_eval)
df, latents = pendulum.eval(
n_trace=1, log_theta=test_log_theta, verbose=False
)
df["step"] = i
outputs_history.append(df)
if not os.path.exists("mlflow_outputs"):
os.makedirs("mlflow_outputs")
pd.concat(outputs_history).reset_index().to_csv(f"mlflow_outputs/designs_hist.csv")
mlflow.log_artifact(f"mlflow_outputs/designs_hist.csv", artifact_path="designs")
pendulum.eval()
# store params, metrics and artifacts to mlflow ------------------------------------
print("Storing model to MlFlow... ", end="")
mlflow.pytorch.log_model(pendulum.cpu(), "model")
ml_info = mlflow.active_run().info
model_loc = f"mlruns/{ml_info.experiment_id}/{ml_info.run_id}/artifacts"
print(f"Model and critic sotred in {model_loc}. Done.")
mlflow.log_param("status", "complete")
return pendulum
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="iDAD: SDE-Based Pendulum Model")
parser.add_argument("--num-steps", default=10000, type=int)
parser.add_argument("--num-batch-samples", default=512, type=int)
parser.add_argument("--num-negative-samples", default=16383, type=int)
parser.add_argument("--seed", default=-1, type=int)
parser.add_argument("--lr", default=0.0005, type=float)
parser.add_argument("--gamma", default=0.96, type=float)
parser.add_argument("--device", default="cuda:1", type=str)
parser.add_argument("--num-experiments", default=50, type=int)
parser.add_argument("--hidden-dim", default=256, type=int)
parser.add_argument("--encoding-dim", default=64, type=int)
parser.add_argument("--mlflow-experiment-name", default="pendulum", type=str)
args = parser.parse_args()
train_model(
num_steps=args.num_steps,
batch_size=args.num_batch_samples,
num_negative_samples=args.num_negative_samples,
seed=args.seed,
lr=args.lr,
gamma=args.gamma,
device=args.device,
T=args.num_experiments,
hidden_dim=args.hidden_dim,
encoding_dim=args.encoding_dim,
mlflow_experiment_name=args.mlflow_experiment_name,
)