2
$\begingroup$

I was trying to implement the loss function of H-GAN. Here is my code . But it seem somethings wrong, maybe is recognition loss on z (EQ 9). I used the EQ 5 on MISO to calculate it. Here is my code:

def recognition_loss_on_z(self,latent_code, r_cont_mu, r_cont_var):
    eplison = (r_cont_mu - latent_code) / (r_cont_var+1e-8)
    return -tf.reduce_mean(tf.reduce_sum(-0.5*tf.log(2*np.pi*r_cont_var+1e-8)-0.5*tf.square(eplison), axis=1))/(config.batch_size * config.latent_dim)

And I calculated loss function:

    self.z_mean, self.z_sigm = self.Encode(self.images)
    self.z_x = tf.add(self.z_mean, tf.sqrt(tf.exp(self.z_sigm))*self.ep)

    self.D_pro_logits, self.l_x_real, self.Q_y_style_given_x_real, continuous_mu_real, continuous_var_real  = self.discriminator(self.images, training=True, reuse=False)
    self.De_pro_tilde, self.l_x_tilde, self.Q_y_style_given_x_tidle, continuous_mu_tidle, continuous_var_tidle= self.discriminator(self.x_tilde, training=True, reuse = True)
    self.G_pro_logits, self.l_x_fake, self.Q_y_style_given_x_fake, continuous_mu_fake, continuous_var_fake = self.discriminator(self.x_p, training=True, reuse=True)

    tidle_latent_loss = self.recognition_loss_on_z(self.z_x, continuous_mu_tidle,continuous_var_tidle)
    real_latent_loss  = self.recognition_loss_on_z(self.z_x, continuous_mu_real,continuous_var_real)
    fake_latent_loss =  self.recognition_loss_on_z(self.zp, continuous_mu_fake,continuous_var_fake)

And discriminator:

    def discriminator(self, x_var,training=False, reuse=False):
    with tf.variable_scope("discriminator_recongnizer") as scope:
        if reuse==True:
            scope.reuse_variables()
        conv1 = tf.nn.leaky_relu(batch_normalization(conv2d(x_var, output_dim = 64 , kernel_size=6, name='dis_R_conv1'),training = training,name='dis_bn1', reuse = reuse), alpha =0.2)
        conv2 = tf.nn.leaky_relu(batch_normalization(conv2d(conv1, output_dim = 128 , kernel_size=4, name='dis_R_conv2'),training = training ,name='dis_bn2', reuse = reuse), alpha =0.2)
        conv3 = tf.nn.leaky_relu(batch_normalization(conv2d(conv2, output_dim = 128 , kernel_size=4, name='dis_R_conv3'),training = training,name='dis_bn3', reuse = reuse), alpha =0.2)
        conv4 = conv2d(conv3, output_dim = 256 , kernel_size=4, name='dis_R_conv4')
        lth_layer = conv4
        conv4 = tf.nn.leaky_relu(batch_normalization(conv4, training=training, name='dis_bn4', reuse = reuse),alpha =0.2)
        conv4 = tf.reshape(conv4,[-1, 256*8*8])
        #Discriminator
        with tf.variable_scope('discriminator'):
            d_output = fully_connect(conv4, output_size=1, scope='dr_dense_2')
        with tf.variable_scope('dis_q'):
            fc_r = tf.nn.leaky_relu(batch_normalization(fully_connect(conv4, output_size=256 + config.style_classes, scope='dis_dr_dense_3'), training=training, name='dis_bn_fc_r', reuse=reuse), alpha=0.2)
            continuous_mu = fully_connect(fc_r, output_size=256, scope='dis_dr_dense_mu')
            continuous_var = tf.exp(fully_connect(fc_r, output_size=256, scope='dis_dr_dense_logvar'))
            style_predict = fully_connect(fc_r, output_size=config.style_classes, scope='dis_dr_dense_y_style')

        return d_output,lth_layer,style_predict,continuous_mu,continuous_var   

Does anyone have experience with that, please tell me where I was wrong. Thanks you so much, I really appreciate that!

$\endgroup$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.