-
Notifications
You must be signed in to change notification settings - Fork 320
/
Copy pathsynthetic.py
268 lines (219 loc) · 7.12 KB
/
synthetic.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
"""Synthetic datasets."""
import numpy as np
import torch
import torchvision
from einops.layers.torch import Rearrange
from src.utils import permutations
from src.dataloaders.base import SequenceDataset
class Copying(SequenceDataset):
_name_ = "copying"
@property
def init_defaults(self):
return {
"l_noise": 100, # number of padding tokens
"l_memorize": 10, # number of tokens to memorize
"n_tokens": 10, # alphabet size
"lag": False,
"variable": False, # Randomly distribute memorization tokens throughout sequence instead of frontloading them
"variable_length": False, # Randomize number of tokens to memorize
"one_hot": False,
"reverse": False,
"static": False, # Use a static dataset of size n_train, otherwise always use random data with n_train per epoch
"n_train": 10000,
"n_eval": 1000,
}
@property
def d_input(self):
return self.n_tokens
@property
def d_output(self):
return self.n_tokens
@property
def l_output(self):
return self.l_noise if self.lag else self.l_memorize
def setup(self):
from .datasets.copying import CopyingEvalDataset, CopyingTrainDataset
if self.static: train_cls = CopyingEvalDataset
else: train_cls = CopyingTrainDataset
self.dataset_train = train_cls(
self.l_noise,
self.l_memorize,
self.n_tokens,
samples=self.n_train,
lag=self.lag,
variable=self.variable,
one_hot=self.one_hot,
reverse=self.reverse,
)
self.dataset_val = CopyingEvalDataset(
self.l_noise,
self.l_memorize,
self.n_tokens,
samples=self.n_eval,
lag=self.lag,
variable=self.variable,
one_hot=self.one_hot,
reverse=self.reverse,
)
self.dataset_test = None
def __str__(self):
return f"{self._name_}{self.l_noise}{'v' if self.variable else ''}"
class Adding(SequenceDataset):
_name_ = "adding"
d_input = 2
d_output = 1
l_output = 0
@property
def init_defaults(self):
return {
"l_max": 1000,
"n_samples": 50000,
"val_split": 0.1,
}
def setup(self):
from .datasets.adding import adding_static_dataset
self.dataset_train = adding_static_dataset(self.l_max, self.n_samples)
self.dataset_test = None
self.split_train_val(self.val_split)
def __str__(self):
return f"{self._name_}{self.l_max}"
class Reconstruct(SequenceDataset):
_name_ = "reconstruct"
@property
def init_defaults(self):
return {
"l_seq": 1024, # length of total sequence
"l_mem": 512, # length to reconstruct
"dt": 0.001,
"freq": 1.0,
"seed": 0,
"static": False, # Use a static dataset of size n_train, otherwise always use random data with n_train per epoch
"n_train": 10000,
"n_eval": 1000,
}
@property
def d_input(self):
return 1
@property
def d_output(self):
return self.l_mem
@property
def l_output(self):
return 0
def setup(self):
from .datasets.reconstruct import ReconstructEvalDataset, ReconstructTrainDataset
if self.static: train_cls = ReconstructEvalDataset
else: train_cls = ReconstructTrainDataset
self.dataset_train = train_cls(
samples=self.n_train,
l_seq=self.l_seq,
l_mem=self.l_mem,
dt=self.dt,
freq=self.freq,
seed=self.seed,
)
self.dataset_val = ReconstructEvalDataset(
samples=self.n_eval,
l_seq=self.l_seq,
l_mem=self.l_mem,
dt=self.dt,
freq=self.freq,
seed=self.seed,
)
self.dataset_test = None
def __str__(self):
raise NotImplementedError
class Delay(SequenceDataset):
_name_ = "delay"
@property
def init_defaults(self):
return {
"l_seq": 1024, # length of total sequence
"n_lag": 1, # length to reconstruct
"l_lag": None, # length to reconstruct
"dt": 0.001,
"freq": 100.0,
"static": False, # Use a static dataset of size n_train, otherwise always use random data with n_train per epoch
"n_train": 10000,
"n_eval": 1000,
}
@property
def d_input(self):
return 1
@property
def d_output(self):
# NOTE: To reproduce numbers from HTTYH paper, set this equal to 4. There was a bug in the implementation at the time
return self.n_lag
@property
def l_output(self):
return self.l_seq
def setup(self):
from .datasets.delay import DelayEvalDataset, DelayTrainDataset
if self.static: train_cls = DelayEvalDataset
else: train_cls = DelayTrainDataset
self.dataset_train = train_cls(
samples=self.n_train,
l_seq=self.l_seq,
n_lag=self.n_lag,
l_lag=self.l_lag,
dt=self.dt,
freq=self.freq,
)
self.dataset_val = DelayEvalDataset(
samples=self.n_eval,
l_seq=self.l_seq,
n_lag=self.n_lag,
l_lag=self.l_lag,
dt=self.dt,
freq=self.freq,
)
self.dataset_test = None
def __str__(self):
return f"{self._name_}{self.l_noise}{'v' if self.variable else ''}"
class MackeyGlass(SequenceDataset):
_name_ = "mackey"
@property
def init_defaults(self):
return {
"l_seq": 5000, # length of total sequence
"l_predict": 15, # length to reconstruct
"tau": 17, # Delay of MG system
"washout": 100,
"delta_t": 10,
"n_train": 1024,
"n_eval": 64,
}
@property
def d_input(self):
return 1
@property
def d_output(self):
return 1
@property
def l_output(self):
return self.l_seq
def setup(self):
from .datasets.mackey import mackey_glass
train_X, train_Y = mackey_glass(
n_samples=self.n_train,
l_seq=self.l_seq,
l_predict=self.l_predict,
tau=self.tau,
washout=self.washout,
delta_t=self.delta_t,
)
train_X, train_Y = torch.FloatTensor(train_X), torch.FloatTensor(train_Y)
val_X, val_Y = mackey_glass(
n_samples=self.n_eval,
l_seq=self.l_seq,
l_predict=self.l_predict,
tau=self.tau,
washout=self.washout,
delta_t=self.delta_t,
)
val_X, val_Y = torch.FloatTensor(val_X), torch.FloatTensor(val_Y)
self.dataset_train = torch.utils.data.TensorDataset(train_X, train_Y)
self.dataset_val = torch.utils.data.TensorDataset(val_X, val_Y)
self.dataset_test = None
def __str__(self):
return f"{self._name_}"