-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmiwae.py
More file actions
386 lines (319 loc) · 17 KB
/
miwae.py
File metadata and controls
386 lines (319 loc) · 17 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
# -*- coding: utf-8 -*-
# This code is an adaptation of the code provided by P.A. Mattei (https://github.com/pamattei/miwae)
from absl import logging
import tensorflow as tf
import numpy as np
import scipy.stats
import scipy.io
import scipy.sparse
from scipy.io import loadmat
import pandas as pd
import tensorflow_probability as tfp
tfd = tfp.distributions
tfk = tf.keras
tfkl = tf.keras.layers
from PIL import Image
import matplotlib.pyplot as plt
from sklearn.metrics import f1_score
from sklearn import preprocessing
import pandas as pd
from sklearn.ensemble import ExtraTreesRegressor
from sklearn.experimental import enable_iterative_imputer
from sklearn.linear_model import BayesianRidge
from sklearn.impute import IterativeImputer
from sklearn.impute import SimpleImputer
from sklearn.model_selection import ShuffleSplit
import os
import pickle
def miwae(X_miss, d_miwae=3, h_miwae=128, add_mask=False, mu_prior=0, sig_prior = 1,
num_samples_zmul=200, l_rate = 0.0001, n_epochs = 602, add_wy = False, w = None, y = None):
np.random.seed(1234)
tf.set_random_seed(1234)
n = X_miss.shape[0] # number of observations
p = X_miss.shape[1] # number of features
pwy = 0
if add_wy:
pwy = 2
X_miss = np.column_stack([X_miss, w, y])
X_miss = np.copy(X_miss)
mask = np.isfinite(X_miss) # binary mask that indicates which values are missing
# ##########
xhat_0 = np.copy(X_miss)
xhat_0[np.isnan(X_miss)] = 0
p_mod = p
if add_mask:
mask_mod = np.copy(mask)
xhat_0 = np.concatenate((xhat_0, mask_mod), axis=1)
mask = np.concatenate((mask, np.ones_like(mask).astype(bool)), axis = 1)
p = p*2
pwy = pwy*2
# ##########
x = tf.placeholder(tf.float32, shape=[None, p+pwy]) # Placeholder for xhat_0
learning_rate = tf.placeholder(tf.float32, shape=[])
batch_size = tf.shape(x)[0]
xmask = tf.placeholder(tf.bool, shape=[None, p+pwy])
K= tf.placeholder(tf.int32, shape=[]) # Placeholder for the number of importance weights
# ##########
p_z = tfd.MultivariateNormalDiag(loc=mu_prior+tf.zeros(d_miwae, tf.float32),
scale_diag = sig_prior*tf.ones(d_miwae, tf.float32))
# ##########
sigma = "relu"
decoder = tfk.Sequential([
tfkl.InputLayer(input_shape=[d_miwae,]),
tfkl.Dense(h_miwae, activation=sigma,kernel_initializer="orthogonal"),
tfkl.Dense(h_miwae, activation=sigma,kernel_initializer="orthogonal"),
tfkl.Dense(3*(p+pwy),kernel_initializer="orthogonal") # the decoder will output both the mean, the scale, and the number of degrees of freedoms (hence the 3*p)
])
# ##########
tiledmask = tf.tile(xmask,[K,1])
tiledmask_float = tf.cast(tiledmask,tf.float32)
mask_not_float = tf.abs(-tf.cast(xmask,tf.float32))
iota = tf.Variable(np.zeros([1,p+pwy]),dtype=tf.float32)
tilediota = tf.tile(iota,[batch_size,1])
iotax = x + tf.multiply(tilediota,mask_not_float)
# ##########
encoder = tfk.Sequential([
tfkl.InputLayer(input_shape=[p+pwy,]),
tfkl.Dense(h_miwae, activation=sigma,kernel_initializer="orthogonal"),
tfkl.Dense(h_miwae, activation=sigma,kernel_initializer="orthogonal"),
tfkl.Dense(3*d_miwae,kernel_initializer="orthogonal")
])
# ##########
out_encoder = encoder(iotax)
q_zgivenxobs = tfd.Independent(distribution=tfd.StudentT(loc=out_encoder[..., :d_miwae], scale=tf.nn.softplus(out_encoder[..., d_miwae:(2*d_miwae)]), df=3 + tf.nn.softplus(out_encoder[..., (2*d_miwae):(3*d_miwae)])))
zgivenx = q_zgivenxobs.sample(K)
zgivenx_flat = tf.reshape(zgivenx,[K*batch_size,d_miwae])
data_flat = tf.reshape(tf.tile(x,[K,1]),[-1,1])
# ##########
out_decoder = decoder(zgivenx_flat)
all_means_obs_model = out_decoder[..., :p+pwy]
all_scales_obs_model = tf.nn.softplus(out_decoder[..., (p+pwy):(2*(p+pwy))]) + 0.001
all_degfreedom_obs_model = tf.nn.softplus(out_decoder[..., (2*(p+pwy)):(3*(p+pwy))]) + 3
all_log_pxgivenz_flat = tfd.StudentT(loc=tf.reshape(all_means_obs_model,[-1,1]),scale=tf.reshape(all_scales_obs_model,[-1,1]),df=tf.reshape(all_degfreedom_obs_model,[-1,1])).log_prob(data_flat)
all_log_pxgivenz = tf.reshape(all_log_pxgivenz_flat,[K*batch_size,p+pwy])
# ##########
logpxobsgivenz = tf.reshape(tf.reduce_sum(tf.multiply(all_log_pxgivenz[:,0:p_mod],tiledmask_float[:,0:p_mod]),1),[K,batch_size])
logpz = p_z.log_prob(zgivenx)
logq = q_zgivenxobs.log_prob(zgivenx)
# ##########
miwae_loss = -tf.reduce_mean(tf.reduce_logsumexp(logpxobsgivenz + logpz - logq,0)) +tf.log(tf.cast(K,tf.float32))
train_miss = tf.train.AdamOptimizer(learning_rate = learning_rate).minimize(miwae_loss)
# ##########
xgivenz = tfd.Independent(
distribution=tfd.StudentT(loc=all_means_obs_model, scale=all_scales_obs_model, df=all_degfreedom_obs_model))
# ##########
imp_weights = tf.nn.softmax(logpxobsgivenz + logpz - logq,0) # these are w_1,....,w_L for all observations in the batch
xms = tf.reshape(xgivenz.mean(),[K,batch_size,p+pwy])
xm=tf.einsum('ki,kij->ij', imp_weights, xms)
# ##########
z_hat = tf.einsum('ki,kij->ij', imp_weights, zgivenx)
# ##########
sir_logits = tf.transpose(logpxobsgivenz + logpz - logq)
# sirx = tfd.Categorical(logits = sir_logits).sample(num_samples_xmul) # needed if we want to do multiple imputation on x
xmul = tf.reshape(xgivenz.sample(), [K, batch_size, p+pwy])
sirz = tfd.Categorical(logits = sir_logits).sample(num_samples_zmul)
zmul = tf.reshape(zgivenx, [K, batch_size, d_miwae])
# ##########
miwae_loss_train=np.array([])
bs = 64 # batch size
xhat = np.copy(xhat_0) # This will be out imputed data matrix
# x_mul_imp = np.tile(xhat_0,[num_samples_xmul,1,1])
zhat = np.zeros([n,d_miwae]) # low-dimensional representations
zhat_mul = np.tile(zhat, [num_samples_zmul, 1, 1])
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for ep in range(1,n_epochs):
perm = np.random.permutation(n) # We use the "random reshuffling" version of SGD
batches_data = np.array_split(xhat_0[perm,], int(n/bs))
batches_mask = np.array_split(mask[perm,], int(n/bs))
for it in range(len(batches_data)):
train_miss.run(feed_dict={x: batches_data[it], learning_rate: l_rate, K:20, xmask: batches_mask[it]}) # Gradient step
if ep == n_epochs - 1:
losstrain = np.array([miwae_loss.eval(feed_dict={x: xhat_0, K:20, xmask: mask})]) # MIWAE bound evaluation
miwae_loss_train = np.append(miwae_loss_train, -losstrain, axis=0)
elbo = -float(losstrain)
logging.info(f'Epoch {ep}')
logging.info(f'MIWAE likelihood bound {-losstrain}')
for i in range(n): # We impute the observations one at a time for memory reasons
# # Single imputation:
xhat[i, :][~mask[i, :]]=xm.eval(feed_dict={x: xhat_0[i,:].reshape([1, p+pwy]), K:10000, xmask: mask[i, :].reshape([1, p+pwy])})[~mask[i,:].reshape([1,p+pwy])]
# # Multiple imputation:
# si, xmu = sess.run([sirx, xmul],feed_dict={x: xhat_0[i,:].reshape([1,p+pwy]), K:10000, xmask: mask[i,:].reshape([1,p+pwy])})
# x_mul_imp[:,i,:][~np.tile(mask[i,:].reshape([1,p+pwy]),[num_samples_xmul,1])] = np.squeeze(xmu[si,:,:])[~np.tile(mask[i,:].reshape([1,p+pwy]),[num_samples_xmul,1])]
# Dimension reduction:
zhat[i, :] = z_hat.eval(feed_dict={x: xhat_0[i,:].reshape([1, p+pwy]), K:10000, xmask: mask[i,:].reshape([1,p+pwy])})
# Z|X* sampling:
si, zmu = sess.run([sirz, zmul],feed_dict={x: xhat_0[i,:].reshape([1, p+pwy]), K:10000, xmask: mask[i,:].reshape([1,p+pwy])})
zhat_mul[:, i, :] = np.squeeze(zmu[si,:,:]).reshape((num_samples_zmul, d_miwae))
logging.info('----- miwae training done -----')
return xhat, zhat, zhat_mul, elbo
def miwae_es(X_miss, d_miwae=3, h_miwae=128, add_mask=False, mu_prior=0, sig_prior = 1,
num_samples_zmul=200, l_rate = 0.0001, n_epochs = 602, add_wy = False, w = None, y = None,
require_improvement = 30, save_session = False, session_file = None):
np.random.seed(1234)
tf.set_random_seed(1234)
n = X_miss.shape[0] # number of observations
p = X_miss.shape[1] # number of features
pwy = 0
if add_wy:
pwy = 2
X_miss = np.column_stack([X_miss, w, y])
X_miss = np.copy(X_miss)
mask = np.isfinite(X_miss) # binary mask that indicates which values are missing
# ##########
xhat_0 = np.copy(X_miss)
xhat_0[np.isnan(X_miss)] = 0
p_mod = p
if add_mask:
mask_mod = np.copy(mask)
xhat_0 = np.concatenate((xhat_0, mask_mod), axis=1)
mask = np.concatenate((mask, np.ones_like(mask).astype(bool)), axis = 1)
p = p*2
pwy = pwy*2
# ##########
x = tf.placeholder(tf.float32, shape=[None, p+pwy], name='x') # Placeholder for xhat_0
learning_rate = tf.placeholder(tf.float32, shape=[], name='learning_rate')
batch_size = tf.shape(x)[0]
xmask = tf.placeholder(tf.bool, shape=[None, p+pwy], name='xmask')
K = tf.placeholder(tf.int32, shape=[], name='K') # Placeholder for the number of importance weights
# ##########
p_z = tfd.MultivariateNormalDiag(loc=mu_prior+tf.zeros(d_miwae, tf.float32),
scale_diag = sig_prior*tf.ones(d_miwae, tf.float32), name='p_z')
# ##########
sigma = "relu"
decoder = tfk.Sequential([
tfkl.InputLayer(input_shape=[d_miwae,]),
tfkl.Dense(h_miwae, activation=sigma,kernel_initializer="orthogonal"),
tfkl.Dense(h_miwae, activation=sigma,kernel_initializer="orthogonal"),
tfkl.Dense(3*(p+pwy),kernel_initializer="orthogonal") # the decoder will output both the mean, the scale, and the number of degrees of freedoms (hence the 3*p)
], name='decoder')
# ##########
tiledmask = tf.tile(xmask,[K,1], name='tiledmask')
tiledmask_float = tf.cast(tiledmask,tf.float32, name='tiledmask_float')
mask_not_float = tf.abs(-tf.cast(xmask,tf.float32), name='mask_not_float')
iota = tf.Variable(np.zeros([1,p+pwy]),dtype=tf.float32, name='iota')
tilediota = tf.tile(iota,[batch_size,1], name='tilediota')
iotax = x + tf.multiply(tilediota,mask_not_float, name='iotax')
# ##########
encoder = tfk.Sequential([
tfkl.InputLayer(input_shape=[p+pwy,]),
tfkl.Dense(h_miwae, activation=sigma,kernel_initializer="orthogonal"),
tfkl.Dense(h_miwae, activation=sigma,kernel_initializer="orthogonal"),
tfkl.Dense(3*d_miwae,kernel_initializer="orthogonal")
], name='encoder')
# ##########
out_encoder = encoder(iotax)
q_zgivenxobs = tfd.Independent(distribution=tfd.StudentT(loc=out_encoder[..., :d_miwae],
scale=tf.nn.softplus(out_encoder[..., d_miwae:(2*d_miwae)]),
df=3 + tf.nn.softplus(out_encoder[..., (2*d_miwae):(3*d_miwae)])),
name='q_zgivenxobs')
zgivenx = q_zgivenxobs.sample(K)
zgivenx_flat = tf.reshape(zgivenx,[K*batch_size,d_miwae], name='zgivenx_flat')
data_flat = tf.reshape(tf.tile(x,[K,1]),[-1,1], name='data_flat')
# ##########
out_decoder = decoder(zgivenx_flat)
all_means_obs_model = out_decoder[..., :p+pwy]
all_scales_obs_model = tf.add(tf.nn.softplus(out_decoder[..., (p+pwy):(2*(p+pwy))]), 0.001, name='all_scales_obs_model')
all_degfreedom_obs_model = tf.add(tf.nn.softplus(out_decoder[..., (2*(p+pwy)):(3*(p+pwy))]), 3, name='all_degfreedom_obs_model')
all_log_pxgivenz_flat = tfd.StudentT(loc=tf.reshape(all_means_obs_model,[-1,1]),
scale=tf.reshape(all_scales_obs_model,[-1,1]),
df=tf.reshape(all_degfreedom_obs_model,[-1,1])).log_prob(data_flat)
all_log_pxgivenz = tf.reshape(all_log_pxgivenz_flat,[K*batch_size,p+pwy], name='all_log_pxgivenz')
# ##########
logpxobsgivenz = tf.reshape(tf.reduce_sum(tf.multiply(all_log_pxgivenz[:,0:p_mod],tiledmask_float[:,0:p_mod]),1),[K,batch_size],
name='logxobsgivenz')
logpz = p_z.log_prob(zgivenx)
logq = q_zgivenxobs.log_prob(zgivenx)
# ##########
miwae_loss = -tf.reduce_mean(tf.reduce_logsumexp(logpxobsgivenz + logpz - logq,0)) +tf.log(tf.cast(K,tf.float32), name='miwae_loss')
train_miss = tf.train.AdamOptimizer(learning_rate = learning_rate).minimize(miwae_loss)
# ##########
xgivenz = tfd.Independent(
distribution=tfd.StudentT(loc=all_means_obs_model, scale=all_scales_obs_model, df=all_degfreedom_obs_model), name='xgivenz')
# ##########
imp_weights = tf.nn.softmax(logpxobsgivenz + logpz - logq,0, name='imp_weights') # these are w_1,....,w_L for all observations in the batch
xms = tf.reshape(xgivenz.mean(),[K,batch_size,p+pwy], name='xms')
xm = tf.einsum('ki,kij->ij', imp_weights, xms, name='xm')
# ##########
z_hat = tf.einsum('ki,kij->ij', imp_weights, zgivenx, name='z_hat')
# ##########
sir_logits = tf.transpose(logpxobsgivenz + logpz - logq, name='sir_logits')
# sirx = tfd.Categorical(logits = sir_logits).sample(num_samples_xmul) # needed if we want to do multiple imputation on x
xmul = tf.reshape(xgivenz.sample(), [K, batch_size, p+pwy], name='xmul')
sirz = tfd.Categorical(logits = sir_logits).sample(num_samples_zmul)
zmul = tf.reshape(zgivenx, [K, batch_size, d_miwae], name='zmul')
# ##########
miwae_loss_train=np.array([])
bs = 64 # batch size
xhat = np.copy(xhat_0) # This will be out imputed data matrix
# x_mul_imp = np.tile(xhat_0,[num_samples_xmul,1,1])
zhat = np.zeros([n,d_miwae]) # low-dimensional representations
zhat_mul = np.tile(zhat, [num_samples_zmul, 1, 1])
# For early stopping
best_loss = 1e10
losses = []
losses_inter = []
stop = False
last_improvement = 0
elbo = np.nan
saver = tf.train.Saver()
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
save_sess = sess
epoch = 0
while epoch < n_epochs and stop == False:
#train the model on the traning set by mini batches
#suffle then split the training set to mini-batches of size self.batch_size
seq = list(range(n))
np.random.shuffle(seq)
mini_batches = [seq[k:(k + bs)] for k in range(0, n, bs)]
avg_loss = 0. # The average loss of mini_batches
for sample in mini_batches:
batch_data = xhat_0[sample, :]
batch_mask = mask[sample, :]
train_miss.run(feed_dict = {x: batch_data, learning_rate: l_rate, K:20, xmask: batch_mask})
avg_loss += miwae_loss.eval(feed_dict = {x: batch_data, K:20, xmask: batch_mask}) * len(sample)/(1.*n)
#cost history since the last best cost
losses_inter.append(avg_loss)
if avg_loss < best_loss:
save_sess= sess # save session
best_loss = avg_loss
losses += losses_inter # losses history of the validation set
last_improvement = 0
losses_inter= []
else:
last_improvement += 1
if last_improvement > require_improvement:
logging.info(f'Early stopping after epoch {epoch}.')
stop = True
sess = save_sess
epoch += 1
if epoch == n_epochs or stop:
losstrain = np.array([miwae_loss.eval(feed_dict={x: xhat_0, K:20, xmask: mask})]) # MIWAE bound evaluation
miwae_loss_train = np.append(miwae_loss_train, -losstrain, axis=0)
elbo = -float(losstrain)
logging.info(f'Epoch {epoch}')
logging.info(f'MIWAE likelihood bound {-losstrain}')
for i in range(n): # We impute the observations one at a time for memory reasons
# # Single imputation:
xhat[i, :][~mask[i, :]]=xm.eval(feed_dict={x: xhat_0[i,:].reshape([1, p+pwy]), K:10000, xmask: mask[i, :].reshape([1, p+pwy])})[~mask[i,:].reshape([1,p+pwy])]
# # Multiple imputation:
# si, xmu = sess.run([sirx, xmul],feed_dict={x: xhat_0[i,:].reshape([1,p+pwy]), K:10000, xmask: mask[i,:].reshape([1,p+pwy])})
# x_mul_imp[:,i,:][~np.tile(mask[i,:].reshape([1,p+pwy]),[num_samples_xmul,1])] = np.squeeze(xmu[si,:,:])[~np.tile(mask[i,:].reshape([1,p+pwy]),[num_samples_xmul,1])]
# Dimension reduction:
zhat[i, :] = z_hat.eval(feed_dict={x: xhat_0[i,:].reshape([1, p+pwy]), K:10000, xmask: mask[i,:].reshape([1,p+pwy])})
# Z|X* sampling:
si, zmu = sess.run([sirz, zmul],feed_dict={x: xhat_0[i,:].reshape([1, p+pwy]), K:10000, xmask: mask[i,:].reshape([1,p+pwy])})
zhat_mul[:, i, :] = np.squeeze(zmu[si,:,:]).reshape((num_samples_zmul, d_miwae))
if save_session:
#if stop:
# sess_file_name = session_file + '_dmiwae' + str(d_miwae) + '_sigprior' + str(sig_prior) + '_epochsES'
#else:
# sess_file_name = session_file + '_dmiwae' + str(d_miwae) + '_sigprior' + str(sig_prior) + '_epochsMAX'
sess_file_name = session_file + '_dmiwae' + str(d_miwae) + '_sigprior' + str(sig_prior)
saver.save(sess, sess_file_name)
#tf.train.export_meta_graph(sess_file_name + '.meta')
with open(sess_file_name + '.pkl', 'wb') as file_data: # Python 3: open(..., 'wb')
pickle.dump([xhat, zhat, zhat_mul, elbo, epoch], file_data)
logging.info('----- miwae training done -----')
return xhat, zhat, zhat_mul, elbo, epoch