-
Notifications
You must be signed in to change notification settings - Fork 0
/
normalizing_flows.py
executable file
·252 lines (197 loc) · 7.63 KB
/
normalizing_flows.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
#!/usr/bin/env python
from __future__ import print_function
import tensorflow as tf
import tensorflow_probability as tfp
import matplotlib.pyplot as plt
import numpy as np
from generate_points import create_uniform_points, create_points, visualize_data
from time import time
tfd = tfp.distributions
tfb = tfp.bijectors
settings = {
'batch_size': 1500,
'method': 'NVP',
'num_bijectors': 8,
'learning_rate': 1e-5,
'train_iters': 2e5,
'visualize_data': False,
}
class Flow(tf.keras.models.Model):
def __init__(self, **kwargs):
super(Flow, self).__init__(**kwargs)
flow = None
def call(self, *inputs):
return self.flow.bijector.forward(*inputs)
@tf.function
def train_step(self, X, optimizer):
with tf.GradientTape() as tape:
loss = -tf.reduce_mean(self.flow.log_prob(X, training=True))
gradients = tape.gradient(loss, self.trainable_variables)
optimizer.apply_gradients(zip(gradients, self.trainable_variables))
return loss
class MAF(Flow):
def __init__(self, output_dim, num_masked, **kwargs):
super(MAF, self).__init__(**kwargs)
self.output_dim = output_dim
self.num_masked = num_masked
self.bijector_fns = []
bijectors = []
for i in range(settings['num_bijectors']):
self.bijector_fns.append(tfb.masked_autoregressive_default_template(hidden_layers=[512, 512]))
bijectors.append(
tfb.MaskedAutoregressiveFlow(
shift_and_log_scale_fn=self.bijector_fns[-1]
)
)
# if i%2 == 0:
# bijectors.append(tfb.BatchNormalization())
bijectors.append(tfb.Permute(permutation=[1, 0]))
bijector = tfb.Chain(list(reversed(bijectors[:-1])))
self.flow = tfd.TransformedDistribution(
distribution=tfd.MultivariateNormalDiag(loc=[0.0, 0.0]),
bijector=bijector)
class RealNVP(Flow):
def __init__(self, output_dim, num_masked, **kwargs):
super(RealNVP, self).__init__(**kwargs)
self.output_dim = output_dim
self.num_masked = num_masked
self.bijector_fns = []
self.bijector_fn = tfp.bijectors.real_nvp_default_template(hidden_layers=[512, 512])
bijectors = []
for i in range(settings['num_bijectors']):
# Note: Must store the bijectors separately, otherwise only a single set of tf variables is created for all layers
self.bijector_fns.append(tfp.bijectors.real_nvp_default_template(hidden_layers=[512, 512]))
bijectors.append(
tfb.RealNVP(num_masked=self.num_masked,
shift_and_log_scale_fn=self.bijector_fns[-1])
)
if i % 3 == 0:
bijectors.append(tfb.BatchNormalization())
bijectors.append(tfb.Permute(permutation=[1, 0]))
bijector = tfb.Chain(list(reversed(bijectors[:-1])))
# bijector = tfb.Chain(bijectors[:-1])
self.flow = tfd.TransformedDistribution(
distribution=tfd.MultivariateNormalDiag(loc=[0.0, 0.0]),
bijector=bijector)
def plot_layers(dist, final=False):
"""
Generate samples from the base distribution and visualize the motion of the points after each
layer transformation
"""
x = dist.distribution.sample(8000)
samples = [x]
names = [dist.distribution.name]
for bijector in reversed(dist.bijector.bijectors):
x = bijector.forward(x)
samples.append(x)
names.append(bijector.name)
results = samples
X0 = results[0].numpy()
rows = 4
cols = int(len(results) / rows) + (len(results) % rows > 0)
f, arr = plt.subplots(rows, cols, figsize=(4 * cols, 4 * rows))
i = 0
# for i in range(len(results)):
for r in range(rows):
for c in range(cols):
if i >= len(results):
break
X1 = results[i].numpy()
idx = np.logical_and(X0[:, 0] < 0, X0[:, 1] < 0)
arr[r, c].scatter(X1[idx, 0], X1[idx, 1], s=5, color='red')
idx = np.logical_and(X0[:, 0] > 0, X0[:, 1] < 0)
arr[r, c].scatter(X1[idx, 0], X1[idx, 1], s=5, color='green')
idx = np.logical_and(X0[:, 0] < 0, X0[:, 1] > 0)
arr[r, c].scatter(X1[idx, 0], X1[idx, 1], s=5, color='blue')
idx = np.logical_and(X0[:, 0] > 0, X0[:, 1] > 0)
arr[r, c].scatter(X1[idx, 0], X1[idx, 1], s=5, color='black')
arr[r, c].set_xlim([-5, 5])
arr[r, c].set_ylim([-5, 5])
arr[r, c].set_title(names[i])
arr[r, c].axis('equal')
i += 1
plt.show()
if not final:
return
idx = np.logical_and(X0[:, 0] < 0, X0[:, 1] < 0)
plt.scatter(X1[idx, 0], X1[idx, 1], s=5, color='red')
idx = np.logical_and(X0[:, 0] > 0, X0[:, 1] < 0)
plt.scatter(X1[idx, 0], X1[idx, 1], s=5, color='green')
idx = np.logical_and(X0[:, 0] < 0, X0[:, 1] > 0)
plt.scatter(X1[idx, 0], X1[idx, 1], s=5, color='blue')
idx = np.logical_and(X0[:, 0] > 0, X0[:, 1] > 0)
plt.scatter(X1[idx, 0], X1[idx, 1], s=5, color='black')
plt.axis('equal')
plt.show()
def train(model, ds, optimizer, print_period=1000):
"""
Train `model` on dataset `ds` using optimizer `optimizer`,
prining the current loss every `print_period` iterations
"""
start = time()
itr = ds.__iter__()
# for i in range(int(2e5 + 1)):
for i in range(int(settings['train_iters'] + 1)):
X = next(itr)
loss = model.train_step(X, optimizer).numpy()
if i % print_period == 0:
print("{} loss: {}, {}s".format(i, loss, time() - start))
if np.isnan(loss):
break
return loss
def print_settings():
"""
display the settings used when creating the model
"""
print("Using settings:")
for k in settings.keys():
print('{}: {}'.format(k, settings[k]))
def build_model(model):
"""
Run a pass of the model to initialize the tensorflow network
"""
x = model.flow.distribution.sample(8000)
for bijector in reversed(model.flow.bijector.bijectors):
x = bijector.forward(x)
def create_dataset():
# pts = create_uniform_points(1000)
# pts = create_points('two_moons.png', 10000)
pts = create_points('BRAD.png', 10000)
if settings['visualize_data']:
visualize_data(pts)
ds = tf.data.Dataset.from_tensor_slices(pts)
ds = ds.repeat()
ds = ds.shuffle(buffer_size=9000)
ds = ds.prefetch(3 * settings['batch_size'])
ds = ds.batch(settings['batch_size'])
return ds, pts
def train_and_run_model(display=True):
print_settings()
ds, pts = create_dataset()
if settings['method'] == 'MAF':
model = MAF(output_dim=2, num_masked=1)
elif settings['method'] == 'NVP':
model = RealNVP(output_dim=2, num_masked=1)
model(pts)
build_model(model)
if display:
model.summary()
optimizer = tf.keras.optimizers.Adam(learning_rate=settings['learning_rate'])
loss = train(model, ds, optimizer)
if display:
XF = model.flow.sample(2000)
plot_layers(model.flow, final=True)
return loss
def run_statistics_trial():
"""
Runs 10 trials and reports the number of times training fails
"""
final_loss = []
for i in range(10):
print()
final_loss.append(train_model(display=False))
print("Final loss for trial {} is {}".format(i, final_loss[-1]))
print("Training failed {} of the time".format(np.sum(np.isnan(final_loss)) * 1.0 / len(final_loss)))
if __name__ == "__main__":
train_and_run_model()
# run_statistics_trial()