-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathdatasets.py
444 lines (391 loc) · 14.4 KB
/
datasets.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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
# pytype: skip-file
"""
This module provides functions to generate various datasets for training and
testing neural networks on differential equations and waveform data. The
datasets include delay differential equations (DDEs), integro-differential
equations, and common waveforms like sine, square, and sawtooth waves. The data
generation functions support options for data normalization, adding noise, and
handling missing data.
"""
###########################
# Neural Laplace: Learning diverse classes of differential equations in the
# Laplace domain
# Author: Samuel Holt
###########################
import shelve
from functools import partial
from pathlib import Path
import numpy as np
import scipy.io as sio
import torch
from ddeint import ddeint
from torch.utils.data import DataLoader
from tqdm import tqdm
from torchlaplace.data_utils import basic_collate_fn
local_path = Path(__file__).parent
# DE Datasets
def dde_ramp_loading_time_sol(device,
double=False,
trajectories_to_sample=100,
t_nsamples=200):
t_end = 20.0
t_begin = t_end / t_nsamples
if double:
ti = torch.linspace(t_begin, t_end, t_nsamples).to(device).double()
else:
ti = torch.linspace(t_begin, t_end, t_nsamples).to(device)
def sampler(t, x0=0):
ans = torch.ones_like(t) * x0 * torch.cos(2 * t)
ans += ((1 / 5.0) * torch.tensor((5 <= t) * (t < 10)).to(torch.float) * \
(1.0 / 4.0) * ((t - 5) - 0.5 * torch.sin(2 * (t - 5))))
ans += ((1 / 5.0) * torch.tensor(10 <= t).to(torch.float) * (1.0 / 4.0) *
((t - 5) -
(t - 10) - 0.5 * torch.sin(2 *
(t - 5)) + 0.5 * torch.sin(2 *
(t - 10))))
return ans
x0s = torch.linspace(0, 1 / 10, trajectories_to_sample)
trajs = []
for x0 in x0s:
trajs.append(sampler(ti, x0))
y = torch.stack(trajs)
trajectories = y.view(trajectories_to_sample, -1, 1)
return trajectories, ti
def stiffvdp(device, double=False, trajectories_to_sample=100, one_dim=True): # pylint: disable=unused-argument
mat_contents = sio.loadmat(local_path / "data/vdp_all.mat")
tm = mat_contents["t_samp"].ravel()
trajs = mat_contents["all"]
if double:
trajs = torch.from_numpy(trajs).to(device).double()
else:
trajs = torch.from_numpy(trajs).to(torch.float32).to(device)
trajectories = torch.transpose(trajs, 0, 2)
trajectories = torch.transpose(trajectories, 1, 2)
trajectories = trajectories[:trajectories_to_sample]
t_scale = 20.0 / tm[-1]
t = t_scale * tm
if double:
t = torch.from_numpy(t).to(device).double()
else:
t = torch.from_numpy(t).to(torch.float32).to(device)
return trajectories, t
def integro_de(device,
double=False,
trajectories_to_sample=100,
t_nsamples=200):
t_end = 4.0
t_begin = t_end / t_nsamples
if double:
ti = torch.linspace(t_begin, t_end, t_nsamples).to(device).double()
else:
ti = torch.linspace(t_begin, t_end, t_nsamples).to(device)
def sampler(t, x0=0):
return ((1 / 4.0) * torch.exp(
(-1 - 2j) * t) * ((2 * x0 + (x0 - 1) * 1j) * torch.exp(4 * 1j * t) +
(2 * x0 - (x0 - 1) * 1j))).real
x0s = torch.linspace(0, 1, trajectories_to_sample)
trajs = []
for x0 in x0s:
trajs.append(sampler(ti, x0))
y = torch.stack(trajs)
trajectories = y.view(trajectories_to_sample, -1, 1)
return trajectories, ti
def spiral_dde(device,
double=False,
trajectories_to_sample=100,
t_nsamples=200):
def model(XY, t, d): # pylint: disable=invalid-name
x, y = XY(t)
xd, yd = XY(t - d)
return np.array([
-np.tanh(x + xd) + np.tanh(y + yd),
-np.tanh(x + xd) - np.tanh(y + yd),
])
subsample_to_points = t_nsamples
compute_points = 1000
tt = np.linspace(20 / compute_points, 20, compute_points)
sample_step = int(compute_points / subsample_to_points)
trajectories_list = []
evaluate_points = int(np.floor(np.sqrt(trajectories_to_sample)))
x0s1d = np.linspace(-2, 2, evaluate_points)
try:
with shelve.open("datasets") as db:
trajectories = db[f"spiral_dde_trajectories_{evaluate_points}"]
except KeyError:
for x0 in tqdm(x0s1d):
for y0 in x0s1d:
yy = ddeint(model, lambda t, x0=x0, y0=y0: np.array([x0, y0]), tt,
fargs=(2.5,))
trajectories_list.append(yy)
trajectories = np.stack(trajectories_list)
with shelve.open("datasets") as db:
db[f"spiral_dde_trajectories_{evaluate_points}"] = trajectories
trajectoriesn = trajectories[:, ::sample_step]
tt = tt[::sample_step]
if double:
trajectories = torch.from_numpy(trajectoriesn).to(device).double()
t = torch.from_numpy(tt).to(device).double()
else:
trajectories = torch.from_numpy(trajectoriesn).to(torch.float32).to(device)
t = torch.from_numpy(tt).to(torch.float32).to(device)
return trajectories, t
def lotka_volterra_system_with_delay(device,
double=False,
trajectories_to_sample=100,
t_nsamples=200):
def model(Y, t, d): # pylint: disable=invalid-name
x, y = Y(t)
xd, yd = Y(t - d)
return np.array([0.5 * x * (1 - yd), -0.5 * y * (1 - xd)])
subsample_to_points = t_nsamples
compute_points = 1000
tt = np.linspace(2, 30, compute_points)
sample_step = int(compute_points / subsample_to_points)
trajectories_list = []
evaluate_points = int(np.floor(np.sqrt(trajectories_to_sample)))
x0s1d = np.linspace(0.1, 2, evaluate_points)
try:
with shelve.open("datasets") as db:
trajectories = db["lotka_volterra_system_with_delay_trajectories"
f"_{evaluate_points}"]
except KeyError:
for x0 in tqdm(x0s1d):
for y0 in x0s1d:
yy = ddeint(model, lambda t, x0=x0, y0=y0: np.array([x0, y0]), tt,
fargs=(0.1,))
trajectories_list.append(yy)
trajectories = np.stack(trajectories_list)
with shelve.open("datasets") as db:
db["lotka_volterra_system_with_delay_trajectories"
f"_{evaluate_points}"] = trajectories
trajectoriesn = trajectories[:, ::sample_step]
tt = tt[::sample_step]
if double:
trajectories = torch.from_numpy(trajectoriesn).to(device).double()
t = torch.from_numpy(tt).to(device).double()
else:
trajectories = torch.from_numpy(trajectoriesn).to(torch.float32)
trajectories = trajectories.to(device)
t = torch.from_numpy(tt).to(torch.float32).to(device)
return trajectories, t
def mackey_glass_dde_long_term_dep(device,
double=False,
trajectories_to_sample=100,
t_nsamples=200):
n = 10
beta = 0.25
gamma = 0.1
third = 50
tau = third * 2
compute_points = 2000
tt = np.linspace(0, third, compute_points // 2)
nt = np.linspace(-third * 2, 0, compute_points // 2)
sample_step = int(compute_points / t_nsamples)
t_end = 20.0
t_begin = t_end / t_nsamples
if double:
ti = torch.linspace(t_begin, t_end, t_nsamples).to(device).double()
else:
ti = torch.linspace(t_begin, t_end, t_nsamples).to(device)
def model(Y, t, d): # pylint: disable=invalid-name
return beta * (Y(t - d) / (1 + Y(t - d)**n)) - gamma * Y(t)
def g(t, on_threshold):
if t > -on_threshold:
return 1.0
else:
return 0.0
thres = third * 2
yp = ddeint(model, partial(g, thres), tt, fargs=(tau,))
yy = [y if isinstance(y, float) else y[0] for y in yp]
ny = np.array([g(t, thres) for t in nt])
ya = np.concatenate((ny, yy))
on_thresholds = np.linspace(third * 2, 0, trajectories_to_sample)
trajs = []
for thres in tqdm(on_thresholds):
yp = ddeint(model, partial(g, thres), tt, fargs=(tau,))
yy = [y if isinstance(y, float) else y[0] for y in yp]
ny = np.array([g(t, thres) for t in nt])
ya = np.concatenate((ny, yy))
trajs.append(ya[::sample_step])
trajectoriesn = np.stack(trajs)
if double:
trajectories = torch.from_numpy(trajectoriesn).to(device).double()
else:
trajectories = torch.from_numpy(trajectoriesn).to(torch.float32)
trajectories = trajectories.to(device)
return trajectories.view(trajectories_to_sample, -1, 1), ti
# Waveform datasets
def sine(device, double=False, trajectories_to_sample=100, t_nsamples=200):
t_end = 20.0
t_begin = t_end / t_nsamples
if double:
ti = torch.linspace(t_begin, t_end, t_nsamples).to(device).double()
else:
ti = torch.linspace(t_begin, t_end, t_nsamples).to(device)
def sampler(t, x0=0):
return torch.sin(t + x0)
x0s = torch.linspace(0, 2 * torch.pi, trajectories_to_sample)
trajs = []
for x0 in x0s:
trajs.append(sampler(ti, x0))
y = torch.stack(trajs)
trajectories = y.view(trajectories_to_sample, -1, 1)
return trajectories, ti
def square(device, double=False, trajectories_to_sample=100, t_nsamples=200):
t_end = 20.0
t_begin = t_end / t_nsamples
if double:
ti = torch.linspace(t_begin, t_end, t_nsamples).to(device).double()
else:
ti = torch.linspace(t_begin, t_end, t_nsamples).to(device)
def sampler(t, x0=0):
return (1 - torch.floor((t + x0) / torch.pi) % 2) * 2
x0s = torch.linspace(0, 2 * torch.pi, trajectories_to_sample)
trajs = []
for x0 in x0s:
trajs.append(sampler(ti, x0))
y = torch.stack(trajs)
trajectories = y.view(trajectories_to_sample, -1, 1)
return trajectories, ti
def sawtooth(device, double=False, trajectories_to_sample=100, t_nsamples=200):
t_end = 20.0
t_begin = t_end / t_nsamples
if double:
ti = torch.linspace(t_begin, t_end, t_nsamples).to(device).double()
else:
ti = torch.linspace(t_begin, t_end, t_nsamples).to(device)
def sampler(t, x0=0):
return (t + x0) / (2 * torch.pi) - \
torch.floor((t + x0) / (2 * torch.pi))
x0s = torch.linspace(0, 2 * torch.pi, trajectories_to_sample)
trajs = []
for x0 in x0s:
trajs.append(sampler(ti, x0))
y = torch.stack(trajs)
trajectories = y.view(trajectories_to_sample, -1, 1)
return trajectories, ti
def generate_data_set(
name,
device,
double=False,
batch_size=128,
extrap=0,
trajectories_to_sample=100,
percent_missing_at_random=0.0,
normalize=True,
test_set_out_of_distribution=False,
noise_std=None,
t_nsamples=200,
observe_step=1,
predict_step=1,
):
if name == "dde_ramp_loading_time_sol":
trajectories, t = dde_ramp_loading_time_sol(device, double,
trajectories_to_sample,
t_nsamples)
elif name == "spiral_dde":
trajectories, t = spiral_dde(device, double, trajectories_to_sample,
t_nsamples)
elif name == "stiffvdp":
trajectories, t = stiffvdp(device, double, trajectories_to_sample)
elif name == "integro_de":
trajectories, t = integro_de(device, double, trajectories_to_sample,
t_nsamples)
elif name == "mackey_glass_dde_long_term_dep":
trajectories, t = mackey_glass_dde_long_term_dep(device, double,
trajectories_to_sample,
t_nsamples)
elif name == "lotka_volterra_system_with_delay":
trajectories, t = lotka_volterra_system_with_delay(device, double,
trajectories_to_sample,
t_nsamples)
elif name == "sine":
trajectories, t = sine(device, double, trajectories_to_sample, t_nsamples)
elif name == "square":
trajectories, t = square(device, double, trajectories_to_sample, t_nsamples)
elif name == "sawtooth":
trajectories, t = sawtooth(device, double, trajectories_to_sample,
t_nsamples)
else:
raise ValueError("Unknown Dataset To Test")
if not extrap:
bool_mask = torch.FloatTensor(
*trajectories.shape).uniform_() < (1.0 - percent_missing_at_random)
if double:
float_mask = (bool_mask).float().double().to(device)
else:
float_mask = (bool_mask).float().to(device)
trajectories = float_mask * trajectories
# normalize
if normalize:
samples = trajectories.shape[0]
dim = trajectories.shape[2]
traj = (torch.reshape(trajectories, (-1, dim)) - torch.reshape(
trajectories,
(-1, dim)).mean(0)) / torch.reshape(trajectories, (-1, dim)).std(0)
trajectories = torch.reshape(traj, (samples, -1, dim))
if noise_std:
trajectories += torch.randn(trajectories.shape).to(device) * noise_std
train_split = int(0.8 * trajectories.shape[0])
test_split = int(0.9 * trajectories.shape[0])
if test_set_out_of_distribution:
train_trajectories = trajectories[:train_split, :, :]
val_trajectories = trajectories[train_split:test_split, :, :]
test_trajectories = trajectories[test_split:, :, :]
else:
traj_index = torch.randperm(trajectories.shape[0])
train_trajectories = trajectories[traj_index[:train_split], :, :]
val_trajectories = trajectories[traj_index[train_split:test_split], :, :]
test_trajectories = trajectories[traj_index[test_split:], :, :]
test_plot_traj = test_trajectories[0, :, :]
input_dim = train_trajectories.shape[2]
output_dim = input_dim
dltrain = DataLoader(
train_trajectories,
batch_size=batch_size,
shuffle=True,
collate_fn=lambda batch: basic_collate_fn(
batch,
t,
data_type="train",
extrap=extrap,
observe_step=observe_step,
predict_step=predict_step,
),
)
dlval = DataLoader(
val_trajectories,
batch_size=batch_size,
shuffle=False,
collate_fn=lambda batch: basic_collate_fn(
batch,
t,
data_type="test",
extrap=extrap,
observe_step=observe_step,
predict_step=predict_step,
),
)
dltest = DataLoader(
test_trajectories,
batch_size=batch_size,
shuffle=False,
collate_fn=lambda batch: basic_collate_fn(
batch,
t,
data_type="test",
extrap=extrap,
observe_step=observe_step,
predict_step=predict_step,
),
)
return (
input_dim,
output_dim,
dltrain,
dlval,
dltest,
test_plot_traj,
t,
test_trajectories,
)