def __init__(self, hidden_size, batch_size, learning_rate): self.input_tensor = tf.placeholder(tf.float32, [None, 28 * 28]) self.is_training = tf.placeholder_with_default(True, []) with arg_scope([layers.conv2d, layers.conv2d_transpose], activation_fn=concat_elu, normalizer_fn=layers.batch_norm, normalizer_params={ 'scale': True, 'is_training': self.is_training }): with tf.variable_scope("model"): D1 = discriminator(self.input_tensor) # positive examples D_params_num = len(tf.trainable_variables()) G = decoder(tf.random_normal([batch_size, hidden_size])) self.sampled_tensor = G with tf.variable_scope("model", reuse=True): D2 = discriminator(G) # generated examples D_loss = self.__get_discrinator_loss(D1, D2) G_loss = self.__get_generator_loss(D2) params = tf.trainable_variables() D_params = params[:D_params_num] G_params = params[D_params_num:] update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS) g_update_ops = [ op for op in update_ops if op.name.startswith('model_1/') ] d_update_ops = [op for op in update_ops if op not in g_update_ops] # train_discrimator = optimizer.minimize(loss=D_loss, var_list=D_params) # train_generator = optimizer.minimize(loss=G_loss, var_list=G_params) global_step = tf.contrib.framework.get_or_create_global_step() with tf.control_dependencies(d_update_ops): self.train_discrimator = layers.optimize_loss(D_loss, global_step, learning_rate / 10, 'Adam', variables=D_params, update_ops=[]) with tf.control_dependencies(g_update_ops): self.train_generator = layers.optimize_loss(G_loss, global_step, learning_rate, 'Adam', variables=G_params, update_ops=[]) self.sess = tf.Session() self.sess.run(tf.global_variables_initializer())
def train_step(video_real, video_wrong, text): num_clips, t, h, w, c = video_real.shape word, sentence = bert([text]) word, sentence = tf.convert_to_tensor(word), tf.convert_to_tensor(sentence) word = tf.squeeze(word) z = tf.random.normal(shape=(1, 100)) with tf.GradientTape() as gen_tape, tf.GradientTape() as disc_tape: video_fake = generator(video_real, word, z) # All frames put together with bs = 1 video_real = tf.reshape(video_real, (1, num_clips * t, h, w, c)) video_wrong = tf.reshape(video_wrong, (1, num_clips * t, h, w, c)) video_fake = tf.reshape(video_fake, (1, num_clips * t, h, w, c)) # Discriminator out disc_video_real, disc_frame_real, disc_motion_real = discriminator( video_real, sentence) disc_video_wrong, disc_frame_wrong, disc_motion_wrong = discriminator( video_wrong, sentence) disc_video_fake, disc_frame_fake, disc_motion_fake = discriminator( video_fake, sentence) # Losses total_video_loss = video_loss(disc_video_real, disc_video_wrong, disc_video_fake) total_frame_loss = frame_loss(disc_frame_real, disc_frame_wrong, disc_frame_fake) total_motion_loss = motion_loss(disc_motion_real, disc_motion_wrong, disc_motion_fake) disc_loss = discriminator_loss(total_video_loss, total_frame_loss, total_motion_loss) gen_loss = generator_loss(disc_video_fake, disc_frame_fake, disc_motion_fake) gradients_of_generator = gen_tape.gradient(gen_loss, generator.trainable_variables) gradients_of_discriminator = disc_tape.gradient( disc_loss, discriminator.trainable_variables) generator_optimizer.apply_gradients( zip(gradients_of_generator, generator.trainable_variables)) discriminator_optimizer.apply_gradients( zip(gradients_of_discriminator, discriminator.trainable_variables))
def __init__(self, hidden_size, batch_size, learning_rate, generate_size): self.input_tensor = tf.placeholder(tf.float32, [None, 28 * 28]) with arg_scope([layers.conv2d, layers.conv2d_transpose], activation_fn=concat_elu, normalizer_fn=layers.batch_norm, normalizer_params={'scale': True}): with tf.variable_scope("model"): D1 = discriminator(self.input_tensor) # positive examples D_params_num = len(tf.trainable_variables()) G = decoder(tf.random_normal([batch_size, hidden_size])) self.sampled_tensor = G with tf.variable_scope("model", reuse=True): D2 = discriminator(G) # generated examples with tf.variable_scope("model", reuse=True): self.sampled_tensor_gener = decoder( tf.random_normal([generate_size, hidden_size])) D_loss = self.__get_discrinator_loss(D1, D2) G_loss = self.__get_generator_loss(D2) params = tf.trainable_variables() D_params = params[:D_params_num] G_params = params[D_params_num:] # train_discrimator = optimizer.minimize(loss=D_loss, var_list=D_params) # train_generator = optimizer.minimize(loss=G_loss, var_list=G_params) global_step = tf.contrib.framework.get_or_create_global_step() self.train_discrimator = layers.optimize_loss(D_loss, global_step, learning_rate / 10, 'Adam', variables=D_params, update_ops=[]) self.train_generator = layers.optimize_loss(G_loss, global_step, learning_rate, 'Adam', variables=G_params, update_ops=[]) self.sess = tf.Session() self.sess.run(tf.global_variables_initializer())
def gans(ginputs, dinputs, choice, freeze_D): gout = generator(ginputs).get_G() gout = tf.cond( freeze_D, lambda: gout, lambda: tf.stop_gradient(gout)) din = tf.cond( choice, lambda: tf.concat([gout, ginputs], axis = -1), lambda: tf.concat([dinputs, ginputs], axis = -1) ) dout = discriminator(din, freeze_D).get_D() return gout, dout
def define_network(self): # Generators # This one is used to generate fake data self.gen_b_fake = generator(self.X_shoes, self.initializer, scope_name="generator_sb") self.gen_s_fake = generator(self.X_bags, self.initializer, scope_name="generator_bs") # Reconstruction Generators # Note that parameters are being used from previous layers self.gen_recon_s = generator(self.gen_b_fake, self.initializer, scope_name="generator_sb", reuse=True) self.gen_recon_b = generator(self.gen_s_fake, self.initializer, scope_name="generator_bs", reuse=True) # Discriminator for Shoes self.disc_s_real = discriminator(self.X_shoes, self.initializer, scope_name="discriminator_s") self.disc_s_fake = discriminator(self.gen_s_fake, self.initializer, scope_name="discriminator_s", reuse=True) # Discriminator for Bags self.disc_b_real = discriminator(self.X_bags, self.initializer, scope_name="discriminator_b") self.disc_b_fake = discriminator(self.gen_b_fake, self.initializer, reuse=True, scope_name="discriminator_b")
def build_model(self): noise_dim = self.config.noise_dim image_dim = self.config.image_dim learning_rate = self.config.learning_rate #create placeholders self.gen_input = tf.placeholder(tf.float32, shape=[None, noise_dim], name="input_noise") self.disc_input = tf.placeholder(tf.float32, shape=[None, image_dim], name="disc_input") self.parameters = give_me_parameters(self.config) #build generator network with tf.name_scope('generator') as scope: self.gen_sample = generator(self.gen_input, self.parameters) with tf.name_scope('discriminator') as scope: disc_real = discriminator(self.disc_input, self.parameters) disc_fake = discriminator(self.gen_sample, self.parameters) with tf.name_scope('loss') as scope: #make losses self.gen_loss = -tf.reduce_mean(tf.log(disc_fake)) self.disc_loss = -tf.reduce_mean(tf.log(disc_real) + tf.log(1. - disc_fake)) with tf.name_scope('summaries') as scope: gen_images = tf.reshape(self.gen_sample, [-1, 28, 28, 1]) tf.summary.scalar('Generative Loss', self.gen_loss) tf.summary.scalar('Discriminator Loss', self.disc_loss) #tf.summary.image("Generated image", gen_images) tf.summary.image("Generated image", gen_images, 100, family='generated_images') self.merged_summary = tf.summary.merge_all() with tf.name_scope('optimizers') as scope: #create Optimizers optimizer_gen = tf.train.AdamOptimizer(learning_rate=learning_rate) optimizer_disc = tf.train.AdamOptimizer(learning_rate=learning_rate) gen_vars = give_gen_vars(self.parameters) disc_vars = give_disc_vars(self.parameters) self.train_gen = optimizer_gen.minimize(self.gen_loss, var_list=gen_vars) self.train_disc = optimizer_disc.minimize(self.disc_loss, var_list=disc_vars)
import tensorflow as tf import os import utils batch_size = 100 iterations = 10000 im_size = 64 layer = 3 z_size = 100 z_in = tf.placeholder(shape=[batch_size,z_size], dtype=tf.float32) real_in = tf.placeholder(shape=[batch_size,im_size,im_size,layers], g = utils.generator(z_in,im_size, layers,batch_size) d = utils.discriminator(real_in,batch_size d_fake = utils.discriminator(g, batch_size, reuse=True) d_loss_real = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(d, tf.ones_like(d))) d_loss = d_loss_fake
def __init__(self,batch_size=10,im_size=64,channels=3,dtype=tf.float32,analytics=True): self.analytics = analytics self.batch_size = batch_size self.x_a = tf.placeholder(dtype,[None,im_size,im_size,channels],name='xa') self.x_b = tf.placeholder(dtype,[None,im_size,im_size,channels],name='xb') #Generator Networks self.g_ab = utils.generator(self.x_a,name="gen_AB",im_size=im_size) self.g_ba = utils.generator(self.x_b,name="gen_BA",im_size=im_size) #Secondary generator networks, reusing params of previous two self.g_aba = utils.generator(self.g_ab,name="gen_BA",im_size=im_size,reuse=True) self.g_bab = utils.generator(self.g_ba,name="gen_AB",im_size=im_size,reuse=True) #Discriminator for input a self.disc_a_real = utils.discriminator(self.x_a,name="disc_a",im_size=im_size) self.disc_a_fake = utils.discriminator(self.g_ba,name="disc_a",im_size=im_size,reuse=True) #Discriminator for input b self.disc_b_real = utils.discriminator(self.x_b,name="disc_b") self.disc_b_fake = utils.discriminator(self.g_ab,name="disc_b",reuse=True) #Reconstruction loss for generators self.l_const_a = tf.reduce_mean(utils.huber_loss(self.g_aba,self.x_a)) self.l_const_b = tf.reduce_mean(utils.huber_loss(self.g_bab,self.x_b)) #Generation loss for generators self.l_gan_a = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=self.disc_a_fake,labels=tf.ones_like(self.disc_a_fake))) self.l_gan_b = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=self.disc_b_fake,labels=tf.ones_like(self.disc_b_fake))) #Real example loss for discriminators self.l_disc_a_real = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=self.disc_a_real,labels=tf.ones_like(self.disc_a_real))) self.l_disc_b_real = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=self.disc_b_real,labels=tf.ones_like(self.disc_b_real))) #Fake example loss for discriminators self.l_disc_a_fake = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=self.disc_a_fake,labels=tf.zeros_like(self.disc_a_fake))) self.l_disc_b_fake = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=self.disc_b_fake,labels=tf.zeros_like(self.disc_b_fake))) #Combined loss for individual discriminators self.l_disc_a = self.l_disc_a_real + self.l_disc_a_fake self.l_disc_b = self.l_disc_b_real + self.l_disc_b_fake #Total discriminator loss self.l_disc = self.l_disc_a + self.l_disc_b #Combined loss for individual generators self.l_ga = self.l_gan_a + self.l_const_b self.l_gb = self.l_gan_b + self.l_const_a #Total GAN loss self.l_g = self.l_ga + self.l_gb #Parameter Lists self.disc_params = [] self.gen_params = [] for v in tf.trainable_variables(): if 'disc' in v.name: self.disc_params.append(v) if 'gen' in v.name: self.gen_params.append(v) if self.analytics: self.init_analytics() self.gen_a_dir = 'generator a->b' self.gen_b_dir = 'generator b->a' self.rec_a_dir = 'reconstruct a' self.rec_b_dir = 'reconstruct b' self.model_directory = "models" if not os.path.exists(self.gen_a_dir): os.makedirs(self.gen_a_dir) if not os.path.exists(self.gen_b_dir): os.makedirs(self.gen_b_dir) if not os.path.exists(self.rec_b_dir): os.makedirs(self.rec_b_dir) if not os.path.exists(self.rec_a_dir): os.makedirs(self.rec_a_dir) self.sess = tf.Session() self.saver = tf.train.Saver()
import tensorflow as tf import os import utils tf.reset_default_graph() batch_size = 1 iterations = 100000 sample_directory = './figs' model_directory = './models' im_size = 64 layers = 3 z_size = 100 z_in = tf.placeholder(shape=[batch_size, z_size], dtype=tf.float32) real_in = tf.placeholder(shape=[batch_size, im_size, im_size, layers], dtype=tf.float32) g = utils.generator(z_in, im_size, layers, batch_size) d = utils.discriminator(real_in, batch_size) d2 = utils.discriminator(g, batch_size, reuse=True) d_loss_real = tf.reduce_mean( tf.nn.sigmoid_cross_entropy_with_logits(logits=d, labels=tf.ones_like(d))) d_loss_fake = tf.reduce_mean( tf.nn.sigmoid_cross_entropy_with_logits(logits=d2, labels=tf.zeros_like(d2))) d_loss = d_loss_real + d_loss_fake g_loss = tf.reduce_mean( tf.nn.sigmoid_cross_entropy_with_logits(logits=d2, labels=tf.ones_like(d2))) tvars = tf.trainable_variables() g_params = [] d_params = [] for var in tvars:
domainB = "EDGES" #Input placeholders x_a = tf.placeholder(tf.float32,shape=[BATCH_SIZE,64,64,3],name="xa") x_b = tf.placeholder(tf.float32,shape=[BATCH_SIZE,64,64,3],name="xb") #Generator Networks g_ab = utils.generator(x_a,BATCH_SIZE,name="gen_AB") g_ba = utils.generator(x_b,BATCH_SIZE,name="gen_BA") #Secondary generator networks, reusing params of previous two g_aba = utils.generator(g_ab,BATCH_SIZE,name="gen_BA",reuse=True) g_bab = utils.generator(g_ba,BATCH_SIZE,name="gen_AB",reuse=True) #Discriminator for input a disc_a_real = utils.discriminator(x_a,name="disc_a") disc_a_fake = utils.discriminator(g_ba,name="disc_a",reuse=True) #Discriminator for input b disc_b_real = utils.discriminator(x_b,name="disc_b") disc_b_fake = utils.discriminator(g_ab,name="disc_b",reuse=True) #Reconstruction loss for generators l_const_a = tf.reduce_mean(utils.huber_loss(g_aba,x_a)) l_const_b = tf.reduce_mean(utils.huber_loss(g_bab,x_b)) #Generation loss for generators l_gan_a = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=disc_a_fake,labels=tf.ones_like(disc_a_fake))) l_gan_b = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=disc_b_fake,labels=tf.ones_like(disc_b_fake))) #Real example loss for discriminators
x = (x - 0.5) / 0.5 n = len(x) nz = params.nz ngf = params.uncon_ngf ndf = params.uncon_ndf assert ( params.uncon_shape[0] == params.uncon_shape[1]), "Images must be square!" sz = params.uncon_shape[0] nc = params.uncon_shape[2] batchSize = params.uncon_batch_size lr = params.uncon_lr niter = params.uncon_niter netG = utils.generator(sz, nz, ngf, nc) netD = utils.discriminator(sz, nz, ndf, nc) criterion = nn.BCELoss() input = torch.FloatTensor(batchSize, nc, sz, sz) noise = torch.FloatTensor(batchSize, nz, 1, 1) fixed_noise = torch.FloatTensor(batchSize, nz, 1, 1).normal_(0, 1) label = torch.FloatTensor(batchSize) real_label = 1 fake_label = 0 netD.cuda() netG.cuda() criterion.cuda() input, label = input.cuda(), label.cuda() noise, fixed_noise = noise.cuda(), fixed_noise.cuda()
NOISE_DIM = 100 batch_size = 128 mnist = datasets.MNIST('./MNIST_data', train=True, download=True, transform=transforms.ToTensor()) loader_train = DataLoader(mnist, batch_size=batch_size, drop_last=True) imgs = loader_train.__iter__().next()[0].view(batch_size, 784).numpy().squeeze() show_images(imgs) #Training original GAN D = discriminator().to(device) G = generator().to(device) D_optimizer = torch.optim.Adam(D.parameters(), lr=1e-3, betas=(0.5, 0.999)) G_optimizer = torch.optim.Adam(G.parameters(), lr=1e-3, betas=(0.5, 0.999)) train(D, G, D_optimizer, G_optimizer, discriminator_loss, generator_loss, train_loader=loader_train, num_epochs=10, device=device)
def __init__(self, hidden_size, batch_size, learning_rate, alpha, beta, gamma, sum_dir, attri_num, add_gan=1, GAN_model="V", similarity_layer=4): print("\nInitializing model with following parameters:") print("batch_size:", batch_size, " learning_rate:", learning_rate, " alpha:", alpha, " beta:", beta, " gamma:", gamma) print("GAN_model:", GAN_model, " similarity_layer:", similarity_layer, "\n") self.input_tensor = tf.placeholder(tf.float32, [batch_size, 64, 64, 3]) #self.input_label = tf.placeholder(tf.int, [batch_size, attri_num]) self.visual_attri = tf.placeholder(tf.float32, [hidden_size]) with arg_scope([layers.conv2d, layers.conv2d_transpose], activation_fn=tf.nn.relu, normalizer_fn=layers.batch_norm, normalizer_params={'scale': True}, padding='SAME'): with tf.variable_scope("model") as scope: #Full VAEGAN structure # Encoder ENC = encoder(self.input_tensor, hidden_size * 2) Enc_params_num = len(tf.trainable_variables()) # Add noise self.mean, stddev = tf.split(1, 2, ENC) stddev = tf.sqrt(tf.exp(stddev)) epsilon = tf.random_normal( [tf.shape(self.mean)[0], hidden_size]) ENC_w_noise = self.mean + epsilon * stddev # Decoder / Generator self.DEC_of_ENC = decoder(ENC_w_noise) Enc_n_Dec_params_num = len(tf.trainable_variables()) # Discriminator if add_gan == 1: DIS_of_DEC_of_ENC = discriminator(self.DEC_of_ENC, GAN_model) Gen_dis_sum = tf.scalar_summary( 'Gen_dis_mean', tf.reduce_mean(DIS_of_DEC_of_ENC)) with tf.variable_scope( "model", reuse=True) as scope: #Computation for Recon_Loss if add_gan == 1: Real_Similarity = discriminator(self.input_tensor, GAN_model, extract=similarity_layer) Gen_Similarity = discriminator( self.DEC_of_ENC, GAN_model, extract=similarity_layer ) #+ tf.random_normal([batch_size, 8, 8, 256]) with tf.variable_scope( "model", reuse=True) as scope: #Computation for GAN_Loss if add_gan == 1: Real_in_Dis = discriminator(self.input_tensor, GAN_model) Real_dis_sum = tf.scalar_summary( 'Real_dis_mean', tf.reduce_mean(Real_in_Dis)) Prior_in_Dis = discriminator( decoder(tf.random_normal([batch_size, hidden_size])), GAN_model) Prior_dis_sum = tf.scalar_summary( 'Prior_dis_mean', tf.reduce_mean(Prior_in_Dis)) with tf.variable_scope( "model", reuse=True) as scope: #Sample from latent space self.sampled_tensor = decoder( tf.random_normal([batch_size, hidden_size])) with tf.variable_scope( "model", reuse=True) as scope: #Add visual attributes #expand_mean = tf.expand_dims(self.mean, -1) print("shape of mean:", np.shape(self.mean), " shape of visual attri:", np.shape(self.visual_attri)) add_attri = self.mean + np.ones( [batch_size, 1] ) * self.visual_attri #[batch size, hidden size] (broadcasting) print("shape of add attri:", tf.shape(add_attri)) self.with_attri_tensor = decoder(add_attri) self.params = tf.trainable_variables() self.Enc_params = self.params[:Enc_params_num] ''' print ('Encoder Param:') for var in Enc_params: print (var.name) ''' self.Dec_params = self.params[Enc_params_num:Enc_n_Dec_params_num] ''' print ('Decoder Param:') for var in Dec_params: print (var.name) ''' if add_gan == 1: self.Dis_params = self.params[Enc_n_Dec_params_num:] ''' print ('Discriminator Param:') for var in Dis_params: print (var.name) ''' self.Prior_loss = self.__get_prior_loss(self.mean, stddev) Prior_loss_sum = tf.scalar_summary('Prior_loss', self.Prior_loss) if add_gan == 1: self.Recon_loss = self.__get_reconstruction_loss( Gen_Similarity, Real_Similarity) Recon_loss_sum = tf.scalar_summary('Recon_loss', self.Recon_loss) self.GAN_loss = self.__get_GAN_loss(Real_in_Dis, Prior_in_Dis, DIS_of_DEC_of_ENC, GAN_model) GAN_loss_sum = tf.scalar_summary('GAN_loss', self.GAN_loss) else: self.Recon_loss = self.__get_reconstruction_loss( self.DEC_of_ENC, self.input_tensor) Recon_loss_sum = tf.scalar_summary('Recon_loss', self.Recon_loss) # merge summary for Tensorboard if add_gan == 1: self.detached_loss_summary_merged = tf.merge_summary([ Prior_loss_sum, Recon_loss_sum, GAN_loss_sum, Real_dis_sum, Prior_dis_sum, Gen_dis_sum ]) #self.dis_mean_value_summary_merged = tf.merge_summary([Real_dis_sum,Prior_dis_sum,Gen_dis_sum]) else: self.detached_loss_summary_merged = tf.merge_summary( [Prior_loss_sum, Recon_loss_sum]) if add_gan == 1: enc_loss = self.Prior_loss + beta * self.Recon_loss dec_loss = gamma * self.Recon_loss + self.GAN_loss dis_loss = (-1) * self.GAN_loss else: total_loss = self.Prior_loss + beta * self.Recon_loss #self.combined_loss_summary_merged = tf.merge_summary([self.prior_loss_sum,self.recon_loss_sum,self.GAN_loss_sum]) if add_gan == 1: self.train_enc = layers.optimize_loss(enc_loss, tf.contrib.framework.get_or_create_global_step(\ ), learning_rate=learning_rate, variables = self.Enc_params, optimizer='RMSProp', update_ops=[]) self.train_dec = layers.optimize_loss(dec_loss, tf.contrib.framework.get_or_create_global_step(\ ), learning_rate=learning_rate, variables = self.Dec_params, optimizer='RMSProp', update_ops=[]) self.train_dis = layers.optimize_loss(dis_loss, tf.contrib.framework.get_or_create_global_step(\ ), learning_rate=learning_rate * alpha, variables = self.Dis_params, optimizer='RMSProp', update_ops=[]) else: self.train = layers.optimize_loss(total_loss, tf.contrib.framework.get_or_create_global_step(\ ), learning_rate=learning_rate, variables = self.params, optimizer='RMSProp', update_ops=[]) config = tf.ConfigProto() config.gpu_options.allow_growth = True self.sess = tf.Session(config=config) self.sess.run(tf.initialize_all_variables()) self.train_writer = tf.train.SummaryWriter(sum_dir + '/train', self.sess.graph)
def __init__(self, hidden_size, batch_size, learning_rate): self.input_tensor = tf.placeholder(tf.float32, [None, 28 * 28]) self.xs2 = tf.placeholder(tf.float32, [None, 28 * 28]) self.dis = tf.placeholder(tf.float32, [1, None]) self.flag = tf.placeholder(tf.float32, [1, None]) with arg_scope([layers.conv2d, layers.conv2d_transpose], activation_fn=concat_elu, normalizer_fn=layers.batch_norm, normalizer_params={'scale': True}): with tf.variable_scope("model"): D1 = discriminator(self.input_tensor) # positive examples D_params_num = len(tf.trainable_variables()) encoded = encoder(self.input_tensor, hidden_size * 2) mean = encoded[:, :hidden_size] stddev = tf.sqrt(tf.square(encoded[:, hidden_size:])) epsilon = tf.random_normal([tf.shape(mean)[0], hidden_size]) input_sample = mean + epsilon * stddev # G = decoder(tf.random_normal([batch_size, hidden_size])) G_params_num = len(tf.trainable_variables()) G = decoder(input_sample) self.sampled_tensor = G with tf.variable_scope("model", reuse=True): D2 = discriminator(G) # generated examples encoded1 = encoder(self.xs2, hidden_size * 2) mean1 = encoded1[:, :hidden_size] stddev1 = tf.sqrt(tf.square(encoded1[:, hidden_size:])) epsilon1 = tf.random_normal([tf.shape(mean1)[0], hidden_size]) input_sample1 = mean1 + epsilon1 * stddev1 output_tensor1 = decoder(input_sample1) D_loss = self.__get_discrinator_loss(D1, D2) G_loss = self.__get_generator_loss(D2, mean, stddev, mean1) params = tf.trainable_variables() D_params = params[:D_params_num] G_params = params[G_params_num:] # train_discrimator = optimizer.minimize(loss=D_loss, var_list=D_params) # train_generator = optimizer.minimize(loss=G_loss, var_list=G_params) global_step = tf.contrib.framework.get_or_create_global_step() self.train_discrimator = layers.optimize_loss(D_loss, global_step, learning_rate / 10, 'Adam', variables=D_params, update_ops=[]) self.train_generator = layers.optimize_loss(G_loss, global_step, learning_rate, 'Adam', variables=G_params, update_ops=[]) self.sess = tf.Session() self.sess.run(tf.global_variables_initializer())
def __init__(self, input_size, hidden_size, batch_size, learning_rate, log_dir): self.input_tensor = tf.placeholder(tf.float32, [None, 3 * input_size]) self.s_t_p_placeholder = tf.placeholder(tf.float32, [None, hidden_size]) ''' ################################## with open('params.txt') as f: first = f.readline() first = first.strip('\n') temp = first.split(' ') o_p_dim = int(temp[3]); s_p_dim = int(temp[4]); ln = f.readline() for i in range(s_p_dim): temp = f.readline() self.sig_2_init = np.zeros((s_p_dim, s_p_dim), np.float32) for i in range(s_p_dim): temp = f.readline().strip('\n').split(' ') for j in range(s_p_dim): self.sig_2_init[i,j] = float(temp[j]) eig_val , eig_vec = np.linalg.eig(self.sig_2_init) cf = np.sqrt(np.repeat(eig_val,s_p_dim).reshape(s_p_dim,s_p_dim).transpose()) self.r_2_init = np.multiply(cf,eig_vec) self.sig_3_init = np.zeros((o_p_dim, o_p_dim), np.float32) for i in range(o_p_dim): temp = f.readline().strip('\n').split(' ') for j in range(o_p_dim): self.sig_3_init[i,j] = float(temp[j]) eig_val , eig_vec = np.linalg.eig(self.sig_3_init) cf = np.sqrt(np.repeat(eig_val,o_p_dim).reshape(o_p_dim,o_p_dim).transpose()) self.r_3_init = np.multiply(cf,eig_vec) self.a_2_init = np.zeros((s_p_dim, s_p_dim), np.float32) for i in range(s_p_dim): temp = f.readline().strip('\n').split(' ') for j in range(s_p_dim): self.a_2_init[i,j] = float(temp[j]) self.a_3_init = np.zeros((s_p_dim, o_p_dim), np.float32) for i in range(s_p_dim): temp = f.readline().strip('\n').split(' ') for j in range(o_p_dim): self.a_3_init[i,j] = float(temp[j]) ################################### ''' with arg_scope([layers.fully_connected], activation_fn=tf.nn.relu): with tf.variable_scope("encoder"): with tf.variable_scope("encoder_s_t"): self.s_t_minus_1_p = encoder(self.input_tensor[:, :input_size],\ hidden_size) with tf.variable_scope("encoder_s_t", reuse=True): self.s_t_p = encoder(self.input_tensor[:, input_size:2 * input_size],\ hidden_size) with tf.variable_scope("encoder_o_t"): self.o_t_p = encoder(self.input_tensor[:, 2 * input_size:],\ hidden_size) with tf.variable_scope("decoder"): with tf.variable_scope("decoder_s_t"): self.output_s_t_minus_1 = decoder(self.s_t_minus_1_p, input_size) with tf.variable_scope("decoder_s_t", reuse=True): self.output_s_t = decoder(self.s_t_p, input_size) with tf.variable_scope("decoder_s_t", reuse=True): self.s_t_decoded = decoder(self.s_t_p_placeholder, input_size) with tf.variable_scope("decoder_o_t"): self.output_o_t = decoder(self.o_t_p, input_size) self.output_tensor = tf.concat( [self.output_s_t_minus_1, self.output_s_t, self.output_o_t], axis=1) #self.a_2, self.b_2, self.sigma_2, self.a_3, self.b_3, self.sigma_3 = self._MLE_Gaussian_params() self.a_2, self.b_2, self.sigma_2, self.a_3, self.b_3, self.sigma_3 = self._simple_Gaussian_params( ) #self.a_2, self.b_2, self.sigma_2, self.a_3, self.b_3, self.sigma_3 = self._simple_Gaussian_plus_offset_params() self.r_2 = tf.cholesky(self.sigma_2) self.r_3 = tf.cholesky(self.sigma_3) #define reconstruction loss reconstruction_loss = tf.reduce_mean(tf.norm(self.output_tensor - \ self.input_tensor, axis=1)) # define classification loss y_1 = self.s_t_p - tf.matmul(self.s_t_minus_1_p, self.a_2) mvn_1 = tf.contrib.distributions.MultivariateNormalFull( self.b_2, self.sigma_2) #mvn_1 = tf.contrib.distributions.MultivariateNormalTrill(self.b_2, scale_tril=self.r_2) pos_samples_1 = mvn_1.sample(batch_size) y_2 = self.o_t_p - tf.matmul(self.s_t_p, self.a_3) #mvn_2 = tf.contrib.distributions.MultivariateNormalTriL(self.b_3, scale_tril=self.r_3) mvn_2 = tf.contrib.distributions.MultivariateNormalFull( self.b_3, self.sigma_3) pos_samples_2 = mvn_2.sample(batch_size) with tf.variable_scope('discriminator'): with tf.variable_scope('d1'): pos_samples_1_pred = discriminator(pos_samples_1) with tf.variable_scope('d1', reuse=True): neg_samples_1_pred = discriminator(y_1) with tf.variable_scope('d2'): pos_samples_2_pred = discriminator(pos_samples_2) with tf.variable_scope('d2', reuse=True): neg_samples_2_pred = discriminator(y_2) classification_loss_1 = compute_classification_loss( pos_samples_1_pred, neg_samples_1_pred) classification_loss_2 = compute_classification_loss( pos_samples_2_pred, neg_samples_2_pred) classification_loss = classification_loss_1 + classification_loss_2 # define s_t likelihood s_diff = self.s_t_p - tf.matmul(self.s_t_minus_1_p, self.a_2) s_t_likelihood = tf.reduce_sum(mvn_1.log_prob(s_diff)) # define o_t likelihood o_diff = self.o_t_p - tf.matmul(self.s_t_p, self.a_3) o_t_likelihood = tf.reduce_sum(mvn_2.log_prob(o_diff)) self.likelihood = s_t_likelihood + o_t_likelihood # add summary ops tf.summary.scalar('likelihood', self.likelihood) tf.summary.scalar('s_t_likelihood', s_t_likelihood) tf.summary.scalar('o_t_likelihood', o_t_likelihood) tf.summary.scalar('classification_loss', classification_loss) tf.summary.scalar('classification_loss_1', classification_loss_1) tf.summary.scalar('classification_loss_2', classification_loss_2) tf.summary.scalar('reconstruction_loss', reconstruction_loss) # define references to params encoder_params = tf.get_collection( tf.GraphKeys.TRAINABLE_VARIABLES, scope='encoder') decoder_params = tf.get_collection( tf.GraphKeys.TRAINABLE_VARIABLES, scope='decoder') autoencoder_params = encoder_params + decoder_params gaussian_params = [self.a_2, self.a_3, self.r_2, self.r_3] discriminator_params = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, \ scope='discriminator') global_step = tf.contrib.framework.get_or_create_global_step() # define training steps self.learn_rate = self._get_learn_rate(global_step, learning_rate) # update autoencoder params to minimise reconstruction loss self.train_autoencoder = layers.optimize_loss(reconstruction_loss, \ global_step, self.learn_rate * 0.1, optimizer=lambda lr: \ tf.train.AdamOptimizer(lr), variables=\ #tf.train.MomentumOptimizer(lr, 0.9), variables=\ autoencoder_params, update_ops=[]) # update discriminator self.train_discriminator = layers.optimize_loss(classification_loss, \ global_step, self.learn_rate * 10, optimizer=lambda lr: \ tf.train.MomentumOptimizer(lr, 0.1), variables=\ #tf.train.AdamOptimizer(lr), variables=\ discriminator_params, update_ops=[]) # update encoder params to fool the discriminator self.train_encoder = layers.optimize_loss(-classification_loss, \ global_step, self.learn_rate , optimizer=lambda lr: \ #tf.train.MomentumOptimizer(lr, 0.9), variables=\ tf.train.AdamOptimizer(lr), variables=\ encoder_params, update_ops=[]) self.sess = tf.Session() self.merged = tf.summary.merge_all() self.train_writer = tf.summary.FileWriter(log_dir, \ self.sess.graph) self.sess.run(tf.global_variables_initializer())
def __init__(self, hidden_size, batch_size, learning_rate, log_dir): self.input_tensor = tf.placeholder(tf.float32, [None, 28 * 28]) # add gaussian noise to the input input_with_noise = gaussian_noise_layer(self.input_tensor, 0.3) with arg_scope([layers.fully_connected], activation_fn=tf.nn.relu): with tf.variable_scope("encoder"): self.latent_representation = encoder(input_with_noise, hidden_size) encoder_params_num = len(tf.trainable_variables()) with tf.variable_scope('encoder', reuse=True): self.true_latent_representation = encoder(self.input_tensor, hidden_size) with tf.variable_scope('decoder'): self.recons = decoder(self.latent_representation) autoencoder_params_num = len(tf.trainable_variables()) with tf.variable_scope('decoder', reuse=True): self.sampled_imgs = decoder(tf.random_normal([batch_size, hidden_size])) pos_samples = tf.random_normal([batch_size, hidden_size], stddev=5.) neg_samples = self.latent_representation with tf.variable_scope('discriminator'): pos_samples_pred = discriminator(pos_samples) with tf.variable_scope('discriminator', reuse=True): neg_samples_pred = discriminator(neg_samples) #define losses reconstruction_loss = tf.reduce_mean(tf.square(self.recons - self.input_tensor)) #* 28 * 28 scale recons loss classification_loss = tf.losses.sigmoid_cross_entropy(\ tf.ones(tf.shape(pos_samples_pred)), pos_samples_pred) +\ tf.losses.sigmoid_cross_entropy(tf.zeros( tf.shape(neg_samples_pred)), neg_samples_pred) tf.summary.scalar('reconstruction_loss', reconstruction_loss) tf.summary.scalar('classification_loss', classification_loss) # define references to params params = tf.trainable_variables() encoder_params = params[:encoder_params_num] decoder_params = params[encoder_params_num:autoencoder_params_num] autoencoder_params = encoder_params + decoder_params discriminator_params = params[autoencoder_params_num:] # record true positive rate and true negative rate correct_pred_pos = tf.equal(tf.cast(pos_samples_pred>0, tf.float32), tf.ones(tf.shape(pos_samples_pred))) self.true_pos_rate = tf.reduce_mean(tf.cast(correct_pred_pos, tf.float32)) correct_pred_neg = tf.equal(tf.cast(neg_samples_pred<0, tf.float32), tf.ones(tf.shape(pos_samples_pred))) self.true_neg_rate = tf.reduce_mean(tf.cast(correct_pred_neg, tf.float32)) tf.summary.scalar('true_pos_rate', self.true_pos_rate) tf.summary.scalar('true_neg_rate', self.true_neg_rate) global_step = tf.contrib.framework.get_or_create_global_step() self.learn_rate = self._get_learn_rate(global_step, learning_rate) self.train_autoencoder = layers.optimize_loss(reconstruction_loss, global_step, self.learn_rate/10, optimizer=lambda lr: \ tf.train.MomentumOptimizer(lr, momentum=0.9), variables= autoencoder_params, update_ops=[]) self.train_discriminator = layers.optimize_loss(classification_loss, global_step, self.learn_rate, optimizer=lambda lr: \ tf.train.MomentumOptimizer(lr, momentum=0.1), variables= discriminator_params, update_ops=[]) self.train_encoder = layers.optimize_loss(-classification_loss, global_step, self.learn_rate/10, optimizer=lambda lr: \ tf.train.MomentumOptimizer(lr, momentum=0.1), variables= encoder_params, update_ops=[]) self.sess = tf.Session() self.merged = tf.summary.merge_all() self.train_writer = tf.summary.FileWriter(log_dir, self.sess.graph) self.sess.run(tf.global_variables_initializer())