-
Notifications
You must be signed in to change notification settings - Fork 1
/
learn_physics.py
284 lines (221 loc) · 8.46 KB
/
learn_physics.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
#!/usr/bin/env python3
import argparse
import os
import math
import matplotlib.pyplot as plt
import torch
import torch.nn as nn
from torchdiffeq import odeint, odeint_event
from bouncing_ball import BouncingBallExample
class HamiltonianDynamics(nn.Module):
def __init__(self):
super().__init__()
self.dvel = nn.Linear(1, 1)
self.scale = nn.Parameter(torch.tensor(10.0))
def forward(self, t, state):
pos, vel, *rest = state
dpos = vel
dvel = torch.tanh(self.dvel(torch.zeros_like(vel))) * self.scale
return (dpos, dvel, *[torch.zeros_like(r) for r in rest])
class EventFn(nn.Module):
def __init__(self):
super().__init__()
self.radius = nn.Parameter(torch.rand(1))
def parameters(self):
return [self.radius]
def forward(self, t, state):
# IMPORTANT: event computation must use variables from the state.
pos, _, radius = state
return pos - radius.reshape_as(pos) ** 2
class InstantaneousStateChange(nn.Module):
def __init__(self):
super().__init__()
self.net = nn.Linear(1, 1)
def forward(self, t, state):
pos, vel, *rest = state
vel = -torch.sigmoid(self.net(torch.ones_like(vel))) * vel
return (pos, vel, *rest)
class NeuralPhysics(nn.Module):
def __init__(self):
super().__init__()
self.initial_pos = nn.Parameter(torch.tensor([10.0]))
self.initial_vel = nn.Parameter(torch.tensor([0.0]))
self.dynamics_fn = HamiltonianDynamics()
self.event_fn = EventFn()
self.inst_update = InstantaneousStateChange()
def simulate(self, times):
t0 = torch.tensor([0.0]).to(times)
# Add a terminal time to the event function.
def event_fn(t, state):
if t > times[-1] + 1e-7:
return torch.zeros_like(t)
event_fval = self.event_fn(t, state)
return event_fval
# IMPORTANT: for gradients of odeint_event to be computed, parameters of the event function
# must appear in the state in the current implementation.
state = (self.initial_pos, self.initial_vel, *self.event_fn.parameters())
event_times = []
trajectory = [state[0][None]]
n_events = 0
max_events = 20
while t0 < times[-1] and n_events < max_events:
last = n_events == max_events - 1
if not last:
event_t, solution = odeint_event(
self.dynamics_fn,
state,
t0,
event_fn=event_fn,
atol=1e-8,
rtol=1e-8,
method="dopri5",
)
else:
event_t = times[-1]
interval_ts = times[times > t0]
interval_ts = interval_ts[interval_ts <= event_t]
interval_ts = torch.cat([t0.reshape(-1), interval_ts.reshape(-1)])
solution_ = odeint(
self.dynamics_fn, state, interval_ts, atol=1e-8, rtol=1e-8
)
traj_ = solution_[0][1:] # [0] for position; [1:] to remove intial state.
trajectory.append(traj_)
if event_t < times[-1]:
state = tuple(s[-1] for s in solution)
# update velocity instantaneously.
state = self.inst_update(event_t, state)
# advance the position a little bit to avoid re-triggering the event fn.
pos, *rest = state
pos = pos + 1e-7 * self.dynamics_fn(event_t, state)[0]
state = pos, *rest
event_times.append(event_t)
t0 = event_t
n_events += 1
# print(event_t.item(), state[0].item(), state[1].item(), self.event_fn.mod(pos).item())
trajectory = torch.cat(trajectory, dim=0).reshape(-1)
return trajectory, event_times
class Sine(nn.Module):
def forward(self, x):
return torch.sin(x)
class NeuralODE(nn.Module):
def __init__(self, aug_dim=2):
super().__init__()
self.initial_pos = nn.Parameter(torch.tensor([10.0]))
self.initial_aug = nn.Parameter(torch.zeros(aug_dim))
self.odefunc = mlp(
input_dim=1 + aug_dim,
hidden_dim=64,
output_dim=1 + aug_dim,
hidden_depth=2,
act=Sine,
)
def init(m):
if isinstance(m, nn.Linear):
std = 1.0 / math.sqrt(m.weight.size(1))
m.weight.data.uniform_(-2.0 * std, 2.0 * std)
m.bias.data.zero_()
self.odefunc.apply(init)
def forward(self, t, state):
return self.odefunc(state)
def simulate(self, times):
x0 = torch.cat([self.initial_pos, self.initial_aug]).reshape(-1)
solution = odeint(self, x0, times, atol=1e-8, rtol=1e-8, method="dopri5")
trajectory = solution[:, 0]
return trajectory, []
def mlp(input_dim, hidden_dim, output_dim, hidden_depth, output_mod=None, act=nn.ReLU):
if hidden_depth == 0:
mods = [nn.Linear(input_dim, output_dim)]
else:
mods = [nn.Linear(input_dim, hidden_dim), act()]
for i in range(hidden_depth - 1):
mods += [nn.Linear(hidden_dim, hidden_dim), act()]
mods.append(nn.Linear(hidden_dim, output_dim))
if output_mod is not None:
mods.append(output_mod)
trunk = nn.Sequential(*mods)
return trunk
def cosine_decay(learning_rate, global_step, decay_steps, alpha=0.0):
global_step = min(global_step, decay_steps)
cosine_decay = 0.5 * (1 + math.cos(math.pi * global_step / decay_steps))
decayed = (1 - alpha) * cosine_decay + alpha
return learning_rate * decayed
def learning_rate_schedule(
global_step, warmup_steps, base_learning_rate, lr_scaling, train_steps
):
warmup_steps = int(round(warmup_steps))
scaled_lr = base_learning_rate * lr_scaling
if warmup_steps:
learning_rate = global_step / warmup_steps * scaled_lr
else:
learning_rate = scaled_lr
if global_step < warmup_steps:
learning_rate = learning_rate
else:
learning_rate = cosine_decay(
scaled_lr, global_step - warmup_steps, train_steps - warmup_steps
)
return learning_rate
def set_learning_rate(optimizer, lr):
for group in optimizer.param_groups:
group["lr"] = lr
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--base_lr", type=float, default=0.1)
parser.add_argument("--num_iterations", type=int, default=1000)
parser.add_argument("--no_events", action="store_true")
parser.add_argument("--save", type=str, default="figs")
args = parser.parse_args()
torch.manual_seed(0)
torch.set_default_dtype(torch.float64)
with torch.no_grad():
system = BouncingBallExample()
obs_times, gt_trajectory, _, _ = system.simulate(nbounces=4)
obs_times = obs_times[:300]
gt_trajectory = gt_trajectory[:300]
if args.no_events:
model = NeuralODE()
else:
model = NeuralPhysics()
optimizer = torch.optim.Adam(model.parameters(), lr=args.base_lr)
decay = 1.0
model.train()
for itr in range(args.num_iterations):
optimizer.zero_grad()
trajectory, event_times = model.simulate(obs_times)
weights = decay**obs_times
loss = (
((trajectory - gt_trajectory) / (gt_trajectory + 1e-3))
.abs()
.mul(weights)
.mean()
)
loss.backward()
lr = learning_rate_schedule(itr, 0, args.base_lr, 1.0, args.num_iterations)
set_learning_rate(optimizer, lr)
optimizer.step()
if itr % 10 == 0:
print(itr, loss.item(), len(event_times))
if itr % 10 == 0:
plt.figure()
plt.plot(
obs_times.detach().cpu().numpy(),
gt_trajectory.detach().cpu().numpy(),
label="Target",
)
plt.plot(
obs_times.detach().cpu().numpy(),
trajectory.detach().cpu().numpy(),
label="Learned",
)
plt.tight_layout()
os.makedirs(args.save, exist_ok=True)
plt.savefig(f"{args.save}/{itr:05d}.png")
plt.close()
if (itr + 1) % 100 == 0:
torch.save(
{
"state_dict": model.state_dict(),
},
f"{args.save}/model.pt",
)
del trajectory, loss