-
Notifications
You must be signed in to change notification settings - Fork 1
/
synthetic_regression_demo.py
285 lines (205 loc) · 8.54 KB
/
synthetic_regression_demo.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
# -*- coding: utf-8 -*-
# Copyright 2024 Ifigeneia Apostolopoulou
#
# 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.
"""
Illustrate Distance-Aware Bottleneck on synthetic regression tasks.
"""
import numpy as np
from absl import app
from absl import flags
from absl import logging
import tensorflow.compat.v2 as tf
import edward2 as ed
import matplotlib.pyplot as plt
from layers import NormalFullCovarianceDAB
FLAGS = flags.FLAGS
flags.DEFINE_integer('epochs', 1500, 'Number of epochs.')
flags.DEFINE_integer('num_train_examples', 20,
'Number of training datapoints.')
flags.DEFINE_float('rdfc_learning_rate', 0.01,
'Learning rate for the codebook.')
flags.DEFINE_float('learning_rate', 0.001,
'Learning rate for main network.')
flags.DEFINE_float('beta', 1.0,
'Lagrange multiplier for DAB regularization.')
flags.DEFINE_float('dab_tau', 5.0, ' Temperature of DAB loss.')
flags.DEFINE_integer('dab_dim', 8, 'Bottleneck dimension')
flags.DEFINE_integer('example', 1,
'Example to be tested: 1. datapoints in the middle. 2: datapoints at the edges'
)
flags.DEFINE_integer('codebook_size', 1, 'Codebook size. ')
flags.DEFINE_bool('verbose', False, 'Print numerical details.')
SMALL_SIZE = 8
MEDIUM_SIZE = 10
BIGGER_SIZE = 12
plt.rc('font', size=BIGGER_SIZE) # controls default text sizes
plt.rc('axes', titlesize=BIGGER_SIZE) # fontsize of the axes title
plt.rc('axes', labelsize=MEDIUM_SIZE) # fontsize of the x and y labels
plt.rc('xtick', labelsize=MEDIUM_SIZE) # fontsize of the tick labels
plt.rc('ytick', labelsize=MEDIUM_SIZE) # fontsize of the tick labels
plt.rc('legend', fontsize=BIGGER_SIZE) # legend fontsize
plt.rc('figure', titlesize=BIGGER_SIZE) # fontsize of the figure title
def create_dataset():
"""
Create dataset for the linear regression example.
"""
if FLAGS.example == 1:
x1 = np.random.uniform(-4, -0.0, size=FLAGS.num_train_examples
// 2).reshape((-1, 1))
x2 = np.random.uniform(0.0, 4, size=FLAGS.num_train_examples
// 2).reshape((-1, 1))
else:
x1 = np.random.uniform(-5, -2.0, size=FLAGS.num_train_examples
// 2).reshape((-1, 1))
x2 = np.random.uniform(2.0, 5, size=FLAGS.num_train_examples
// 2).reshape((-1, 1))
x = np.concatenate([x1, x2])
noise = np.random.normal(0, 9,
size=FLAGS.num_train_examples).reshape((-1,
1))
y = x ** 3 + noise
x_ = np.linspace(-5, 5).reshape((-1, 1))
y_ = x_ ** 3
x = np.float32(x)
y = np.float32(y)
x_ = np.float32(x_)
y_ = np.float32(y_)
return (y, x, x_, y_)
def multilayer_perceptron():
"""
Create model.
"""
inputs = tf.keras.layers.Input(shape=1)
hidden_1 = tf.keras.layers.Dense(units=100, activation='elu'
)(inputs)
hidden_2 = tf.keras.layers.Dense(units=100, activation='elu'
)(hidden_1)
dab_output = NormalFullCovarianceDAB(
dab_dim=FLAGS.dab_dim,
codebook_size=FLAGS.codebook_size,
dab_tau=FLAGS.dab_tau,
name='dense_dab',
activation=None,
momentum=0.0,
)(hidden_2)
(latent_features, uncertainty) = tf.split(dab_output,
[FLAGS.dab_dim, 1], axis=-1)
output = tf.keras.layers.Dense(units=1,
activation=None)(latent_features)
return tf.keras.Model(inputs=inputs, outputs=tf.concat([output,
uncertainty], axis=-1))
def main(argv):
(y_train, x_train, x_test, y_test) = create_dataset()
model = multilayer_perceptron()
codebook_optimizer = \
tf.keras.optimizers.Adam(FLAGS.rdfc_learning_rate)
optimizer = tf.keras.optimizers.Adam(FLAGS.learning_rate)
@tf.function
def rdfc_step(inputs):
"""
This function computes Rate Distortion Finite Cardinality (RDFC) [1].
In this phase, the codebook (the centroids) is trained.
References
[1] Banerjee A, Dhillon I, Ghosh J, Merugu S. An information theoretic analysis of
maximum likelihood mixture estimation for exponential families. ICML 2004.
"""
def centroid_step_fn(inputs):
"""
Train the codebook.
"""
with tf.GradientTape() as tape:
outputs = model(inputs, training=True)
(_, uncertainty) = tf.split(outputs, 2, axis=-1)
loss = tf.reduce_mean(uncertainty)
grads = tape.gradient(loss, model.trainable_variables)
# train centroids
grads_and_vars = []
for (grad, var) in zip(grads,
model.trainable_variables):
if 'centroid' in var.name:
grads_and_vars.append((grad, var))
codebook_optimizer.apply_gradients(grads_and_vars)
def centroid_probs_step_fn(inputs):
"""
Gather conditional centroid probabilities (E-step) needed for
the prior centroid probabilities.
"""
model(inputs, training=True)
# flag network as initialized
model.get_layer('dense_dab'
).initialized.assign(tf.constant(True,
dtype=tf.bool))
# update centroids
model.get_layer('dense_dab').reset_codebook_covariance()
centroid_step_fn(inputs)
model.get_layer('dense_dab').set_codebook_covariance()
# update prior centroid probabilities
model.get_layer('dense_dab').reset_centroid_probs()
centroid_probs_step_fn(inputs)
model.get_layer('dense_dab').set_centroid_probs()
@tf.function
def train_step(inputs, labels):
"""
This step trains the encoder & decoder.
"""
with tf.GradientTape() as tape:
outputs = model(inputs, training=True)
(predictions, uncertainty) = tf.split(outputs, 2, axis=-1)
distribution = tf.keras.layers.Lambda(lambda x: \
ed.Normal(loc=x, scale=1.0))(predictions)
negative_log_likelihood = \
-distribution.distribution.log_prob(labels)
l2_loss = sum([l for l in model.losses])
loss = negative_log_likelihood + l2_loss + FLAGS.beta \
* tf.reduce_mean(uncertainty)
grads = tape.gradient(loss, model.trainable_variables)
# train encoder & decoder
grads_and_vars = []
for (grad, var) in zip(grads, model.trainable_variables):
if 'centroid' not in var.name:
grads_and_vars.append((grad, var))
optimizer.apply_gradients(grads_and_vars)
# =========================== training loop =========================== #
for epoch in range(FLAGS.epochs):
if FLAGS.verbose:
logging.info('Starting to train epoch: %s', epoch)
train_step(x_train, y_train)
rdfc_step(x_train)
# =========================== qualitative evaluation =========================== #
output = model.predict(x_test)
(y_pred, uncertainty) = tf.split(output, 2, axis=-1)
y_pred_low = np.squeeze(y_pred - 2 * uncertainty)
y_pred_high = np.squeeze(y_pred + 2 * uncertainty)
plt.fill_between(
np.squeeze(x_test),
y_pred_low,
y_pred_high,
color='coral',
alpha=0.5,
label='Uncertainty',
)
plt.plot(x_test, y_pred, c='royalblue', label='Prediction',
linewidth=2)
plt.scatter(x_train, y_train, c='navy', label='Train Datapoint')
plt.plot(x_test, y_test, c='grey', label='Ground Truth',
linewidth=2)
if FLAGS.example == 1:
plt.legend(loc='upper center')
plt.legend().set_visible(True)
else:
plt.legend().set_visible(False)
plt.tight_layout()
plt.show()
if __name__ == '__main__':
app.run(main)