//  [python]
def _build_loss(self):
    # Refiner loss
    def fake_label(layer):
      return tf.zeros_like(layer, dtype=tf.int32)[:,:,:,0]

    def real_label(layer):
      return tf.ones_like(layer, dtype=tf.int32)[:,:,:,0]

    def log_loss(logits, label, name):
      return tf.reduce_sum(SE_loss(logits=logits, labels=label), [1, 2], name=name)

    with tf.name_scope("refiner"):
      self.realism_loss = log_loss(
          self.D_R_x_logits, real_label(self.D_R_x_logits), "realism_loss")
      self.regularization_loss = \
          self.reg_scale * tf.reduce_sum(
              tf.abs(self.R_x - self.normalized_x), [1, 2, 3],
              name="regularization_loss")

      self.refiner_loss = tf.reduce_mean(
          self.realism_loss + self.regularization_loss,
          name="refiner_loss")

      if self.debug:
        self.refiner_loss = tf.Print(
            self.refiner_loss, [self.R_x], "R_x")
        self.refiner_loss = tf.Print(
            self.refiner_loss, [self.D_R_x], "D_R_x")
        self.refiner_loss = tf.Print(
            self.refiner_loss, [self.normalized_x], "normalized_x")
        self.refiner_loss = tf.Print(
            self.refiner_loss, [self.denormalized_R_x], "denormalized_R_x")
        self.refiner_loss = tf.Print(
            self.refiner_loss, [self.regularization_loss], "reg_loss")

    self.refiner_summary = tf.summary.merge([
        #tf.summary.image("synthetic_images",
        #                 self.x, max_outputs=self.config.max_image_summary),
        #tf.summary.image("refined_images",
        #                 self.denormalized_R_x, max_outputs=self.config.max_image_summary),
        tf.summary.scalar("refiner/realism_loss",
                          tf.reduce_mean(self.realism_loss)),
        tf.summary.scalar("refiner/regularization_loss",
                          tf.reduce_mean(self.regularization_loss)),
        tf.summary.scalar("refiner/loss",
                          tf.reduce_mean(self.refiner_loss)),
    ])

    # Discriminator loss
    with tf.name_scope("discriminator"):
      self.refiner_d_loss = log_loss(
          self.D_R_x_logits, fake_label(self.D_R_x_logits), "refiner_d_loss")
      self.synthetic_d_loss = log_loss(
          self.D_y_logits, real_label(self.D_y_logits), "synthetic_d_loss")

      self.discrim_loss = tf.reduce_mean(
          self.refiner_d_loss + \
              self.synthetic_d_loss, name="discrim_loss")

      # with history
      self.refiner_d_loss_with_history = log_loss(
          self.D_R_x_history_logits,
          fake_label(self.D_R_x_history_logits),
          "refiner_d_loss_with_history")
      self.discrim_loss_with_history = tf.reduce_mean(
          tf.concat([self.refiner_d_loss, self.refiner_d_loss_with_history], axis=0) + \
              self.synthetic_d_loss, name="discrim_loss_with_history")

      if self.debug:
        self.discrim_loss_with_history = tf.Print(
            self.discrim_loss_with_history, [self.D_R_x_logits], "D_R_x_logits")
        self.discrim_loss_with_history = tf.Print(
            self.discrim_loss_with_history, [self.D_y_logits], "D_y_logits")
        self.discrim_loss_with_history = tf.Print(
            self.discrim_loss_with_history, [self.refiner_d_loss], "refiner_d_loss")
        self.discrim_loss_with_history = tf.Print(
            self.discrim_loss_with_history, [self.refiner_d_loss_with_history], "refiner_d_loss_with_history")
        self.discrim_loss_with_history = tf.Print(
            self.discrim_loss_with_history, [self.synthetic_d_loss], "synthetic_d_loss")
        self.discrim_loss_with_history = tf.Print(
            self.discrim_loss_with_history, [self.D_R_x_history_logits], "D_R_x_history_logits")
        self.discrim_loss_with_history = tf.Print(
            self.discrim_loss_with_history, [self.D_y_logits], "D_y_logits")

      self.discrim_summary = tf.summary.merge([
          #tf.summary.image("real_images",
          #                  self.resized_y, max_outputs=self.config.max_image_summary),
          tf.summary.scalar("synthetic_d_loss",
                            tf.reduce_mean(self.synthetic_d_loss)),
          tf.summary.scalar("refiner_d_loss",
                            tf.reduce_mean(self.refiner_d_loss)),
          tf.summary.scalar("discrim_loss",
                            tf.reduce_mean(self.discrim_loss)),
      ])
      self.discrim_summary_with_history = tf.summary.merge([
          #tf.summary.image("real_images",
          #                 self.resized_y, max_outputs=self.config.max_image_summary),
          tf.summary.scalar("synthetic_d_loss",
                            tf.reduce_mean(self.synthetic_d_loss)),
          tf.summary.scalar("refiner_d_loss_with_history",
                            tf.reduce_mean(self.refiner_d_loss_with_history)),
          tf.summary.scalar("discrim_loss_with_history",
                            tf.reduce_mean(self.discrim_loss_with_history)),
      ])