def __init__(self): self.en = encoder(FLAGS.z_dim) self.de = decoder() self.di = discriminator() self.x = tf.placeholder(tf.float32, [None, 28, 28, 1], name='x') self.real_z = tf.placeholder(tf.float32, [None, FLAGS.z_dim]) self.fake_z = self.en(self.x) self.rec_x, _ = self.de(self.fake_z, False) self.gen_x, _ = self.de(self.real_z) self.g_loss = tf.reduce_mean(self.di(self.fake_z, False)) self.d_loss = tf.reduce_mean(self.di(self.real_z)) - tf.reduce_mean( self.di(self.fake_z)) + self._dd() self.a_loss = tf.reduce_mean(tf.square(self.rec_x - self.x)) self.g_optim = tf.train.AdamOptimizer(1e-3).minimize( self.g_loss, var_list=self.en.vars) self.d_optim = tf.train.AdamOptimizer(1e-3).minimize( self.d_loss, var_list=self.di.vars) self.a_optim = tf.train.AdamOptimizer(1e-3).minimize( self.a_loss, tf.train.get_or_create_global_step()) self.fit_summary = tf.summary.merge([ tf.summary.scalar('g_loss', self.g_loss), tf.summary.scalar('d_loss', self.d_loss), tf.summary.scalar('a_loss', self.a_loss), tf.summary.histogram('real_z', self.real_z), tf.summary.histogram('fake_z', self.fake_z), tf.summary.image('x', self.x, 8), tf.summary.image('rec_x', self.rec_x, 8) ]) self.gen_summary = tf.summary.merge( [tf.summary.image('gen_x', self.gen_x, 8)])
def __init__(self): self.en = encoder() self.de = decoder() self.z = tf.placeholder(tf.float32, [None, FLAGS.z_dim], name='z') self.x = tf.placeholder(tf.float32, [None, 28, 28, 1], name='x') en_ = self.en(self.x) self.mu = tf.layers.dense(en_, FLAGS.z_dim) self.log_var = tf.layers.dense(en_, FLAGS.z_dim) eps = tf.random_normal(tf.shape(self.mu)) self.z_latent = self.mu + tf.exp(0.5 * self.log_var) * eps self.rec_x, logits = self.de(self.z_latent, False) self.gen_x, _ = self.de(self.z) # self.loss_nll = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=logits, labels=self.x)) self.loss_nll = tf.reduce_mean(tf.square(self.x - self.rec_x)) self.loss_mmd = compute_mmd(self.z, self.z_latent) self.loss = self.loss_nll + FLAGS.beta * self.loss_mmd self.optim = tf.train.AdamOptimizer(1e-3).minimize( self.loss, tf.train.get_or_create_global_step()) self.fit_summary = tf.summary.merge([ tf.summary.scalar('loss', self.loss), tf.summary.scalar('loss_nll', self.loss_nll), tf.summary.scalar('loss_mmd', self.loss_mmd), tf.summary.image('x', self.x, 8), tf.summary.image('rec_x', self.rec_x, 8), tf.summary.histogram('z', self.z_latent), tf.summary.histogram('mu', self.mu), tf.summary.histogram('logvar', self.log_var) ]) self.gen_summary = tf.summary.merge( [tf.summary.image('gen_x', self.gen_x, 8)])