def _gradient_penalty(self, X, X_hat, L, G, Z, Z_hat, EPSILON): ax1 = [i for i in range(1, len(X.get_shape()))] ax2 = [i for i in range(1, len(Z.get_shape()))] with tf.name_scope('grad_penalty'): with tf.name_scope('x'): x_inter = ops.interpolate(X, X_hat, EPSILON, name='x_inter') d_inter_x = self._discriminator(x_inter, L, is_training=True, reuse=True) x_grads, z_grads = tf.gradients([d_inter_x], [x_inter, L], name="grads_x") x_slopes = tf.sqrt(tf.reduce_sum(x_grads**2, axis=ax1)) tf.summary.scalar('x_slopes', tf.reduce_mean(x_slopes)) delta_x = X - X_hat x_unit = delta_x / ops.norm(delta_x, ax1, True) x_alpha = tf.reduce_sum(x_grads * x_unit, ax1) / x_slopes tf.summary.scalar('x_alpha', tf.reduce_mean(x_alpha)) tf.summary.scalar('x_unit_grads', tf.reduce_mean(ops.norm(x_unit, ax1))) x_pen = tf.sqrt(tf.reduce_sum((x_grads - x_unit)**2, axis=ax1)) gp_loss_x = tf.reduce_mean(x_pen, name='gp_loss_x') tf.add_to_collection('losses', gp_loss_x) tf.summary.scalar('gp_loss_x', gp_loss_x) with tf.name_scope('z'): z_inter = ops.interpolate(Z, Z_hat, EPSILON, name='z_inter') d_inter_z = -self._discriminator( G, z_inter, is_training=True, reuse=True) _, z_grads = tf.gradients([d_inter_z], [G, z_inter], name="grads_z") delta_z = Z - Z_hat z_unit = delta_z / ops.norm(delta_z, ax2, True) z_pen = tf.sqrt(tf.reduce_sum((z_grads - z_unit)**2, axis=ax2)) gp_loss_z = tf.reduce_mean(z_pen, name='gp_loss_z') tf.add_to_collection('losses', gp_loss_z) tf.summary.scalar('gp_loss_z', gp_loss_z) gp_loss = gp_loss_x # gp_loss = gp_loss_x + gp_loss_z tf.add_to_collection('losses', gp_loss) tf.summary.scalar('gp_loss', gp_loss) return gp_loss
def _gradient_penalty(self, X, G, EPSILON, D_fn): with tf.name_scope('grad_penalty'): x_inter = ops.interpolate(X, G, EPSILON, name='x_inter') d_inter = D_fn(x_inter, is_training=True, reuse=True) grad = tf.gradients(d_inter, x_inter, name="grads")[0] n_dim = len(grad.get_shape()) slopes = tf.sqrt( tf.reduce_sum(tf.square(grad), reduction_indices=[i for i in range(1, n_dim)])) tf.summary.histogram('slopes', slopes) gp_loss = tf.reduce_mean((slopes - 1.)**2, name='gp_loss') tf.add_to_collection('losses', gp_loss) tf.summary.scalar('gp_loss', gp_loss) return gp_loss