def samples_generator(batch_data_list, X_list,zpreds, miss_list, types_list, batch_size, z_dim, y_dim, y_dim_partition, s_dim, tau, normalization_params): samples_test = dict.fromkeys(['s','z','y','x'],[]) test_params = dict() X = tf.concat(X_list,1) #Create the proposal of q(s|x^o) _, params = VAE_functions.s_proposal_multinomial(X, batch_size, s_dim, tau, reuse=True) samples_test['s'] = tf.one_hot(tf.argmax(params,1),depth=s_dim) #Create the proposal of q(z|s,x^o) _, params = VAE_functions.z_proposal_GMM(X, samples_test['s'],zpreds, batch_size, z_dim, reuse=True) samples_test['z'] = params[0] #Create deterministic layer y samples_test['y'] = tf.layers.dense(inputs=samples_test['z'], units=y_dim, activation=None, kernel_initializer=tf.random_normal_initializer(stddev=0.05), name= 'layer_h1_', reuse=True) grouped_samples_y = VAE_functions.y_partition(samples_test['y'], types_list, y_dim_partition) #Compute the parameters h_y theta = VAE_functions.theta_estimation_from_y(grouped_samples_y, types_list, miss_list, batch_size, reuse=True) #Compute loglik and output of the VAE log_p_x, log_p_x_missing, samples_test['x'], test_params['x'] = VAE_functions.loglik_evaluation(batch_data_list, types_list, miss_list, theta, normalization_params, reuse=True) return samples_test, test_params, log_p_x, log_p_x_missing
def fixed_decoder(batch_data_list, X_list, miss_list_VP,miss_list, types_list, batch_size, z_dim, y_dim, y_dim_partition, s_dim, tau, normalization_params,zcodes,scodes): samples_test = dict.fromkeys(['s','z','y','x'],[]) test_params = dict() X = tf.concat(X_list,1) #Create the proposal of q(s|x^o) samples_test['s'] = tf.one_hot(scodes,depth=s_dim) # set fixed z samples_test['z'] = zcodes #Create deterministic layer y samples_test['y'] = tf.layers.dense(inputs=samples_test['z'], units=y_dim, activation=None, kernel_initializer=tf.random_normal_initializer(stddev=0.05), name= 'layer_h1_', reuse=True) grouped_samples_y = VAE_functions.y_partition(samples_test['y'], types_list, y_dim_partition) #Compute the parameters h_y theta = VAE_functions.theta_estimation_from_y(grouped_samples_y, types_list, miss_list_VP, batch_size, reuse=True) #Compute loglik and output of the VAE log_p_x, log_p_x_missing, samples_test['x'], test_params['x'] = VAE_functions.loglik_evaluation(batch_data_list, types_list, miss_list, theta, normalization_params, reuse=True) return samples_test, test_params, log_p_x, log_p_x_missing
def encoder(X_list, miss_list, batch_size, z_dim, s_dim, tau): samples = dict.fromkeys(['s','z','y','x'],[]) q_params = dict() X = tf.concat(X_list,1) #Create the proposal of q(s|x^o) samples['s'], q_params['s'] = VAE_functions.s_proposal_multinomial(X, batch_size, s_dim, tau, reuse=None) #Create the proposal of q(z|s,x^o) samples['z'], q_params['z'] = VAE_functions.z_proposal_GMM(X, samples['s'], batch_size, z_dim, reuse=None) return samples, q_params
def decoder(batch_data_list, miss_list, types_list, samples, q_params, normalization_params, batch_size, z_dim, y_dim, y_dim_partition, tau2): p_params = dict() #Create the distribution of p(z|s) p_params['z'] = VAE_functions.z_distribution_GMM(samples['s'], z_dim, reuse=None) #Create deterministic layer y samples['y'] = tf.layers.dense( inputs=samples['z'], units=y_dim, activation=None, kernel_initializer=tf.random_normal_initializer(stddev=0.05), name='layer_h1_', reuse=None) grouped_samples_y = VAE_functions.y_partition(samples['y'], types_list, y_dim_partition) #Compute the parameters h_y # theta = VAE_functions.theta_estimation_from_y(grouped_samples_y, types_list, miss_list, batch_size, reuse=None) theta = VAE_functions.theta_estimation_from_ys(grouped_samples_y, samples['s'], types_list, miss_list, batch_size, reuse=None) #Compute loglik and output of the VAE log_p_x, log_p_x_missing, samples['x'], p_params[ 'x'] = VAE_functions.loglik_evaluation(batch_data_list, types_list, miss_list, theta, tau2, normalization_params, reuse=None) return theta, samples, p_params, log_p_x, log_p_x_missing
def HVAE_graph(model_name, types_file, batch_size, learning_rate=1e-3, z_dim=2, y_dim=1, s_dim=2, y_dim_partition=[]): #We select the model for the VAE print('[*] Importing model: ' + model_name) model = __import__(model_name) #Load placeholders print('[*] Defining placeholders') batch_data_list, batch_data_list_observed, miss_list, tau, types_list = VAE_functions.place_holder_types( types_file, batch_size) #Batch normalization of the data X_list, normalization_params = VAE_functions.batch_normalization( batch_data_list_observed, types_list, miss_list) #Set dimensionality of Y if y_dim_partition: y_dim_output = np.sum(y_dim_partition) else: y_dim_partition = y_dim * np.ones(len(types_list), dtype=int) y_dim_output = np.sum(y_dim_partition) #Encoder definition print('[*] Defining Encoder...') samples, q_params = model.encoder(X_list, miss_list, batch_size, z_dim, s_dim, tau) print('[*] Defining Decoder...') theta, samples, p_params, log_p_x, log_p_x_missing = model.decoder( batch_data_list, miss_list, types_list, samples, q_params, normalization_params, batch_size, z_dim, y_dim_output, y_dim_partition) print('[*] Defining Cost function...') ELBO, loss_reconstruction, KL_z, KL_s = model.cost_function( log_p_x, p_params, q_params, types_list, z_dim, y_dim_output, s_dim) optim = tf.train.AdamOptimizer(learning_rate).minimize(-ELBO) #Generator function for testing purposes samples_test, test_params, log_p_x_test, log_p_x_missing_test = model.samples_generator( batch_data_list, X_list, miss_list, types_list, batch_size, z_dim, y_dim_output, y_dim_partition, s_dim, tau, normalization_params) #Packing results tf_nodes = { 'ground_batch': batch_data_list, 'ground_batch_observed': batch_data_list_observed, 'miss_list': miss_list, 'tau_GS': tau, 'samples': samples, 'log_p_x': log_p_x, 'log_p_x_missing': log_p_x_missing, 'loss_re': loss_reconstruction, 'loss': -ELBO, 'optim': optim, 'KL_s': KL_s, 'KL_z': KL_z, 'p_params': p_params, 'q_params': q_params, 'samples_test': samples_test, 'test_params': test_params, 'log_p_x_test': log_p_x, 'log_p_x_missing_test': log_p_x_missing_test } return tf_nodes
def HVAE_graph(model_name, types_file, batch_size, learning_rate=1e-3, z_dim=2, y_dim=1, s_dim=2, y_dim_partition=[]): #We select the model for the VAE print('[*] Importing model: ' + model_name) model = __import__(model_name) #Load placeholders print('[*] Defining placeholders') batch_data_list, batch_data_list_observed, miss_list, miss_list_VP, tau, types_list, zcodes, scodes = VAE_functions.place_holder_types( types_file, batch_size) #Batch normalization of the data X_list, normalization_params = VAE_functions.batch_normalization( batch_data_list_observed, types_list, miss_list) #Set dimensionality of Y if y_dim_partition: y_dim_output = np.sum(y_dim_partition) else: y_dim_partition = y_dim * np.ones(len(types_list), dtype=int) y_dim_output = np.sum(y_dim_partition) #Encoder definition print('[*] Defining Encoder...') samples, q_params = model.encoder(X_list, miss_list, batch_size, z_dim, s_dim, tau) print('[*] Defining Decoder...') theta, samples, p_params, log_p_x, log_p_x_missing = model.decoder( batch_data_list, miss_list, types_list, samples, q_params, normalization_params, batch_size, z_dim, y_dim_output, y_dim_partition) print('[*] Defining Cost function...') ELBO, loss_reconstruction, KL_z, KL_s = model.cost_function( log_p_x, p_params, q_params, types_list, z_dim, y_dim_output, s_dim) loss_reg = tf.losses.get_regularization_loss( ) # not using this at the moment (set weight_decay to 0 to be safe) optim = tf.train.AdamOptimizer(learning_rate).minimize( -ELBO) # + loss_reg) # fixed decoder for passing s/z codes and miss_list of VPs generated in the BNet samples_zgen, test_params_zgen, log_p_x_zgen, log_p_x_missing_zgen = model.fixed_decoder( batch_data_list, X_list, miss_list_VP, miss_list, types_list, batch_size, z_dim, y_dim_output, y_dim_partition, s_dim, tau, normalization_params, zcodes, scodes) #Packing results tf_nodes = { 'ground_batch': batch_data_list, 'ground_batch_observed': batch_data_list_observed, 'miss_list': miss_list, 'miss_list_VP': miss_list_VP, 'tau_GS': tau, 'zcodes': zcodes, 'scodes': scodes, 'samples': samples, 'log_p_x': log_p_x, 'log_p_x_missing': log_p_x_missing, 'loss_re': loss_reconstruction, 'loss': -ELBO, 'loss_reg': loss_reg, 'optim': optim, 'KL_s': KL_s, 'KL_z': KL_z, 'p_params': p_params, 'q_params': q_params, 'samples_zgen': samples_zgen, 'test_params_zgen': test_params_zgen, 'log_p_x_zgen': log_p_x_zgen, 'log_p_x_missing_zgen': log_p_x_missing_zgen } return tf_nodes