-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnormalizing_flows.py
More file actions
executable file
·397 lines (320 loc) · 13.4 KB
/
Copy pathnormalizing_flows.py
File metadata and controls
executable file
·397 lines (320 loc) · 13.4 KB
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
#!/usr/bin/env python
from __future__ import print_function
import os
# Suppress the wrapt C-extension warning that fires on Python 3.12
os.environ.setdefault("WRAPT_DISABLE_EXTENSIONS", "1")
import tensorflow as tf
import tensorflow_probability as tfp
# TFP requires Keras 2 (tf_keras), not Keras 3 bundled with TF 2.16+.
# tf_keras is installed automatically by `tensorflow-probability[tf]`.
import tf_keras
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
# Allow TF to grow GPU memory incrementally rather than claiming it all at once.
# Important on a shared GPU (e.g. one also driving the display).
for _gpu in tf.config.list_physical_devices('GPU'):
tf.config.experimental.set_memory_growth(_gpu, True)
def make_nvp_network(hidden_units, output_units):
"""Return a Keras model used as the shift-and-log-scale network for RealNVP."""
return tf_keras.Sequential([
tf_keras.layers.Dense(h, activation="relu") for h in hidden_units
] + [tf_keras.layers.Dense(output_units * 2)])
def nvp_shift_and_log_scale_fn(hidden_units, output_units):
"""
Factory that returns a (callable, network) pair for tfb.RealNVP's
shift_and_log_scale_fn. Each call creates a fresh set of Keras weights,
so call this once per bijector layer. The returned network must be stored
on the model so its variables are tracked for gradient updates.
"""
net = make_nvp_network(hidden_units, output_units)
def fn(x, input_depth=None, **kwargs):
out = net(x)
shift, log_scale = tf.split(out, 2, axis=-1)
return shift, log_scale
return fn, net
settings = {
'batch_size': 1500,
'method': 'NVP',
'num_bijectors': 8,
'learning_rate': 1e-4,
'train_iters': 2e5,
'visualize_data': False,
'print_period': 1000,
'plot_period': 500,
'plot_axis_limit': 4.0, # fixed axis bounds for all frames so video is stable
}
class Flow(tf_keras.Model):
def __init__(self, **kwargs):
super(Flow, self).__init__(**kwargs)
self.flow = None
# Keras networks backing the bijectors — tracked for trainable_variables
self._bijector_nets = []
def call(self, *inputs):
return self.flow.bijector.forward(*inputs)
@property
def trainable_variables(self):
# Collect variables from all backing networks
vars_ = []
seen = set()
for net in self._bijector_nets:
for v in net.trainable_variables:
if id(v) not in seen:
seen.add(id(v))
vars_.append(v)
return vars_
@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)
gradients, _ = tf.clip_by_global_norm(gradients, 1.0)
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
bijectors = []
for i in range(settings['num_bijectors']):
# AutoregressiveNetwork is the modern, Keras-native replacement for
# masked_autoregressive_default_template
net = tfb.AutoregressiveNetwork(params=2, hidden_units=[512, 512], activation="relu")
self._bijector_nets.append(net)
bijectors.append(
tfb.MaskedAutoregressiveFlow(shift_and_log_scale_fn=net)
)
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
bijectors = []
for i in range(settings['num_bijectors']):
# Each layer needs its own network; store nets so their variables are tracked.
# Note: Must store the bijectors separately, otherwise only a single set of
# tf variables is created for all layers.
fn, net = nvp_shift_and_log_scale_fn(hidden_units=[512, 512], output_units=num_masked)
self._bijector_nets.append(net)
bijectors.append(
tfb.RealNVP(num_masked=self.num_masked, shift_and_log_scale_fn=fn)
)
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)
def plot_layers(dist, final=False, save_path=None, step=None):
"""
Generate samples from the base distribution and visualize the motion of the points after each
layer transformation. If save_path is given, save to file instead of displaying.
All subplots use fixed axis bounds (settings['plot_axis_limit']) so frames are stable in video.
"""
lim = settings['plot_axis_limit']
# Fixed seed so the same base points are used every frame — eliminates
# random jitter between frames in the training video.
tf.random.set_seed(42)
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))
step_str = 'Step {:,}'.format(step) if step is not None else ''
info = 'Normalizing Flows (RealNVP) | {} | bijectors={} lr={} batch={}'.format(
step_str, settings['num_bijectors'], settings['learning_rate'], settings['batch_size'])
f.suptitle(info, fontsize=11, fontweight='bold')
i = 0
for r in range(rows):
for c in range(cols):
ax = arr[r, c]
if i >= len(results):
ax.axis('off')
continue
X1 = results[i].numpy()
idx = np.logical_and(X0[:, 0] < 0, X0[:, 1] < 0)
ax.scatter(X1[idx, 0], X1[idx, 1], s=5, color='red')
idx = np.logical_and(X0[:, 0] > 0, X0[:, 1] < 0)
ax.scatter(X1[idx, 0], X1[idx, 1], s=5, color='green')
idx = np.logical_and(X0[:, 0] < 0, X0[:, 1] > 0)
ax.scatter(X1[idx, 0], X1[idx, 1], s=5, color='blue')
idx = np.logical_and(X0[:, 0] > 0, X0[:, 1] > 0)
ax.scatter(X1[idx, 0], X1[idx, 1], s=5, color='black')
ax.set_xlim([-lim, lim])
ax.set_ylim([-lim, lim])
ax.set_aspect('equal')
ax.set_title(names[i])
i += 1
plt.tight_layout()
if save_path:
f.savefig(save_path, dpi=100)
plt.close(f)
else:
plt.show()
if not final:
return
fig2, ax2 = plt.subplots()
X1 = results[-1].numpy()
idx = np.logical_and(X0[:, 0] < 0, X0[:, 1] < 0)
ax2.scatter(X1[idx, 0], X1[idx, 1], s=5, color='red')
idx = np.logical_and(X0[:, 0] > 0, X0[:, 1] < 0)
ax2.scatter(X1[idx, 0], X1[idx, 1], s=5, color='green')
idx = np.logical_and(X0[:, 0] < 0, X0[:, 1] > 0)
ax2.scatter(X1[idx, 0], X1[idx, 1], s=5, color='blue')
idx = np.logical_and(X0[:, 0] > 0, X0[:, 1] > 0)
ax2.scatter(X1[idx, 0], X1[idx, 1], s=5, color='black')
ax2.set_xlim([-lim, lim])
ax2.set_ylim([-lim, lim])
ax2.set_aspect('equal')
if step is not None:
ax2.set_title('Step {:,}'.format(step))
if save_path:
final_path = save_path.replace('.png', '_final.png')
fig2.savefig(final_path, dpi=100)
plt.close(fig2)
else:
plt.show()
def make_checkpoint_manager(model, optimizer, checkpoint_dir='checkpoints'):
"""
Create a tf.train.Checkpoint + CheckpointManager for the model's networks,
optimizer state, and a global step counter. Keeps the 3 most recent checkpoints.
"""
global_step = tf.Variable(0, trainable=False, dtype=tf.int64, name='global_step')
ckpt_kwargs = {f'net_{i}': net for i, net in enumerate(model._bijector_nets)}
ckpt_kwargs['optimizer'] = optimizer
ckpt_kwargs['global_step'] = global_step
ckpt = tf.train.Checkpoint(**ckpt_kwargs)
manager = tf.train.CheckpointManager(ckpt, checkpoint_dir, max_to_keep=3)
return ckpt, manager, global_step
def restore_if_available(ckpt, manager):
"""Restore the latest checkpoint if one exists. Returns True if restored."""
if manager.latest_checkpoint:
ckpt.restore(manager.latest_checkpoint).expect_partial()
print("Restored checkpoint: {}".format(manager.latest_checkpoint))
return True
print("No checkpoint found, starting from scratch.")
return False
def train(model, ds, optimizer):
"""
Train `model` on dataset `ds` using optimizer `optimizer`.
- Prints loss every settings['print_period'] steps.
- Saves a layer-visualization PNG and checkpoint every settings['plot_period'] steps.
- Uses a persistent global_step so filenames and logs are continuous across restarts.
Loss tensor stays on GPU between syncs to avoid CPU-GPU sync overhead.
"""
print_period = settings['print_period']
plot_period = settings['plot_period']
total_iters = int(settings['train_iters'])
os.makedirs('training_progress', exist_ok=True)
ckpt, manager, global_step = make_checkpoint_manager(model, optimizer)
restore_if_available(ckpt, manager)
start_step = int(global_step.numpy())
if start_step >= total_iters:
print("Already trained for {} steps, nothing to do.".format(start_step))
return None
print("Resuming from step {}.".format(start_step))
start = time()
itr = ds.__iter__()
loss = None
for i in range(start_step, total_iters + 1):
X = next(itr)
loss = model.train_step(X, optimizer)
global_step.assign(i)
if i % print_period == 0:
loss_val = loss.numpy()
print("{} loss: {}, {:.1f}s".format(i, loss_val, time() - start))
if np.isnan(loss_val):
break
if i % plot_period == 0:
manager.save()
save_path = 'training_progress/step_{:07d}.png'.format(i)
plot_layers(model.flow, save_path=save_path, step=i)
return loss.numpy()
def print_settings():
"""
display the settings used when creating the model
"""
gpus = tf.config.list_physical_devices('GPU')
if gpus:
print("GPU: {}".format(gpus[0].name))
else:
print("WARNING: No GPU detected, training on CPU")
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'], jit_compile=False)
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__":
import argparse
import shutil
parser = argparse.ArgumentParser(description='Normalizing Flows (RealNVP) on 2D data.')
parser.add_argument('--start-new', action='store_true',
help='Delete existing checkpoints and training images, then start fresh.')
args = parser.parse_args()
if args.start_new:
for path in ('checkpoints', 'training_progress'):
if os.path.exists(path):
shutil.rmtree(path)
print("Deleted: {}".format(path))
train_and_run_model()
# run_statistics_trial()