class_docstr: Neural Autoregressive Distribution Estimator [1].
  
  [1]: https://arxiv.org/abs/1605.02226
  
  Args:
    num_dims: The number of binary dimensions for each observation.
    num_hidden: The number of hidden units in the NADE.
    internal_bias: Whether the model should maintain its own bias varaibles.
        Otherwise, external values must be passed to `log_prob` and `sample`.
class_docstr_tok: ["Neural","Autoregressive","Distribution","Estimator","[","1","]",".","[","1","]",":","https",":","\/\/arxiv.org\/abs\/1605.02226","Args",":","num_dims",":","The","number","of","binary","dimensions","for","each","observation",".","num_hidden",":","The","number","of","hidden","units","in","the","NADE",".","internal_bias",":","Whether","the","model","should","maintain","its","own","bias","varaibles",".","Otherwise",",","external","values","must","be","passed","to","`","log_prob","`","and","`","sample","`","."]
docstr: Generate samples for the batch from the NADE.
  
  Args:
    b_enc: External encoder bias terms (`b` in [1]), sized
        `[batch_size, num_hidden]`, or None if the internal bias term should
        be used.
    b_dec: External decoder bias terms (`c` in [1]), sized
        `[batch_size, num_dims]`, or None if the internal bias term should
        be used.
    n: The number of samples to generate, or None, if the batch size of
        `b_enc` should be used.
    temperature: The amount to divide the logits by before sampling
        each Bernoulli, or None if a threshold of 0.5 should be used instead
        of sampling.
  
  Returns:
    sample: The generated samples, sized `[batch_size, num_dims]`.
    log_prob: The log probabilities of each observation in the batch, sized
        `[batch_size]`.
docstr_tok: ["Generate","samples","for","the","batch","from","the","NADE",".","Args",":","b_enc",":","External","encoder","bias","terms","(","`","b","`","in","[","1","]",")",",","sized","`","[","batch_size",",","num_hidden","]","`",",","or","None","if","the","internal","bias","term","should","be","used",".","b_dec",":","External","decoder","bias","terms","(","`","c","`","in","[","1","]",")",",","sized","`","[","batch_size",",","num_dims","]","`",",","or","None","if","the","internal","bias","term","should","be","used",".","n",":","The","number","of","samples","to","generate",",","or","None",",","if","the","batch","size","of","`","b_enc","`","should","be","used",".","temperature",":","The","amount","to","divide","the","logits","by","before","sampling","each","Bernoulli",",","or","None","if","a","threshold","of","0.5","should","be","used","instead","of","sampling",".","Returns",":","sample",":","The","generated","samples",",","sized","`","[","batch_size",",","num_dims","]","`",".","log_prob",":","The","log","probabilities","of","each","observation","in","the","batch",",","sized","`","[","batch_size","]","`","."]
code: def sample(self, b_enc=None, b_dec=None, n=None, temperature=None):
      """Generate samples for the batch from the NADE.
  
      Args:
        b_enc: External encoder bias terms (`b` in [1]), sized
            `[batch_size, num_hidden]`, or None if the internal bias term should
            be used.
        b_dec: External decoder bias terms (`c` in [1]), sized
            `[batch_size, num_dims]`, or None if the internal bias term should
            be used.
        n: The number of samples to generate, or None, if the batch size of
            `b_enc` should be used.
        temperature: The amount to divide the logits by before sampling
            each Bernoulli, or None if a threshold of 0.5 should be used instead
            of sampling.
  
      Returns:
        sample: The generated samples, sized `[batch_size, num_dims]`.
        log_prob: The log probabilities of each observation in the batch, sized
            `[batch_size]`.
      """
      b_enc = b_enc if b_enc is not None else self.b_enc
      b_dec = b_dec if b_dec is not None else self.b_dec
  
      batch_size = n or tf.shape(b_enc)[0]
  
      # Broadcast if needed.
      if b_enc.shape[0] == 1 != batch_size:
          b_enc = tf.tile(b_enc, [batch_size, 1])
      if b_dec.shape[0] == 1 != batch_size:
          b_dec = tf.tile(b_dec, [batch_size, 1])
  
      a_0 = b_enc
      sample_0 = []
      log_p_0 = tf.zeros([batch_size, 1])
  
      w_enc_arr = tf.unstack(self.w_enc)
      w_dec_arr = tf.unstack(self.w_dec_t)
      b_dec_arr = tf.unstack(
          tf.reshape(
              tf.transpose(b_dec), [self.num_dims, batch_size, 1]
          )
      )
  
      def loop_body(i, a, sample, log_p):
          """Accumulate hidden state, sample, and log probability for index i."""
          # Get weights and bias for time step.
          w_enc_i = w_enc_arr[i]
          w_dec_i = w_dec_arr[i]
          b_dec_i = b_dec_arr[i]
  
          cond_p_i, cond_l_i = self._cond_prob(a, w_dec_i, b_dec_i)
  
          if temperature is None:
              v_i = tf.to_float(tf.greater_equal(cond_p_i, 0.5))
          else:
              bernoulli = tfp.distributions.Bernoulli(
                  logits=cond_l_i / temperature, dtype=tf.float32
              )
              v_i = bernoulli.sample()
  
          # Accumulate sampled values.
          sample_new = sample + [v_i]
  
          # Get log probability for this value. Log space avoids numerical issues.
          log_p_i = v_i * _safe_log(cond_p_i) + (1 - v_i) * _safe_log(
              1 - cond_p_i
          )
  
          # Accumulate log probability.
          log_p_new = log_p + log_p_i
  
          # Encode value and add to hidden units.
          a_new = a + tf.matmul(v_i, w_enc_i)
  
          return a_new, sample_new, log_p_new
  
      a, sample, log_p = a_0, sample_0, log_p_0
      for i in range(self.num_dims):
          a, sample, log_p = loop_body(i, a, sample, log_p)
  
      return (
          tf.transpose(tf.squeeze(tf.stack(sample), [2])),
          tf.squeeze(log_p, squeeze_dims=[1]),
      )
  
file_path: magenta/magenta/common/nade.py
imports: from __future__ import absolute_import
  from __future__ import division
  from __future__ import print_function
  import math
  import tensorflow.compat.v1
  import tensorflow_probability
label_desc: Use image node_id 5 for calling the Nade obj's underlying member method code with example usage: obj.sample(b_enc, b_dec, n, temperature) and returns: tf,  tf, a_new,  sample_new,  log_p_new