class_docstr: Basic next-frame model with a tiny discrete latent.
docstr: Inject a deterministic latent based on the target frame.
docstr_tok: ["Inject","a","deterministic","latent","based","on","the","target","frame","."]
code: def inject_latent(self, layer, inputs, target, action):
      """Inject a deterministic latent based on the target frame."""
      hparams = self.hparams
      final_filters = common_layers.shape_list(layer)[-1]
      filters = hparams.hidden_size
      kernel = (4, 4)
      layer_shape = common_layers.shape_list(layer)
      activation_fn = common_layers.belu
      if hparams.activation_fn == "relu":
          activation_fn = tf.nn.relu
  
      def add_bits(layer, bits):
          z_mul = tfl.dense(
              bits, final_filters, name="unbottleneck_mul"
          )
          if not hparams.complex_addn:
              return layer + z_mul
          layer *= tf.nn.sigmoid(z_mul)
          z_add = tfl.dense(
              bits, final_filters, name="unbottleneck_add"
          )
          layer += z_add
          return layer
  
      if not self.is_training:
          if hparams.full_latent_tower:
              rand = tf.random_uniform(
                  layer_shape[:-1] + [hparams.bottleneck_bits]
              )
              bits = 2.0 * tf.to_float(tf.less(0.5, rand)) - 1.0
          else:
              bits, _ = discretization.predict_bits_with_lstm(
                  layer,
                  hparams.latent_predictor_state_size,
                  hparams.bottleneck_bits,
                  temperature=hparams.latent_predictor_temperature,
              )
              bits = tf.expand_dims(
                  tf.expand_dims(bits, axis=1), axis=2
              )
          return add_bits(layer, bits), 0.0
  
      # Embed.
      frames = tf.concat(inputs + [target], axis=-1)
      x = tfl.dense(
          frames,
          filters,
          name="latent_embed",
          bias_initializer=tf.random_normal_initializer(stddev=0.01),
      )
      x = common_attention.add_timing_signal_nd(x)
  
      # Add embedded action if present.
      if action is not None:
          x = common_video.inject_additional_input(
              x, action, "action_enc_latent", hparams.action_injection
          )
  
      if hparams.full_latent_tower:
          for i in range(hparams.num_compress_steps):
              with tf.variable_scope("latent_downstride%d" % i):
                  x = common_layers.make_even_size(x)
                  if i < hparams.filter_double_steps:
                      filters *= 2
                  x = common_attention.add_timing_signal_nd(x)
                  x = tfl.conv2d(
                      x,
                      filters,
                      kernel,
                      activation=activation_fn,
                      strides=(2, 2),
                      padding="SAME",
                  )
                  x = common_layers.layer_norm(x)
      else:
          x = common_layers.double_discriminator(x)
          x = tf.expand_dims(tf.expand_dims(x, axis=1), axis=1)
  
      bits, bits_clean = discretization.tanh_discrete_bottleneck(
          x,
          hparams.bottleneck_bits,
          hparams.bottleneck_noise,
          hparams.discretize_warmup_steps,
          hparams.mode,
      )
      if not hparams.full_latent_tower:
          _, pred_loss = discretization.predict_bits_with_lstm(
              layer,
              hparams.latent_predictor_state_size,
              hparams.bottleneck_bits,
              target_bits=bits_clean,
          )
          # Mix bits from latent with predicted bits on forward pass as a noise.
          if hparams.latent_rnn_max_sampling > 0.0:
              with tf.variable_scope(
                  tf.get_variable_scope(), reuse=True
              ):
                  bits_pred, _ = discretization.predict_bits_with_lstm(
                      layer,
                      hparams.latent_predictor_state_size,
                      hparams.bottleneck_bits,
                      temperature=hparams.latent_predictor_temperature,
                  )
                  bits_pred = tf.expand_dims(
                      tf.expand_dims(bits_pred, axis=1), axis=2
                  )
              # Be bits_pred on the forward pass but bits on the backward one.
              bits_pred = bits_clean + tf.stop_gradient(
                  bits_pred - bits_clean
              )
              # Select which bits to take from pred sampling with bit_p probability.
              which_bit = tf.random_uniform(
                  common_layers.shape_list(bits)
              )
              bit_p = common_layers.inverse_lin_decay(
                  hparams.latent_rnn_warmup_steps
              )
              bit_p *= hparams.latent_rnn_max_sampling
              bits = tf.where(which_bit < bit_p, bits_pred, bits)
  
      res = add_bits(layer, bits)
      # During training, sometimes skip the latent to help action-conditioning.
      res_p = common_layers.inverse_lin_decay(
          hparams.latent_rnn_warmup_steps / 2
      )
      res_p *= hparams.latent_use_max_probability
      res_rand = tf.random_uniform([layer_shape[0]])
      res = tf.where(res_rand < res_p, res, layer)
      return res, pred_loss
  
imports: from __future__ import absolute_import
  from __future__ import division
  from __future__ import print_function
  from tensor2tensor.layers import common_attention
  from tensor2tensor.layers import common_layers
  from tensor2tensor.layers import common_video
  from tensor2tensor.layers import discretization
  from tensor2tensor.models.video import base_vae
  from tensor2tensor.models.video import basic_deterministic
  from tensor2tensor.models.video import basic_deterministic_params
  from tensor2tensor.utils import registry
  import tensorflow.compat.v1
label_desc: Use image node_id 7 for calling the NextFrameBasicStochasticDiscrete obj's underlying member method code with example usage: obj.inject_latent(layer, inputs, target, action) and returns: res,  pred_loss, layer, add_bits,  int, unknown