-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdataset.py
More file actions
288 lines (229 loc) · 10.8 KB
/
dataset.py
File metadata and controls
288 lines (229 loc) · 10.8 KB
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
import math
import torch
import numpy as np
from torch.utils.data import DataLoader, IterableDataset
import control # pip install python-control, pip install slycot (optional)
from lti import drss_matrices, dlsim
class LinearDynamicalDataset(IterableDataset):
def __init__(self, nx=5, nu=1, ny=1, seq_len=500, strictly_proper=True, dtype="float32", normalize=True):
super(LinearDynamicalDataset).__init__()
self.nx = nx
self.nu = nu
self.ny = ny
self.seq_len = seq_len
self.strictly_proper = strictly_proper
self.dtype = dtype
self.normalize = normalize
def __iter__(self):
while True: # infinite dataset
# for _ in range(1000):
sys = control.drss(states=self.nx,
inputs=self.nu,
outputs=self.ny,
strictly_proper=self.strictly_proper)
u = np.random.randn(self.nu, self.seq_len).astype(self.dtype) # C, T as python-control wants
y = control.forced_response(sys, T=None, U=u, X0=0.0)
u = u.transpose() # T, C
y = y.y.transpose().astype(self.dtype) # T, C
if self.normalize:
y = (y - y.mean(axis=0)) / (y.std(axis=0) + 1e-6)
yield torch.tensor(y), torch.tensor(u)
class LinearDynamicalDatasetNb(IterableDataset):
def __init__(self, nx=5, nu=1, ny=1, random_order=True, seq_len=500, dtype="float32", normalize=True, system_seed=None,
**mdlargs):
super(LinearDynamicalDatasetNb).__init__()
self.nx = nx
self.nu = nu
self.ny = ny
self.random_order = random_order
self.seq_len = seq_len
self.mdlargs = mdlargs # strictly_proper=True, mag_range=(0.5, 0.97), phase_range=(0, math.pi / 2)
self.dtype = dtype
self.normalize = normalize
self.system_seed = system_seed
def __iter__(self):
if self.system_seed is not None:
rng = np.random.default_rng(self.system_seed)
else:
rng = None
while True: # infinite dataset
# for _ in range(1000):
G = drss_matrices(states=np.random.randint(1, self.nx+1) if self.random_order else self.nx,
inputs=self.nu,
outputs=self.ny,
rng=rng,
**self.mdlargs)
#print(G[0])
u = np.random.randn(self.seq_len, self.nu)
y = dlsim(*G, u)
if self.normalize:
y = (y - y.mean(axis=0)) / (y.std(axis=0) + 1e-6)
u = u.astype(self.dtype)
y = y.astype(self.dtype)
yield torch.tensor(y), torch.tensor(u)
class WHDataset(IterableDataset):
def __init__(self, nx=5, nu=1, ny=1, seq_len=600, random_order=True,
strictly_proper=True, normalize=True, dtype="float32",
fixed_system=False, system_seed=None, data_seed=None, **mdlargs):
super(WHDataset).__init__()
self.nx = nx
self.nu = nu
self.ny = ny
self.seq_len = seq_len
self.strictly_proper = strictly_proper
self.dtype = dtype
self.normalize = normalize
self.strictly_proper = strictly_proper
self.random_order = random_order # random number of states from 1 to nx
self.system_seed = system_seed
self.data_seed = data_seed
self.system_rng = np.random.default_rng(system_seed) # source of randomness for model generation
self.data_rng = np.random.default_rng(data_seed) # source of randomness for model generation
self.fixed_system = fixed_system # same model at each iteration (classical identification)
self.mdlargs = mdlargs
def __iter__(self):
# A simple ff neural network
def nn_fun(x):
out = x @ w1.transpose() + b1
out = np.tanh(out)
out = out @ w2.transpose() + b2
return out
n_in = 1
n_out = 1
n_hidden = 32
n_skip = 200
if self.fixed_system: # same model at each step, generate only once!
w1 = self.system_rng.normal(size=(n_hidden, n_in)) / np.sqrt(n_in) * 5 / 3
b1 = self.system_rng.normal(size=(1, n_hidden)) * 1.0
w2 = self.system_rng.normal(size=(n_out, n_hidden)) / np.sqrt(n_hidden)
b2 = self.system_rng.normal(size=(1, n_out)) * 1.0
G1 = drss_matrices(states=self.system_rng.integers(1, self.nx+1) if self.random_order else self.nx,
inputs=1,
outputs=1,
strictly_proper=self.strictly_proper,
rng=self.system_rng,
**self.mdlargs)
G2 = drss_matrices(states=self.system_rng.integers(1, self.nx+1) if self.random_order else self.nx,
inputs=1,
outputs=1,
strictly_proper=False,
rng=self.system_rng,
**self.mdlargs)
while True: # infinite dataset
if not self.fixed_system: # different model for different instances!
w1 = self.system_rng.normal(size=(n_hidden, n_in)) / np.sqrt(n_in) * 5 / 3
b1 = self.system_rng.normal(size=(1, n_hidden)) * 1.0
w2 = self.system_rng.normal(size=(n_out, n_hidden)) / np.sqrt(n_hidden)
b2 = self.system_rng.normal(size=(1, n_out)) * 1.0
G1 = drss_matrices(states=self.system_rng.integers(1, self.nx+1) if self.random_order else self.nx,
inputs=1,
outputs=1,
strictly_proper=self.strictly_proper,
rng=self.system_rng,
**self.mdlargs)
G2 = drss_matrices(states=self.system_rng.integers(1, self.nx+1) if self.random_order else self.nx,
inputs=1,
outputs=1,
strictly_proper=False,
rng=self.system_rng,
**self.mdlargs)
#u = np.random.randn(self.seq_len + n_skip, 1) # input to be improved (filtered noise, multisine, etc)
u = self.data_rng.normal(size=(self.seq_len + n_skip, 1))
# G1
y1 = dlsim(*G1, u)
y1 = (y1 - y1[n_skip:].mean(axis=0)) / (y1[n_skip:].std(axis=0) + 1e-6)
# F
y2 = nn_fun(y1)
# G2
y3 = dlsim(*G2, y2)
u = u[n_skip:]
y = y3[n_skip:]
if self.normalize:
y = (y - y.mean(axis=0)) / (y.std(axis=0) + 1e-6)
u = u.astype(self.dtype)
y = y.astype(self.dtype)
yield torch.tensor(y), torch.tensor(u)
def seed_worker(worker_id):
worker_info = torch.utils.data.get_worker_info()
dataset = worker_info.dataset
worker_id = worker_info.id
dataset.data_rng = np.random.default_rng(dataset.data_seed + 1000*worker_id)
dataset.system_rng = np.random.default_rng(dataset.system_seed + 1000*worker_id)
class PWHDataset(IterableDataset):
def __init__(self, nx=50, nu=1, ny=1, nbr=10, seq_len=1024, random_order=True,
strictly_proper=True, normalize=True, dtype="float32", **mdlargs):
super(PWHDataset).__init__()
self.nx = nx
self.nu = nu
self.ny = ny
self.nbr = nbr
self.seq_len = seq_len
self.strictly_proper = strictly_proper
self.dtype = dtype
self.normalize = normalize
self.strictly_proper = strictly_proper
self.random_order = random_order
self.mdlargs = mdlargs
def __iter__(self):
# A simple ff neural network
def nn_fun(x):
out = x @ w1.transpose() + b1
out = np.tanh(out)
out = out @ w2.transpose() + b2
return out
while True: # infinite dataset
# for _ in range(1000):
n_in = 1
n_out = 1
n_hidden = 128
n_skip = 200
w1 = np.random.randn(n_hidden, n_in) / np.sqrt(n_in) * 1.0
b1 = np.random.randn(1, n_hidden) * 1.0
w2 = np.random.randn(n_out, n_hidden) / np.sqrt(n_hidden) * 5/3
b2 = np.random.randn(1, n_out) * 1.0
G1 = drss_matrices(states=np.random.randint(1, self.nx+1) if self.random_order else self.nx,
inputs=1,
outputs=1,
strictly_proper=self.strictly_proper,
**self.mdlargs)
G2 = drss_matrices(states=np.random.randint(1, self.nx+1) if self.random_order else self.nx,
inputs=1,
outputs=1,
strictly_proper=False,
**self.mdlargs)
# which kind of randomness for u?
u = np.random.randn(self.seq_len + n_skip, 1) # input to be improved (filtered noise, multisine, etc)
# G1
y1 = dlsim(*G1, u)
y1 = (y1 - y1[n_skip:].mean(axis=0)) / (y1[n_skip:].std(axis=0) + 1e-6)
# F
y2 = nn_fun(y1)
# G2
y3 = dlsim(*G2, y2)
u = u[n_skip:]
y = y3[n_skip:]
if self.normalize:
y = (y - y.mean(axis=0)) / (y.std(axis=0) + 1e-6)
u = u.astype(self.dtype)
y = y.astype(self.dtype)
yield torch.tensor(y), torch.tensor(u)
class MultiIterableDataSet(IterableDataset):
def __init__(self, datasets):
self.datasets = datasets
def __iter__(self):
iterators = [iter(dataset) for dataset in self.datasets]
while True:
dataset_index = torch.randint(low=0, high=len(iterators), size=(1,))[0] # or any other dataset sampling logic...
yield next(iterators[dataset_index])
if __name__ == "__main__":
# Create data loader
mdlargs = {"strictly_proper":True, "mag_range": (0.8, 0.97), "phase_range": (0, math.pi / 2)}
train_ds = LinearDynamicalDatasetNb(nx=5, nu=1, ny=1, seq_len=500, **mdlargs)
#train_ds = WHDataset(nx=2, seq_len=4, mag_range=(0.5, 0.96),
# phase_range=(0, math.pi / 3),
# system_seed=42, data_seed=445, fixed_system=False)
# train_ds = LinearDynamicalDataset(nx=5, nu=2, ny=3, seq_len=1000)
train_dl = DataLoader(train_ds, batch_size=2)
batch_y, batch_u = next(iter(train_dl))
batch_y, batch_u = next(iter(train_dl))
print(batch_u.shape, batch_u.shape)