class_docstr: Densely-connected layer class with reparameterization estimator.
  
  This layer implements the Bayesian variational inference analogue to
  a dense layer by assuming the `kernel` and/or the `bias` are drawn
  from distributions. By default, the layer implements a stochastic
  forward pass via sampling from the kernel and bias posteriors,
  
  ```none
  kernel, bias ~ posterior
  outputs = activation(matmul(inputs, kernel) + bias)
  ```
  
  It uses the reparameterization estimator [(Kingma and Welling, 2014)][1],
  which performs a Monte Carlo approximation of the distribution integrating
  over the `kernel` and `bias`.
  
  The arguments permit separate specification of the surrogate posterior
  (`q(W|x)`), prior (`p(W)`), and divergence for both the `kernel` and `bias`
  distributions.
  
  Upon being built, this layer adds losses (accessible via the `losses`
  property) representing the divergences of `kernel` and/or `bias` surrogate
  posteriors and their respective priors. When doing minibatch stochastic
  optimization, make sure to scale this loss such that it is applied just once
  per epoch (e.g. if `kl` is the sum of `losses` for each element of the batch,
  you should pass `kl / num_examples_per_epoch` to your optimizer).
  
  You can access the `kernel` and/or `bias` posterior and prior distributions
  after the layer is built via the `kernel_posterior`, `kernel_prior`,
  `bias_posterior` and `bias_prior` properties.
  
  #### Examples
  
  We illustrate a Bayesian neural network with [variational inference](
  https://en.wikipedia.org/wiki/Variational_Bayesian_methods),
  assuming a dataset of `features` and `labels`.
  
  ```python
  import tensorflow as tf
  import tensorflow_probability as tfp
  
  model = tf_keras.Sequential([
      tfp.layers.DenseReparameterization(512, activation=tf.nn.relu),
      tfp.layers.DenseReparameterization(10),
  ])
  
  logits = model(features)
  neg_log_likelihood = tf.nn.softmax_cross_entropy_with_logits(
      labels=labels, logits=logits)
  kl = sum(model.losses)
  loss = neg_log_likelihood + kl
  train_op = tf.train.AdamOptimizer().minimize(loss)
  ```
  
  It uses reparameterization gradients to minimize the
  Kullback-Leibler divergence up to a constant, also known as the
  negative Evidence Lower Bound. It consists of the sum of two terms:
  the expected negative log-likelihood, which we approximate via
  Monte Carlo; and the KL divergence, which is added via regularizer
  terms which are arguments to the layer.
  
  #### References
  
  [1]: Diederik Kingma and Max Welling. Auto-Encoding Variational Bayes. In
       _International Conference on Learning Representations_, 2014.
       https://arxiv.org/abs/1312.6114
class_docstr_tok: ["Densely-connected","layer","class","with","reparameterization","estimator",".","This","layer","implements","the","Bayesian","variational","inference","analogue","to","a","dense","layer","by","assuming","the","`","kernel","`","and\/or","the","`","bias","`","are","drawn","from","distributions",".","By","default",",","the","layer","implements","a","stochastic","forward","pass","via","sampling","from","the","kernel","and","bias","posteriors",",","``","`","none","kernel",",","bias","~","posterior","outputs","=","activation","(","matmul","(","inputs",",","kernel",")","+","bias",")","``","`","It","uses","the","reparameterization","estimator","[","(","Kingma","and","Welling",",","2014",")","]","[","1","]",",","which","performs","a","Monte","Carlo","approximation","of","the","distribution","integrating","over","the","`","kernel","`","and","`","bias","`",".","The","arguments","permit","separate","specification","of","the","surrogate","posterior","(","`","q","(","W|x",")","`",")",",","prior","(","`","p","(","W",")","`",")",",","and","divergence","for","both","the","`","kernel","`","and","`","bias","`","distributions",".","Upon","being","built",",","this","layer","adds","losses","(","accessible","via","the","`","losses","`","property",")","representing","the","divergences","of","`","kernel","`","and\/or","`","bias","`","surrogate","posteriors","and","their","respective","priors",".","When","doing","minibatch","stochastic","optimization",",","make","sure","to","scale","this","loss","such","that","it","is","applied","just","once","per","epoch","(","e.g",".","if","`","kl","`","is","the","sum","of","`","losses","`","for","each","element","of","the","batch",",","you","should","pass","`","kl","\/","num_examples_per_epoch","`","to","your","optimizer",")",".","You","can","access","the","`","kernel","`","and\/or","`","bias","`","posterior","and","prior","distributions","after","the","layer","is","built","via","the","`","kernel_posterior","`",",","`","kernel_prior","`",",","`","bias_posterior","`","and","`","bias_prior","`","properties",".","#","#","#","#","Examples","We","illustrate","a","Bayesian","neural","network","with","[","variational","inference","]","(","https",":","\/\/en.wikipedia.org\/wiki\/Variational_Bayesian_methods",")",",","assuming","a","dataset","of","`","features","`","and","`","labels","`",".","``","`","python","import","tensorflow","as","tf","import","tensorflow_probability","as","tfp","model","=","tf_keras.Sequential","(","[","tfp.layers.DenseReparameterization","(","512",",","activation=tf.nn.relu",")",",","tfp.layers.DenseReparameterization","(","10",")",",","]",")","logits","=","model","(","features",")","neg_log_likelihood","=","tf.nn.softmax_cross_entropy_with_logits","(","labels=labels",",","logits=logits",")","kl","=","sum","(","model.losses",")","loss","=","neg_log_likelihood","+","kl","train_op","=","tf.train.AdamOptimizer","(",")",".minimize","(","loss",")","``","`","It","uses","reparameterization","gradients","to","minimize","the","Kullback-Leibler","divergence","up","to","a","constant",",","also","known","as","the","negative","Evidence","Lower","Bound",".","It","consists","of","the","sum","of","two","terms",":","the","expected","negative","log-likelihood",",","which","we","approximate","via","Monte","Carlo",";","and","the","KL","divergence",",","which","is","added","via","regularizer","terms","which","are","arguments","to","the","layer",".","#","#","#","#","References","[","1","]",":","Diederik","Kingma","and","Max","Welling",".","Auto-Encoding","Variational","Bayes",".","In","_International","Conference","on","Learning","Representations_",",","2014.","https",":","\/\/arxiv.org\/abs\/1312.6114"]
code: def _apply_variational_kernel(self, inputs):
      self.kernel_posterior_tensor = self.kernel_posterior_tensor_fn(
          self.kernel_posterior
      )
      self.kernel_posterior_affine = None
      self.kernel_posterior_affine_tensor = None
      return tf.matmul(inputs, self.kernel_posterior_tensor)
  
imports: import tensorflow.compat.v2
  from tensorflow_probability.python import random
  from tensorflow_probability.python.distributions import independent
  from tensorflow_probability.python.distributions import kullback_leibler
  from tensorflow_probability.python.distributions import normal
  from tensorflow_probability.python.internal import docstring_util
  from tensorflow_probability.python.internal import tf_keras
  from tensorflow_probability.python.layers import util
  from tensorflow_probability.python.util import SeedStream
label_desc: Use image node_id 2 for calling the DenseReparameterization obj's underlying member method code with example usage: obj._apply_variational_kernel(inputs) and returns: tf