forked from google/flax
-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
204 lines (150 loc) · 5.37 KB
/
train.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
# Copyright 2020 The Flax Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from absl import app
from absl import flags
import numpy as np
import jax.numpy as jnp
import jax
from jax import random
from flax import nn
from flax import optim
import tensorflow as tf
import tensorflow_datasets as tfds
from utils import save_image
FLAGS = flags.FLAGS
flags.DEFINE_float(
'learning_rate', default=1e-3,
help=('The learning rate for the Adam optimizer.')
)
flags.DEFINE_integer(
'batch_size', default=128,
help=('Batch size for training.')
)
flags.DEFINE_integer(
'num_epochs', default=30,
help=('Number of training epochs.')
)
flags.DEFINE_integer(
'latents', default=20,
help=('Number of latent variables.')
)
class Encoder(nn.Module):
def apply(self, x, latents):
x = nn.Dense(x, 500, name='fc1')
x = nn.relu(x)
mean_x = nn.Dense(x, latents, name='fc2_mean')
logvar_x = nn.Dense(x, latents, name='fc2_logvar')
return mean_x, logvar_x
class Decoder(nn.Module):
def apply(self, z):
z = nn.Dense(z, 500, name='fc1')
z = nn.relu(z)
z = nn.Dense(z, 784, name='fc2')
return z
class VAE(nn.Module):
def apply(self, x, z_rng, latents=20):
decoder = self._create_decoder()
mean, logvar = Encoder(x, latents, name='encoder')
z = reparameterize(z_rng, mean, logvar)
recon_x = decoder(z)
return recon_x, mean, logvar
@nn.module_method
def generate(self, z, **unused_kwargs):
decoder = self._create_decoder()
return nn.sigmoid(decoder(z))
def _create_decoder(self):
return Decoder.shared(name='decoder')
def reparameterize(rng, mean, logvar):
std = jnp.exp(0.5 * logvar)
eps = random.normal(rng, logvar.shape)
return mean + eps * std
@jax.vmap
def kl_divergence(mean, logvar):
return -0.5 * jnp.sum(1 + logvar - jnp.square(mean) - jnp.exp(logvar))
@jax.vmap
def binary_cross_entropy_with_logits(logits, labels):
logits = nn.log_sigmoid(logits)
return -jnp.sum(labels * logits + (1. - labels) * jnp.log(-jnp.expm1(logits)))
def compute_metrics(recon_x, x, mean, logvar):
bce_loss = binary_cross_entropy_with_logits(recon_x, x).mean()
kld_loss = kl_divergence(mean, logvar).mean()
return {
'bce': bce_loss,
'kld': kld_loss,
'loss': bce_loss + kld_loss
}
@jax.jit
def train_step(optimizer, batch, z_rng):
def loss_fn(model):
recon_x, mean, logvar = model(batch, z_rng)
bce_loss = binary_cross_entropy_with_logits(recon_x, batch).mean()
kld_loss = kl_divergence(mean, logvar).mean()
loss = bce_loss + kld_loss
return loss, recon_x
grad_fn = jax.value_and_grad(loss_fn, has_aux=True)
_, grad = grad_fn(optimizer.target)
optimizer = optimizer.apply_gradient(grad)
return optimizer
@jax.jit
def eval(model, images, z, z_rng):
recon_images, mean, logvar = model(images, z_rng)
comparison = jnp.concatenate([images[:8].reshape(-1, 28, 28, 1),
recon_images[:8].reshape(-1, 28, 28, 1)])
generate_images = model.generate(z)
generate_images = generate_images.reshape(-1, 28, 28, 1)
metrics = compute_metrics(recon_images, images, mean, logvar)
return metrics, comparison, generate_images
def prepare_image(x):
x = tf.cast(x['image'], tf.float32)
x = tf.reshape(x, (-1,))
return x
def main(argv):
del argv
rng = random.PRNGKey(0)
rng, key = random.split(rng)
ds_builder = tfds.builder('binarized_mnist')
ds_builder.download_and_prepare()
train_ds = ds_builder.as_dataset(split=tfds.Split.TRAIN)
train_ds = train_ds.map(prepare_image)
train_ds = train_ds.cache()
train_ds = train_ds.repeat()
train_ds = train_ds.shuffle(50000)
train_ds = train_ds.batch(FLAGS.batch_size)
train_ds = tfds.as_numpy(train_ds)
test_ds = ds_builder.as_dataset(split=tfds.Split.TEST)
test_ds = test_ds.map(prepare_image).batch(10000)
test_ds = np.array(list(test_ds)[0])
test_ds = jax.device_put(test_ds)
module = VAE.partial(latents=FLAGS.latents)
_, params = module.init_by_shape(
key, [(FLAGS.batch_size, 784)], z_rng=random.PRNGKey(0))
vae = nn.Model(module, params)
optimizer = optim.Adam(learning_rate=FLAGS.learning_rate).create(vae)
optimizer = jax.device_put(optimizer)
rng, z_key, eval_rng = random.split(rng, 3)
z = random.normal(z_key, (64, FLAGS.latents))
steps_per_epoch = 50000 // FLAGS.batch_size
for epoch in range(FLAGS.num_epochs):
for _ in range(steps_per_epoch):
batch = next(train_ds)
rng, key = random.split(rng)
optimizer = train_step(optimizer, batch, key)
metrics, comparison, sample = eval(optimizer.target, test_ds, z, eval_rng)
save_image(comparison, f'results/reconstruction_{epoch}.png', nrow=8)
save_image(sample, f'results/sample_{epoch}.png', nrow=8)
print('eval epoch: {}, loss: {:.4f}, BCE: {:.4f}, KLD: {:.4f}'.format(
epoch + 1, metrics['loss'], metrics['bce'], metrics['kld']
))
if __name__ == '__main__':
app.run(main)