import tensorflow as tf

    self.build_predictive_model()  # builds on top of predictive model. Reuses triplet encoding

    # build denoising objective
    models = self.models
    self.loss_dn = self._noisy_decode(models[1])
    self.losses = [self.loss_reco, self.loss_pred, self.loss_dist, self.loss_dn]

  def _noisy_decode(self, model):
    """Distort middle encoding with [<= 1/3*dist(neigbour)] and demand good reconstruction"""
    # dist = l2(x1 - x2)
    # noise = dist * self.epsilon_sphere_noise()
    # tf.stop_gradient(noise)
    noise = tf.random_normal(self.model.encode.get_shape().as_list()) * FLAGS.epsilon
    noisy_encoding = noise + self.models[1].encode
    tf.stop_gradient(noisy_encoding)  # or maybe here, who knows
    noisy_decode = interpreter.build_decoder(noisy_encoding, model.config, reuse=True, masks=model.mask_list)
    loss = interpreter.l2_loss(noisy_decode, self.raw_targets[1], alpha=FLAGS.beta)
    self.models += [noisy_decode]
    return loss

  def _tensor_to_image(self, net):
    with tf.name_scope('to_image'):
      if FLAGS.new_blur:
        net = net[..., :self.batch_shape[-1]]
      net = tf.nn.relu(net)
      net = tf.cast(net <= 1, net.dtype) * net * 255
      net = tf.cast(net, tf.uint8)
      return net
