-
Notifications
You must be signed in to change notification settings - Fork 0
/
odeint.py
296 lines (243 loc) · 8.83 KB
/
odeint.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
import mlx.core as mx
from solver import str_to_solver
def hairer_norm(arr):
"""L2-type norm used for stepsize selection."""
return mx.sqrt(mx.mean(mx.power(mx.abs(arr), 2)))
def init_step(f, f0, x0, t0, order, atol, rtol):
"""
Estimate a good initial dt by comparing the scale of x0 and f0.
"""
scale = atol + mx.abs(x0) * rtol
d0 = hairer_norm(x0 / scale)
d1 = hairer_norm(f0 / scale)
if (d0 < 1e-5) or (d1 < 1e-5):
h0 = mx.array(1e-6, dtype=t0.dtype)
else:
h0 = 0.01 * d0 / d1
x_new = x0 + h0 * f0
f_new = f(t0 + h0, x_new)
d2 = hairer_norm((f_new - f0) / scale) / h0
if (d1 <= 1e-15) and (d2 <= 1e-15):
h1 = mx.maximum(mx.array(1e-6, dtype=t0.dtype), h0 * 1e-3)
else:
h1 = (0.01 / mx.maximum(d1, d2)) ** (1.0 / float(order + 1))
dt = mx.minimum(100 * h0, h1)
return dt
def adapt_step(dt, error_ratio, safety, min_factor, max_factor, order):
"""
Adaptive stepsize update. If error_ratio < 1, we accept and possibly increase dt.
If error_ratio > 1, we reject and decrease dt.
"""
if error_ratio == 0:
return dt * max_factor
if error_ratio < 1:
min_factor = mx.ones_like(dt)
exponent = mx.array(order, dtype=dt.dtype).reciprocal()
factor = mx.minimum(
max_factor, mx.maximum(safety / error_ratio**exponent, min_factor)
)
return dt * factor
def adaptive_odeint(
f,
k1,
x,
dt,
t_span,
solver,
atol=1e-4,
rtol=1e-4,
args=None,
interpolator=None,
return_all_eval=False,
seminorm=(False, None),
):
"""
Adaptive integrator for solvers that provide an error estimate (DormandPrince45, etc).
"""
# t, T as scalars
t = t_span[0]
T = t_span[-1]
# t_eval for checkpoint times
t_eval = t_span[1:] if len(t_span) > 1 else mx.array([], dtype=t_span.dtype)
ckpt_counter, ckpt_flag = 0, False
eval_times, sol = [t], [x]
while t < T:
# Adjust dt if we're about to overshoot T
if t + dt > T:
dt = T - t
# Possibly shorten dt to hit a checkpoint exactly
if len(t_eval) > 0 and (ckpt_counter < len(t_eval)):
if interpolator is None:
next_ckpt = t_eval[ckpt_counter]
if t + dt > next_ckpt:
dt_old = dt
dt = next_ckpt - t
ckpt_flag = True
# Step
f_new, x_new, x_err, stages = solver.step(f, x, t, dt, k1, args=args)
# Error ratio
if x_err is not None:
if seminorm[0]:
sd = seminorm[1]
error_scaled = x_err[:sd] / (
atol + rtol * mx.maximum(mx.abs(x[:sd]), mx.abs(x_new[:sd]))
)
else:
error_scaled = x_err / (
atol + rtol * mx.maximum(mx.abs(x), mx.abs(x_new))
)
error_ratio = hairer_norm(error_scaled)
else:
# No error => accept
error_ratio = 0
accept_step = error_ratio <= 1
if accept_step:
# Interpolation
if interpolator is not None and (ckpt_counter < len(t_eval)):
coefs = None
while (ckpt_counter < len(t_eval)) and (
(t + dt) > t_eval[ckpt_counter]
):
t0, t1 = t, t + dt
if coefs is None:
x_mid = x + dt * sum(
interpolator.bmid[i] * stages[i] for i in range(len(stages))
)
f0, f1, x0, x1 = k1, f_new, x, x_new
coefs = interpolator.fit(dt, f0, f1, x0, x1, x_mid)
x_ckpt = interpolator.evaluate(coefs, t0, t1, t_eval[ckpt_counter])
sol.append(x_ckpt)
eval_times.append(t_eval[ckpt_counter])
ckpt_counter += 1
# If new time matches a checkpoint or we want all evaluations
if (
(ckpt_counter < len(t_eval))
and (mx.isclose(t + dt, t_eval[ckpt_counter]).sum() == 1)
) or return_all_eval:
sol.append(x_new)
eval_times.append(t + dt)
if (ckpt_counter < len(t_eval)) and mx.isclose(
t + dt, t_eval[ckpt_counter]
).sum():
ckpt_counter += 1
# Accept
t, x = t + dt, x_new
k1 = f_new
else:
# Revert dt if we shortened for a checkpoint
if ckpt_flag:
dt = dt_old
ckpt_flag = False
# Adapt dt
dt = adapt_step(
dt,
error_ratio,
solver.safety,
solver.min_factor,
solver.max_factor,
solver.order,
)
return mx.array(eval_times), mx.stack(sol)
def fixed_odeint(f, x, t_span, solver, save_at=None, args=None):
"""
Fixed-step integrator (Euler, Midpoint, etc).
"""
if save_at is None:
save_at = t_span
elif not isinstance(save_at, mx.array):
save_at = mx.array(save_at)
# Each save_at time must appear exactly once in t_span
for t_s in save_at:
c = mx.isclose(t_span, t_s).sum()
assert c == 1, f"Time {t_s} in save_at not found exactly once in t_span!"
t = t_span[0]
T = t_span[-1]
dt = t_span[1] - t_span[0] if len(t_span) > 1 else mx.array(0.0, dtype=t.dtype)
sol = []
if mx.isclose(t, save_at).sum():
sol.append(x)
steps = 0
while steps < len(t_span) - 1:
# Step
f_new, x_new, err, stages = solver.step(f, x, t, dt, k1=None, args=args)
x = x_new
t = t + dt
steps += 1
# If new time is in save_at, store
if mx.isclose(t, save_at).sum():
sol.append(x)
# Prepare next dt
if steps < len(t_span) - 1:
dt = t_span[steps + 1] - t
if isinstance(sol[0], dict):
# If the state is a dictionary
final_out = {k: [] for k in sol[0].keys()}
for s in sol:
for k in s.keys():
final_out[k].append(s[k])
final_out = {k: mx.stack(v) for k, v in final_out.items()}
return save_at, final_out
return save_at, mx.stack(sol)
def odeint(f, x, t_span, solver, atol: float = 1e-3, rtol: float = 1e-3):
"""
High-level ODE solver interface. Uses fixed or adaptive integration.
"""
if not isinstance(t_span, mx.array):
t_span = mx.array(t_span)
if solver.stepping_class == "fixed":
return fixed_odeint(f, x, t_span, solver)
else:
t0 = t_span[0]
k1 = f(t0, x)
dt = init_step(f, k1, x, t0, solver.order, atol, rtol)
return adaptive_odeint(f, k1, x, dt, t_span, solver, atol, rtol)
################################################################################
# NEURAL ODE #
################################################################################
class NeuralODE:
"""
Example container that uses 'odeint' to integrate a vector field.
"""
def __init__(self, vector_field, solver="dopri5", atol=1e-4, rtol=1e-4):
self.vf = vector_field
self.solver = str_to_solver(solver)
self.atol = atol
self.rtol = rtol
def forward(self, x, t_span):
"""Can define your own forward pass if needed."""
raise NotImplementedError
def trajectory(self, x, t_span):
"""
Integrate the vector_field from x(t0) across t_span.
Returns the stacked solution states.
"""
_, sol = odeint(self.vf, x, t_span, self.solver, self.atol, self.rtol)
return sol
def __repr__(self):
return (
f"Neural ODE:\n\t- solver: {self.solver}"
f"\n\t- order: {self.solver.order}"
f"\n\t- tolerances: relative {self.rtol} absolute {self.atol}"
)
if __name__ == "__main__":
import matplotlib.pyplot as plt
def vector_field(t, x): # dx/dt = -x
return -x
x0 = mx.array([[1.0]])
t_span = mx.linspace(0, 5, 50)
euler_ode = NeuralODE(vector_field, solver="euler")
midpoint_ode = NeuralODE(vector_field, solver="midpoint")
dopri_ode = NeuralODE(vector_field, solver="dopri5", atol=1e-6, rtol=1e-6)
sol_euler = euler_ode.trajectory(x0, t_span)
sol_midpoint = midpoint_ode.trajectory(x0, t_span)
sol_dopri = dopri_ode.trajectory(x0, t_span)
fig, ax = plt.subplots(figsize=(6, 4))
ax.plot(t_span, sol_euler[:, 0, 0], ".-", label="NeuralODE Euler")
ax.plot(t_span, sol_midpoint[:, 0, 0], "+-", label="NeuralODE Midpoint")
ax.plot(t_span, sol_dopri[:, 0, 0], "--", label="NeuralODE Dopri5")
ax.set_xlabel("time")
ax.set_ylabel("x(t)")
ax.grid(alpha=0.3)
ax.legend()
plt.tight_layout()
plt.show()