class_docstr: VAE for modling latant space.
class_docstr_tok: ["VAE","for","modling","latant","space","."]
code: def _build(self, unused_input=None):
      # pylint:disable=unused-variable,possibly-unused-variable
      # Reason:
      #   All endpoints are stored as attribute at the end of `_build`.
      #   Pylint cannot infer this case so it emits false alarm of
      #   unused-variable if we do not disable this warning.
  
      config = self.config
  
      # Constants
      batch_size = config["batch_size"]
      n_latent = config["n_latent"]
      n_latent_shared = config["n_latent_shared"]
  
      # ---------------------------------------------------------------------
      # ## Placeholders
      # ---------------------------------------------------------------------
  
      x = tf.placeholder(tf.float32, shape=(None, n_latent))
  
      # ---------------------------------------------------------------------
      # ## Modules with parameters
      # ---------------------------------------------------------------------
      # Variable that is class has name consider to be invalid by pylint so we
      # disable the warning.
      # pylint:disable=invalid-name
      Encoder = config["Encoder"]
      Decoder = config["Decoder"]
      encoder = Encoder(name="encoder")
      decoder = Decoder(name="decoder")
      # pylint:enable=invalid-name
  
      # ---------------------------------------------------------------------
      # ## Placeholders
      # ---------------------------------------------------------------------
      mu, sigma = encoder(x)
      mean_abs_mu, mean_abs_sigma = tf.reduce_mean(
          tf.abs(mu)
      ), tf.reduce_mean(
          tf.abs(sigma)
      )  # for summary only
      q_z = ds.Normal(loc=mu, scale=sigma)
      q_z_sample = q_z.sample()
  
      # Decode
      x_prime = decoder(q_z_sample)
  
      # Reconstruction Loss
      # Don't use log_prob from tf.ds (larger = better)
      # Instead, we use L2 norm (smaller = better)
      # # recons = tf.reduce_sum(p_x.log_prob(x), axis=[-1])
      recons = tf.reduce_mean(tf.square(x_prime - x))
      mean_recons = tf.reduce_mean(recons)
  
      # Prior
      p_z = ds.Normal(loc=0.0, scale=1.0)
      p_z_sample = p_z.sample(
          sample_shape=[batch_size, n_latent_shared]
      )
      x_from_prior = decoder(p_z_sample)
  
      # Space filling
  
      # We use `KL` in variable name for naming consistency with math.
      # pylint:disable=invalid-name
      beta = config["prior_loss_beta"]
      if beta == 0:
          prior_loss = tf.constant(0.0)
      else:
          if config["prior_loss"].lower() == "KL".lower():
              KL_qp = ds.kl_divergence(
                  ds.Normal(loc=mu, scale=sigma), p_z
              )
              KL = tf.reduce_sum(KL_qp, axis=-1)
              mean_KL = tf.reduce_mean(KL)
              prior_loss = mean_KL
          else:
              raise NotImplementedError()
      # pylint:enable=invalid-name
  
      # VAE Loss
      beta = tf.constant(config["prior_loss_beta"])
      scaled_prior_loss = prior_loss * beta
      vae_loss = mean_recons + scaled_prior_loss
  
      # ---------------------------------------------------------------------
      # ## Training
      # ---------------------------------------------------------------------
      # Learning rates
      vae_lr = tf.constant(3e-4)
      # Training Ops
      vae_vars = list(encoder.get_variables())
      vae_vars.extend(decoder.get_variables())
  
      if vae_vars:
          # Here, if we use identity transferm, there is no var to optimize,
          # so in this case we shall avoid building optimizer and saver,
          # otherwise there would be
          # "No variables to optimize." / "No variables to save" error.
  
          # Optimizer
          train_vae = tf.train.AdamOptimizer(
              learning_rate=vae_lr
          ).minimize(vae_loss, var_list=vae_vars)
  
          # Savers
          vae_saver = tf.train.Saver(vae_vars, max_to_keep=100)
  
      # Add all endpoints as object attributes
      for k, v in iteritems(locals()):
          self.__dict__[k] = v
  
imports: from __future__ import absolute_import
  from __future__ import division
  from __future__ import print_function
  from magenta.models.latent_transfer import nn
  from six import iteritems
  import sonnet
  import tensorflow.compat.v1
  import tensorflow_probability
label_desc: Use image node_id 2 for calling the VAE obj's underlying member method code with example usage: obj._build(unused_input) without return types