forked from LukasRinder/normalizing-flows
-
Notifications
You must be signed in to change notification settings - Fork 0
/
toy_data.py
256 lines (209 loc) · 8.89 KB
/
toy_data.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
import numpy as np
import sklearn
import sklearn.datasets
from sklearn.utils import shuffle as util_shuffle
import tensorflow as tf
import tensorflow_probability as tfp
tfd = tfp.distributions
# Dataset iterator for generation of dataset samples
def generate_2d_data(data, rng=None, batch_size=1000):
if rng is None:
rng = np.random.RandomState()
if data == "swissroll":
data = sklearn.datasets.make_swiss_roll(n_samples=batch_size, noise=1.0)[0]
data = data.astype("float32")[:, [0, 2]]
data /= 5
return data, np.max(data)
elif data == "circles":
data = sklearn.datasets.make_circles(n_samples=batch_size, factor=.5, noise=0.08)[0]
data = data.astype("float32")
data *= 3
return data, np.max(data)
elif data == "rings":
n_samples4 = n_samples3 = n_samples2 = batch_size // 4
n_samples1 = batch_size - n_samples4 - n_samples3 - n_samples2
# so as not to have the first point = last point, we set endpoint=False
linspace4 = np.linspace(0, 2 * np.pi, n_samples4, endpoint=False)
linspace3 = np.linspace(0, 2 * np.pi, n_samples3, endpoint=False)
linspace2 = np.linspace(0, 2 * np.pi, n_samples2, endpoint=False)
linspace1 = np.linspace(0, 2 * np.pi, n_samples1, endpoint=False)
circ4_x = np.cos(linspace4)
circ4_y = np.sin(linspace4)
circ3_x = np.cos(linspace4) * 0.75
circ3_y = np.sin(linspace3) * 0.75
circ2_x = np.cos(linspace2) * 0.5
circ2_y = np.sin(linspace2) * 0.5
circ1_x = np.cos(linspace1) * 0.25
circ1_y = np.sin(linspace1) * 0.25
X = np.vstack([
np.hstack([circ4_x, circ3_x, circ2_x, circ1_x]),
np.hstack([circ4_y, circ3_y, circ2_y, circ1_y])
]).T * 3.0
X = util_shuffle(X, random_state=rng)
# Add noise
X = X + rng.normal(scale=0.08, size=X.shape)
return X.astype("float32"), np.max(X)
elif data == "moons":
data = sklearn.datasets.make_moons(n_samples=batch_size, noise=0.1)[0]
data = data.astype("float32")
data = data * 2 + np.array([-1, -0.2], dtype="float32")
return data, np.max(data)
elif data == "4gaussians":
scale = 4.
centers = [(1, 0), (-1, 0), (0, 1), (0, -1)]
centers = [(scale * x, scale * y) for x, y in centers]
dataset = []
for i in range(batch_size):
point = rng.randn(2) * 0.5
idx = rng.randint(4)
center = centers[idx]
point[0] += center[0]
point[1] += center[1]
dataset.append(point)
dataset = np.array(dataset, dtype="float32")
dataset /= 1.414
return dataset, np.max(dataset)
elif data == "8gaussians":
scale = 4.
centers = [(1, 0), (-1, 0), (0, 1), (0, -1), (1. / np.sqrt(2), 1. / np.sqrt(2)),
(1. / np.sqrt(2), -1. / np.sqrt(2)), (-1. / np.sqrt(2),
1. / np.sqrt(2)), (-1. / np.sqrt(2), -1. / np.sqrt(2))]
centers = [(scale * x, scale * y) for x, y in centers]
dataset = []
for i in range(batch_size):
point = rng.randn(2) * 0.5
idx = rng.randint(8)
center = centers[idx]
point[0] += center[0]
point[1] += center[1]
dataset.append(point)
dataset = np.array(dataset, dtype="float32")
dataset /= 1.414
return dataset, np.max(dataset)
elif data == "pinwheel":
radial_std = 0.3
tangential_std = 0.1
num_classes = 5
num_per_class = batch_size // 5
rate = 0.25
rads = np.linspace(0, 2 * np.pi, num_classes, endpoint=False)
features = rng.randn(num_classes*num_per_class, 2) \
* np.array([radial_std, tangential_std])
features[:, 0] += 1.
labels = np.repeat(np.arange(num_classes), num_per_class)
angles = rads[labels] + rate * np.exp(features[:, 0])
rotations = np.stack([np.cos(angles), -np.sin(angles), np.sin(angles), np.cos(angles)])
rotations = np.reshape(rotations.T, (-1, 2, 2))
data = 2 * rng.permutation(np.einsum("ti,tij->tj", features, rotations))
return data, np.max(data)
elif data == "2spirals":
n = np.sqrt(np.random.rand(batch_size // 2, 1)) * 540 * (2 * np.pi) / 360
d1x = -np.cos(n) * n + np.random.rand(batch_size // 2, 1) * 0.5
d1y = np.sin(n) * n + np.random.rand(batch_size // 2, 1) * 0.5
x = np.vstack((np.hstack((d1x, d1y)), np.hstack((-d1x, -d1y)))) / 3
x += np.random.randn(*x.shape) * 0.1
return np.array(x, dtype='float32'), np.max(x)
elif data == "checkerboard":
x1 = np.random.rand(batch_size) * 4 - 2
x2_ = np.random.rand(batch_size) - np.random.randint(0, 2, batch_size) * 2
x2 = x2_ + (np.floor(x1) % 2)
data = np.concatenate([x1[:, None], x2[:, None]], 1) * 2
return np.array(data, dtype="float32"), np.max(data)
elif data == "line":
x = rng.rand(batch_size) * 5 - 2.5
y = x
data = np.stack((x, y), 1)
return data, np.max(data)
elif data == "cos":
x = rng.rand(batch_size) * 5 - 2.5
y = np.sin(x) * 2.5
data = np.stack((x, y), 1)
return data, np.max(data)
elif data == "tum":
mesh = np.zeros((400,400), dtype="float32")
mesh[20:140, 100:130] = 1
mesh[70:100, 120:300] = 1
mesh[140:170, 100:300] = 1
mesh[170:220, 250:300] = 1
mesh[220:250, 100:300] = 1
mesh[250:370, 100:130] = 1
mesh[280:310, 120:300] = 1
mesh[340:370, 120:300] = 1
index = np.argwhere(mesh == 1)
coordinates = index - 200
coordinates[:,1] *= -1
coordinates = coordinates / 50
index_2 = np.random.randint(len(coordinates), size=batch_size)
dataset = np.array(coordinates[index_2,:], dtype="float32")
return dataset, np.max(dataset)
else:
data = generate_2d_data("8gaussians", rng, batch_size)
return data, np.max(data)
# distribution generator for initial distribution and sampling
def generate_2d_dist(distribution, rng=None):
if rng is None:
rng = np.random.RandomState()
if distribution == "normal":
dist = tfd.MultivariateNormalDiag(loc=tf.zeros([2], tf.float32))
return dist
elif distribution == "4gaussians":
mix = [0.25, 0.25, 0.25, 0.25]
scale = 4.0/1.414
scale_diag = [0.5/1.414, 0.5/1.414]
centers = [(1, 0), (-1, 0), (0, 1), (0, -1)]
centers = tf.cast([(scale * x, scale * y) for x, y in centers], dtype=tf.float32)
dist = tfd.Mixture(
cat=tfd.Categorical(probs=mix),
components=[
tfd.MultivariateNormalDiag(
loc=centers[0],
scale_diag=scale_diag),
tfd.MultivariateNormalDiag(
loc=centers[1],
scale_diag=scale_diag),
tfd.MultivariateNormalDiag(
loc=centers[2],
scale_diag=scale_diag),
tfd.MultivariateNormalDiag(
loc=centers[3],
scale_diag=scale_diag)
])
return dist
elif distribution == "8gaussians":
mix = [0.125, 0.125, 0.125, 0.125, 0.125, 0.125, 0.125, 0.125]
scale = 4.0/1.414
scale_diag = [0.5/1.414, 0.5/1.414]
centers = [(1, 0), (-1, 0), (0, 1), (0, -1), (1. / np.sqrt(2), 1. / np.sqrt(2)), (1. / np.sqrt(2), -1. / np.sqrt(2)),
(-1. / np.sqrt(2), 1. / np.sqrt(2)), (-1. / np.sqrt(2), -1. / np.sqrt(2))]
centers = tf.cast([(scale * x, scale * y) for x, y in centers], dtype=tf.float32)
dist = tfd.Mixture(
cat=tfd.Categorical(probs=mix),
components=[
tfd.MultivariateNormalDiag(
loc=centers[0],
scale_diag=scale_diag),
tfd.MultivariateNormalDiag(
loc=centers[1],
scale_diag=scale_diag),
tfd.MultivariateNormalDiag(
loc=centers[2],
scale_diag=scale_diag),
tfd.MultivariateNormalDiag(
loc=centers[3],
scale_diag=scale_diag),
tfd.MultivariateNormalDiag(
loc=centers[4],
scale_diag=scale_diag),
tfd.MultivariateNormalDiag(
loc=centers[5],
scale_diag=scale_diag),
tfd.MultivariateNormalDiag(
loc=centers[6],
scale_diag=scale_diag),
tfd.MultivariateNormalDiag(
loc=centers[7],
scale_diag=scale_diag)
])
return dist
else:
return generate_2d_dist("normal", rng)