-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.py
260 lines (197 loc) · 7.19 KB
/
utils.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
from math import *
import numpy as np
import ot
from ot import gpu
import os
import matplotlib.pyplot as plt
import torch
plt.rcParams["figure.figsize"] = [9.0, 6.0]
plt.rcParams["axes.grid"] = False
def one_hot(labels, num_classes):
'''
Converts labels to one_hot encoding.
'''
y = torch.eye(num_classes)
return y[labels]
def display_img(x, columns=4):
'''
Displays images in a grid using matplotlib
x: numpy array containing images to display.
Must be formatted channels back.
'''
# If x has 3 color channels, then we're fine.
# However, plt.imshow doesn't work well with 1 channel
# So we reshape to remove the channel if that ever happens.
if len(x[0][0][0]) == 1:
x = np.reshape(x, x.shape[0:3])
rows=len(x)//columns
fig=plt.figure(figsize=(rows, columns))
for i in range(len(x)):
img = x[i]
fig.add_subplot(rows, columns, i+1)
plt.imshow(img, cmap='gray')
plt.show()
def save_points(inputs, answers, path, name='image', lines=False):
'''
Saves an img of points in a scatterplot using matplotlib
inputs: (list or np array) Points to plot
answers: (list or np array) Points to plot
path: (str) Where to save the points
name: (str) Name of the image file
lines: (bool) Whether to draw lines from inputs[i] to answers[i]
'''
plt.scatter(inputs[:,0], inputs[:,1], color='blue')
plt.scatter(answers[:,0], answers[:,1], color='green')
if lines:
for i in range(512):
plt.plot([inputs[i][0], answers[i][0]], [inputs[i][1], answers[i][1]], color='red')
file_path = os.path.join(path, "{}.png".format(name))
plt.savefig(file_path)
plt.clf()
def make_moons(train=60000, test=10000, noise=0.05):
'''
Makes the moons datasets. See
https://scikit-learn.org/stable/modules/generated/sklearn.datasets.make_moons.html
'''
moons_train, train_labels = sklearn.datasets.make_moons(n_samples=train, noise=noise)
moons_test, test_labels = sklearn.datasets.make_moons(n_samples=test, noise=noise)
return moons_train, moons_test
def make_circles(train=60000, test=10000, noise=0.05):
'''
Makes the circles datasets. See
https://scikit-learn.org/stable/modules/generated/sklearn.datasets.make_circles.html
'''
moon_train, _ = sklearn.datasets.make_circles(n_samples=train, noise=noise, factor=0.5)
moon_test, _ = sklearn.datasets.make_circles(n_samples=test, noise=noise, factor=0.5)
return moon_train, moon_test
def two_cluster(train=60000, test=10000, noise_scale=0.1):
'''
Makes a toy dataset with two clusters. Clusters are centered
at (1,0), (-1,0), are normal, and have a stdev = noise_scale.
'''
d = {0: (1, 0), 1: (-1, 0)}
x_train = []
x_test = []
for i in range(60000):
noise = noise_scale * np.random.normal(size=2)
noisex = noise[0]
noisey = noise[1]
center = d[i%2]
l = [center[0] + noisex, center[1] + noisey]
x_train.append(l)
for i in range(10000):
noise = noise_scale * np.random.normal(size=2)
noisex = noise[0]
noisey = noise[1]
center = d[i%2]
l = [center[0] + noisex, center[1] + noisey]
x_test.append(l)
return np.array(x_train), np.array(x_test)
def eight_cluster(train=60000, test=10000, noise_scale=0.1):
'''
Makes a toy dataset with eight clusters. Clusters are centered
evenly around the origin two away from it. Clusters are normal
and stdev of clusters = noise_scale
'''
x_train = []
x_test = []
d = {0: (2, 0), 1: (sqrt(2), sqrt(2)), 2: (0, 2), 3: (-sqrt(2), sqrt(2)), \
4: (-2, 0), 5: (-sqrt(2), -sqrt(2)), 6: (0, -2), 7: (sqrt(2), -sqrt(2))}
for i in range(60000):
noise = 0.1 * np.random.normal(size=2)
noisex = noise[0]
noisey = noise[1]
center = d[i%8]
l = [center[0] + noisex, center[1] + noisey]
x_train.append(l)
for i in range(10000):
noise = 0.1 * np.random.normal(size=2)
noisex = noise[0]
noisey = noise[1]
center = d[i%8]
l = [center[0] + noisex, center[1] + noisey]
x_test.append(l)
return np.array(x_train), np.array(x_test)
def optimal_transport(inputs, encodings, log=False):
'''
Computes optimal transport for one batch.
inputs (np array): Inputs of the optimal transport mapping
encodings (np array): Outputs of the optimal transport mapping
return answers: Index array. encodings[answers[0]] corresponds with
inputs[0]
'''
batch_size = len(inputs)
a = np.ones((batch_size, ))
b = np.ones((batch_size, ))
M = ot.dist(inputs, encodings)
M = np.array(M)
if log:
mapping, log = ot.emd(a, b, M, numItermax=1000000)
else:
mapping = ot.emd(a, b, M, numItermax=1000000)
answers = []
for j in range(batch_size):
index = np.argmax(mapping[j])
answers.append(index)
answers = np.array(answers)
if log:
return answers, log
else:
return answers
def sinkhorn_transport(inputs, encodings, reg):
'''
'''
batch_size = len(inputs)
a = np.ones((batch_size, ))
b = np.ones((batch_size, ))
import pudb; pudb.set_trace()
M = gpu.dist(inputs, encodings)
#M = 10*M
mapping = gpu.sinkhorn(a, b, M, reg)
answers = []
for j in range(batch_size):
index = np.argmax(mapping[j])
answers.append(index)
answers = np.array(answers)
return answers
def unload(dataloader):
'''
Unloads the dataloader fully, into a numpy array.
'''
data_iter = iter(dataloader)
x = next(data_iter)[0].numpy()
while True:
try:
x_batch = next(data_iter)[0].numpy()
x = np.concatenate((x, x_batch), 0)
except StopIteration:
break
return x
def reg_stdev(generated, answers, lambda_=1):
gen_stdev = generated.std(0)
ans_stdev = answers.std(0)
return lambda_ * torch.abs(gen_stdev - ans_stdev).mean()
def reg_cyclic(generated, answers, lambda_=1):
rand1 = np.random.choice(range(generated.shape[0]), 1000)
rand2 = np.random.choice(range(generated.shape[0]), 1000)
rand3 = np.random.choice(range(generated.shape[0]), 1000)
rand4 = np.random.choice(range(generated.shape[0]), 1000)
# total implies .mean(), separated_along_dimension implies .mean(0)
generated_cyclic = torch.abs(generated[rand1] - generated[rand2]).mean(0)
real_cyclic = torch.abs(answers[rand3] - answers[rand4]).mean(0)
return lambda_ * torch.abs(generated_cyclic - real_cyclic).mean()
def unbounded_cyclic(generated, lambda_=1):
rand1 = np.random.choice(range(generated.shape[0]), 1000)
rand2 = np.random.choice(range(generated.shape[0]), 1000)
generated_cyclic = torch.abs(generated[rand1] - generated[rand2]).mean(0)
return - lambda_ * torch.abs(generated_cyclic).mean()
class View(torch.nn.Module):
'''
With this, we can reshape tensors from within
the middle of a Sequential object
'''
def __init__(self, *args):
super(View, self).__init__()
self.shape = args
def forward(self, x):
return x.view(self.shape)