class_docstr: Densely-connected layer class with local 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 = matmul(inputs, kernel) + bias
  ```
  
  It uses the local reparameterization estimator [(Kingma et al., 2015)][1],
  which performs a Monte Carlo approximation of the distribution on the hidden
  units induced by the `kernel` and `bias`. The default `kernel_posterior_fn`
  is a normal distribution which factorizes across all elements of the weight
  matrix and bias vector. Unlike [1]'s multiplicative parameterization, this
  distribution has trainable location and scale parameters which is known as
  an additive noise parameterization [(Molchanov et al., 2017)][2].
  
  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 images and length-10 one-hot `targets`.
  
  ```python
  # Using the following substitution, see:
  tfn = tfp.experimental.nn
  help(tfn.AffineVariationalReparameterization)
  BayesAffine =  tfn.AffineVariationalReparameterizationLocal
  ```
  
  This example 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, Tim Salimans, and Max Welling. Variational Dropout and
       the Local Reparameterization Trick. In _Neural Information Processing
       Systems_, 2015. https://arxiv.org/abs/1506.02557
  [2]: Dmitry Molchanov, Arsenii Ashukha, Dmitry Vetrov. Variational Dropout
       Sparsifies Deep Neural Networks. In _International Conference on Machine
       Learning_, 2017. https://arxiv.org/abs/1701.05369
class_docstr_tok: ["Densely-connected","layer","class","with","local","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","=","matmul","(","inputs",",","kernel",")","+","bias","``","`","It","uses","the","local","reparameterization","estimator","[","(","Kingma","et","al.",",","2015",")","]","[","1","]",",","which","performs","a","Monte","Carlo","approximation","of","the","distribution","on","the","hidden","units","induced","by","the","`","kernel","`","and","`","bias","`",".","The","default","`","kernel_posterior_fn","`","is","a","normal","distribution","which","factorizes","across","all","elements","of","the","weight","matrix","and","bias","vector",".","Unlike","[","1","]","'s","multiplicative","parameterization",",","this","distribution","has","trainable","location","and","scale","parameters","which","is","known","as","an","additive","noise","parameterization","[","(","Molchanov","et","al.",",","2017",")","]","[","2","]",".","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","images","and","length-10","one-hot","`","targets","`",".","``","`","python","#","Using","the","following","substitution",",","see",":","tfn","=","tfp.experimental.nn","help","(","tfn.AffineVariationalReparameterization",")","BayesAffine","=","tfn.AffineVariationalReparameterizationLocal","``","`","This","example","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",",","Tim","Salimans",",","and","Max","Welling",".","Variational","Dropout","and","the","Local","Reparameterization","Trick",".","In","_Neural","Information","Processing","Systems_",",","2015.","https",":","\/\/arxiv.org\/abs\/1506.02557","[","2","]",":","Dmitry","Molchanov",",","Arsenii","Ashukha",",","Dmitry","Vetrov",".","Variational","Dropout","Sparsifies","Deep","Neural","Networks",".","In","_International","Conference","on","Machine","Learning_",",","2017.","https",":","\/\/arxiv.org\/abs\/1701.05369"]
code: def __call__(self, x):
      x = tf.convert_to_tensor(x, dtype=self.dtype, name="x")
      self._posterior_value = self.posterior_value_fn(
          self.posterior, seed=self._seed()
      )  # pylint: disable=not-callable
      (
          kernel_dist,
          bias_dist,
      ) = self.unpack_weights_fn(  # pylint: disable=not-callable
          self.posterior.sample_distributions(
              value=self.posterior_value
          )[0]
      )
      kernel_loc, kernel_scale = vi_lib.get_spherical_normal_loc_scale(
          kernel_dist
      )
      loc = tf.matmul(x, kernel_loc)
      scale = tf.sqrt(tf.matmul(tf.square(x), tf.square(kernel_scale)))
      _, sampled_bias = self.unpack_weights_fn(
          self.posterior_value
      )  # pylint: disable=not-callable
      if sampled_bias is not None:
          try:
              (
                  bias_loc,
                  bias_scale,
              ) = vi_lib.get_spherical_normal_loc_scale(bias_dist)
              is_bias_spherical_normal = True
          except TypeError:
              is_bias_spherical_normal = False
          if is_bias_spherical_normal:
              loc = loc + bias_loc
              scale = tf.sqrt(tf.square(scale) + tf.square(bias_scale))
          else:
              loc = loc + sampled_bias
      y = normal_lib.Normal(loc=loc, scale=scale).sample(
          seed=self._seed()
      )
      if self.activation_fn is not None:
          y = self.activation_fn(y)
      return y
  
imports: import tensorflow.compat.v2
  from tensorflow_probability.python.distributions import distribution
  from tensorflow_probability.python.distributions import normal
  from tensorflow_probability.python.experimental.nn import layers
  from tensorflow_probability.python.experimental.nn import variational_base
  from tensorflow_probability.python.experimental.nn.util import kernel_bias
  from tensorflow_probability.python.internal import prefer_static
label_desc: Use image node_id 3 for calling the AffineVariationalReparameterizationLocal obj's underlying member method code with example usage: obj.__call__(x) and returns: y