forked from gpapamak/maf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.py
411 lines (298 loc) · 10.6 KB
/
util.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
from itertools import izip
import os
import numpy as np
import numpy.random as rng
import theano
import theano.tensor as tt
import matplotlib.pyplot as plt
import cPickle as pickle
def isposint(n):
"""
Determines whether number n is a positive integer.
:param n: number
:return: bool
"""
return isinstance(n, int) and n > 0
def logistic(x):
"""
Elementwise logistic sigmoid.
:param x: numpy array
:return: numpy array
"""
return 1.0 / (1.0 + np.exp(-x))
def logit(x):
"""
Elementwise logit (inverse logistic sigmoid).
:param x: numpy array
:return: numpy array
"""
return np.log(x / (1.0 - x))
def disp_imdata(xs, imsize, layout=(1,1)):
"""
Displays an array of images, a page at a time. The user can navigate pages with
left and right arrows, start over by pressing space, or close the figure by esc.
:param xs: an numpy array with images as rows
:param imsize: size of the images
:param layout: layout of images in a page
:return: none
"""
num_plots = np.prod(layout)
num_xs = xs.shape[0]
idx = [0]
# create a figure with suplots
fig, axs = plt.subplots(layout[0], layout[1])
if isinstance(axs, np.ndarray):
axs = axs.flatten()
else:
axs = [axs]
for ax in axs:
ax.axes.get_xaxis().set_visible(False)
ax.axes.get_yaxis().set_visible(False)
def plot_page():
"""Plots the next page."""
ii = np.arange(idx[0], idx[0]+num_plots) % num_xs
for ax, i in zip(axs, ii):
ax.imshow(xs[i].reshape(imsize), cmap='gray', interpolation='none')
ax.set_title(str(i))
fig.canvas.draw()
def on_key_event(event):
"""Event handler after key press."""
key = event.key
if key == 'right':
# show next page
idx[0] = (idx[0] + num_plots) % num_xs
plot_page()
elif key == 'left':
# show previous page
idx[0] = (idx[0] - num_plots) % num_xs
plot_page()
elif key == ' ':
# show first page
idx[0] = 0
plot_page()
elif key == 'escape':
# close figure
plt.close(fig)
fig.canvas.mpl_connect('key_press_event', on_key_event)
plot_page()
def isdistribution(p):
"""
:param p: a vector representing a discrete probability distribution
:return: True if p is a valid probability distribution
"""
return np.all(p >= 0.0) and np.isclose(np.sum(p), 1.0)
def discrete_sample(p, n_samples=1):
"""
Samples from a discrete distribution.
:param p: a distribution with N elements
:param n_samples: number of samples
:return: vector of samples
"""
# check distribution
#assert isdistribution(p), 'Probabilities must be non-negative and sum to one.'
# cumulative distribution
c = np.cumsum(p[:-1])[np.newaxis, :]
# get the samples
r = rng.rand(n_samples, 1)
return np.sum((r > c).astype(int), axis=1)
def ess_importance(ws):
"""
Calculates the effective sample size of a set of weighted independent samples (e.g. as given by importance
sampling or sequential monte carlo). Takes as input the normalized sample weights.
"""
ess = 1.0 / np.sum(ws ** 2)
return ess
def ess_mcmc(xs):
"""
Calculates the effective sample size of a correlated sequence of samples, e.g. as given by markov chain monte
carlo.
"""
n_samples, n_dim = xs.shape
mean = np.mean(xs, axis=0)
xms = xs - mean
acors = np.zeros_like(xms)
for i in xrange(n_dim):
for lag in xrange(n_samples):
acor = np.sum(xms[:n_samples-lag, i] * xms[lag:, i]) / (n_samples - lag)
if acor <= 0.0: break
acors[lag, i] = acor
act = 1.0 + 2.0 * np.sum(acors[1:], axis=0) / acors[0]
ess = n_samples / act
return np.min(ess)
def probs2contours(probs, levels):
"""
Takes an array of probabilities and produces an array of contours at specified percentile levels
:param probs: probability array. doesn't have to sum to 1, but it is assumed it contains all the mass
:param levels: percentile levels. have to be in [0.0, 1.0]
:return: array of same shape as probs with percentile labels
"""
# make sure all contour levels are in [0.0, 1.0]
levels = np.asarray(levels)
assert np.all(levels <= 1.0) and np.all(levels >= 0.0)
# flatten probability array
shape = probs.shape
probs = probs.flatten()
# sort probabilities in descending order
idx_sort = probs.argsort()[::-1]
idx_unsort = idx_sort.argsort()
probs = probs[idx_sort]
# cumulative probabilities
cum_probs = probs.cumsum()
cum_probs /= cum_probs[-1]
# create contours at levels
contours = np.ones_like(cum_probs)
levels = np.sort(levels)[::-1]
for level in levels:
contours[cum_probs <= level] = level
# make sure contours have the order and the shape of the original probability array
contours = np.reshape(contours[idx_unsort], shape)
return contours
def plot_pdf_marginals(pdf, lims, gt=None, levels=(0.68, 0.95)):
"""
Plots marginals of a pdf, for each variable and pair of variables.
"""
if pdf.ndim == 1:
fig, ax = plt.subplots(1, 1)
xx = np.linspace(lims[0], lims[1], 200)
pp = pdf.eval(xx[:, np.newaxis], log=False)
ax.plot(xx, pp)
ax.set_xlim(lims)
ax.set_ylim([0, ax.get_ylim()[1]])
if gt is not None: ax.vlines(gt, 0, ax.get_ylim()[1], color='r')
else:
fig, ax = plt.subplots(pdf.ndim, pdf.ndim)
lims = np.asarray(lims)
lims = np.tile(lims, [pdf.ndim, 1]) if lims.ndim == 1 else lims
for i in xrange(pdf.ndim):
for j in xrange(pdf.ndim):
if i == j:
xx = np.linspace(lims[i, 0], lims[i, 1], 500)
pp = pdf.eval(xx, ii=[i], log=False)
ax[i, j].plot(xx, pp)
ax[i, j].set_xlim(lims[i])
ax[i, j].set_ylim([0, ax[i, j].get_ylim()[1]])
if gt is not None: ax[i, j].vlines(gt[i], 0, ax[i, j].get_ylim()[1], color='r')
else:
xx = np.linspace(lims[i, 0], lims[i, 1], 200)
yy = np.linspace(lims[j ,0], lims[j, 1], 200)
X, Y = np.meshgrid(xx, yy)
xy = np.concatenate([X.reshape([-1, 1]), Y.reshape([-1, 1])], axis=1)
pp = pdf.eval(xy, ii=[i, j], log=False)
pp = pp.reshape(list(X.shape))
ax[i, j].contour(X, Y, probs2contours(pp, levels), levels)
ax[i, j].set_xlim(lims[i])
ax[i, j].set_ylim(lims[j])
if gt is not None: ax[i, j].plot(gt[i], gt[j], 'r.', ms=8)
plt.show(block=False)
return fig, ax
def plot_hist_marginals(data, lims=None, gt=None):
"""
Plots marginal histograms and pairwise scatter plots of a dataset.
"""
n_bins = int(np.sqrt(data.shape[0]))
if data.ndim == 1:
fig, ax = plt.subplots(1, 1)
ax.hist(data, n_bins, normed=True)
ax.set_ylim([0, ax.get_ylim()[1]])
if lims is not None: ax.set_xlim(lims)
if gt is not None: ax.vlines(gt, 0, ax.get_ylim()[1], color='r')
else:
n_dim = data.shape[1]
fig, ax = plt.subplots(n_dim, n_dim)
ax = np.array([[ax]]) if n_dim == 1 else ax
if lims is not None:
lims = np.asarray(lims)
lims = np.tile(lims, [n_dim, 1]) if lims.ndim == 1 else lims
for i in xrange(n_dim):
for j in xrange(n_dim):
if i == j:
ax[i, j].hist(data[:, i], n_bins, normed=True)
ax[i, j].set_ylim([0, ax[i, j].get_ylim()[1]])
if lims is not None: ax[i, j].set_xlim(lims[i])
if gt is not None: ax[i, j].vlines(gt[i], 0, ax[i, j].get_ylim()[1], color='r')
else:
ax[i, j].plot(data[:, i], data[:, j], 'k.', ms=2)
if lims is not None:
ax[i, j].set_xlim(lims[i])
ax[i, j].set_ylim(lims[j])
if gt is not None: ax[i, j].plot(gt[i], gt[j], 'r.', ms=8)
plt.show(block=False)
return fig, ax
def save(data, file):
"""
Saves data to a file.
"""
f = open(file, 'w')
pickle.dump(data, f)
f.close()
def load(file):
"""
Loads data from file.
"""
f = open(file, 'r')
data = pickle.load(f)
f.close()
return data
def calc_whitening_transform(xs):
"""
Calculates the parameters that whiten a dataset.
"""
assert xs.ndim == 2, 'Data must be a matrix'
N = xs.shape[0]
means = np.mean(xs, axis=0)
ys = xs - means
cov = np.dot(ys.T, ys) / N
vars, U = np.linalg.eig(cov)
istds = np.sqrt(1.0 / vars)
return means, U, istds
def whiten(xs, params):
"""
Whitens a given dataset using the whitening transform provided.
"""
means, U, istds = params
ys = xs.copy()
ys -= means
ys = np.dot(ys, U)
ys *= istds
return ys
def select_theano_act_function(name, dtype=theano.config.floatX):
"""
Given the name of an activation function, returns a handle for the corresponding function in theano.
"""
if name == 'logistic':
clip = 15.0 if dtype == 'float32' else 19.0
f = lambda x: tt.nnet.sigmoid(tt.clip(x, -clip, clip))
elif name == 'tanh':
clip = 9.0 if dtype == 'float32' else 19.0
f = lambda x: tt.tanh(tt.clip(x, -clip, clip))
elif name == 'linear':
f = lambda x: x
elif name == 'relu':
f = tt.nnet.relu
elif name == 'softplus':
f = tt.nnet.softplus
elif name == 'softmax':
f = tt.nnet.softmax
else:
raise ValueError(name + ' is not a supported activation function type.')
return f
def copy_model_parms(source_model, target_model):
"""
Copies the parameters of source_model to target_model.
"""
for sp, tp in izip(source_model.parms, target_model.parms):
tp.set_value(sp.get_value())
def one_hot_encode(labels, n_labels):
"""
Transforms numeric labels to 1-hot encoded labels. Assumes numeric labels are in the range 0, 1, ..., n_labels-1.
"""
assert np.min(labels) >= 0 and np.max(labels) < n_labels
y = np.zeros([labels.size, n_labels])
y[xrange(labels.size), labels] = 1
return y
def make_folder(folder):
"""
Creates given folder (or path) if it doesn't exist.
"""
if not os.path.exists(folder):
os.makedirs(folder)