class_docstr: Samples from a distribution with reparameterization trick, and
  transforms samples into specified size.
  
  Reparameterization allows gradients to be back-propagated through the
  stochastic samples. Used in, e.g., Variational Autoencoders (VAEs).
  
  Args:
      output_size: Size of output **excluding** the batch dimension. For
          example, set `output_size` to `dim` to generate output of
          shape `[batch_size, dim]`.
          Can be an `int`, a tuple of `int`, a Tensorshape, or a tuple of
          TensorShapes.
          For example, to transform inputs to have decoder state size, set
          `output_size=decoder.state_size`.
      hparams (dict, optional): Hyperparameters. Missing
          hyperparamerter will be set to default values. See
          :meth:`default_hparams` for the hyperparameter sturcture and
          default values.
  
  Example:
  
      .. code-block:: python
  
          cell = LSTMCell(num_units=256)
          # cell.state_size == LSTMStateTuple(c=256, h=256)
  
          connector = ReparameterizedStochasticConnector(cell.state_size)
  
          kwargs = {
              'loc': tf.zeros([batch_size, 10]),
              'scale_diag': tf.ones([batch_size, 10])
          }
          output, sample = connector(distribution_kwargs=kwargs)
          # output == LSTMStateTuple(c=tensor_of_shape_(batch_size, 256),
          #                          h=tensor_of_shape_(batch_size, 256))
          # sample == Tensor([batch_size, 10])
  
  
          kwargs = {
              'loc': tf.zeros([10]),
              'scale_diag': tf.ones([10])
          }
          output_, sample_ = connector(distribution_kwargs=kwargs,
                                       num_samples=batch_size_)
          # output_ == LSTMStateTuple(c=tensor_of_shape_(batch_size_, 256),
          #                           h=tensor_of_shape_(batch_size_, 256))
          # sample == Tensor([batch_size_, 10])
  
  .. document private functions
  .. automethod:: _build
class_docstr_tok: ["Samples","from","a","distribution","with","reparameterization","trick",",","and","transforms","samples","into","specified","size",".","Reparameterization","allows","gradients","to","be","back-propagated","through","the","stochastic","samples",".","Used","in",",","e.g.",",","Variational","Autoencoders","(","VAEs",")",".","Args",":","output_size",":","Size","of","output","*","*","excluding","*","*","the","batch","dimension",".","For","example",",","set","`","output_size","`","to","`","dim","`","to","generate","output","of","shape","`","[","batch_size",",","dim","]","`",".","Can","be","an","`","int","`",",","a","tuple","of","`","int","`",",","a","Tensorshape",",","or","a","tuple","of","TensorShapes",".","For","example",",","to","transform","inputs","to","have","decoder","state","size",",","set","`","output_size=decoder.state_size","`",".","hparams","(","dict",",","optional",")",":","Hyperparameters",".","Missing","hyperparamerter","will","be","set","to","default","values",".","See",":","meth",":","`","default_hparams","`","for","the","hyperparameter","sturcture","and","default","values",".","Example",":","..","code-block",":",":","python","cell","=","LSTMCell","(","num_units=256",")","#","cell.state_size","==","LSTMStateTuple","(","c=256",",","h=256",")","connector","=","ReparameterizedStochasticConnector","(","cell.state_size",")","kwargs","=","{","'loc","'",":","tf.zeros","(","[","batch_size",",","10","]",")",",","'scale_diag","'",":","tf.ones","(","[","batch_size",",","10","]",")","}","output",",","sample","=","connector","(","distribution_kwargs=kwargs",")","#","output","==","LSTMStateTuple","(","c=tensor_of_shape_","(","batch_size",",","256",")",",","#","h=tensor_of_shape_","(","batch_size",",","256",")",")","#","sample","==","Tensor","(","[","batch_size",",","10","]",")","kwargs","=","{","'loc","'",":","tf.zeros","(","[","10","]",")",",","'scale_diag","'",":","tf.ones","(","[","10","]",")","}","output_",",","sample_","=","connector","(","distribution_kwargs=kwargs",",","num_samples=batch_size_",")","#","output_","==","LSTMStateTuple","(","c=tensor_of_shape_","(","batch_size_",",","256",")",",","#","h=tensor_of_shape_","(","batch_size_",",","256",")",")","#","sample","==","Tensor","(","[","batch_size_",",","10","]",")","..","document","private","functions","..","automethod",":",":","_build"]
docstr: Samples from a distribution and optionally performs transformation
  with an MLP layer.
  
  The distribution must be reparameterizable, i.e.,
  `distribution.reparameterization_type = FULLY_REPARAMETERIZED`.
  
  Args:
      distribution: A instance of subclass of
          :tf_main:`TF Distribution <distributions/Distribution>`,
          or :tf_hmpg:`tensorflow_probability Distribution <probability>`,
          Can be a class, its name or module path, or a class instance.
      distribution_kwargs (dict, optional): Keyword arguments for the
          distribution constructor. Ignored if `distribution` is a
          class instance.
      transform (bool): Whether to perform MLP transformation of the
          distribution samples. If `False`, the structure/shape of a
          sample must match :attr:`output_size`.
      num_samples (optional): An `int` or `int` Tensor. Number of samples
          to generate. If not given, generate a single sample. Note
          that if batch size has already been included in
          `distribution`'s dimensionality, `num_samples` should be
          left as `None`.
  
  Returns:
      A tuple (output, sample), where
  
      - output: A Tensor or a (nested) tuple of Tensors with the same             structure and size of :attr:`output_size`. The batch dimension             equals :attr:`num_samples` if specified, or is determined by the             distribution dimensionality. If :attr:`transform` is `False`,             :attr:`output` will be equal to :attr:`sample`.
      - sample: The sample from the distribution, prior to transformation.
  
  Raises:
      ValueError: If distribution cannot be reparametrized.
      ValueError: The output does not match :attr:`output_size`.
docstr_tok: ["Samples","from","a","distribution","and","optionally","performs","transformation","with","an","MLP","layer",".","The","distribution","must","be","reparameterizable",",","i.e.",",","`","distribution.reparameterization_type","=","FULLY_REPARAMETERIZED","`",".","Args",":","distribution",":","A","instance","of","subclass","of",":","tf_main",":","`","TF","Distribution","<","distributions\/Distribution",">","`",",","or",":","tf_hmpg",":","`","tensorflow_probability","Distribution","<","probability",">","`",",","Can","be","a","class",",","its","name","or","module","path",",","or","a","class","instance",".","distribution_kwargs","(","dict",",","optional",")",":","Keyword","arguments","for","the","distribution","constructor",".","Ignored","if","`","distribution","`","is","a","class","instance",".","transform","(","bool",")",":","Whether","to","perform","MLP","transformation","of","the","distribution","samples",".","If","`","False","`",",","the","structure\/shape","of","a","sample","must","match",":","attr",":","`","output_size","`",".","num_samples","(","optional",")",":","An","`","int","`","or","`","int","`","Tensor",".","Number","of","samples","to","generate",".","If","not","given",",","generate","a","single","sample",".","Note","that","if","batch","size","has","already","been","included","in","`","distribution","`","'s","dimensionality",",","`","num_samples","`","should","be","left","as","`","None","`",".","Returns",":","A","tuple","(","output",",","sample",")",",","where","-","output",":","A","Tensor","or","a","(","nested",")","tuple","of","Tensors","with","the","same","structure","and","size","of",":","attr",":","`","output_size","`",".","The","batch","dimension","equals",":","attr",":","`","num_samples","`","if","specified",",","or","is","determined","by","the","distribution","dimensionality",".","If",":","attr",":","`","transform","`","is","`","False","`",",",":","attr",":","`","output","`","will","be","equal","to",":","attr",":","`","sample","`",".","-","sample",":","The","sample","from","the","distribution",",","prior","to","transformation",".","Raises",":","ValueError",":","If","distribution","can","not","be","reparametrized",".","ValueError",":","The","output","does","not","match",":","attr",":","`","output_size","`","."]
code: def _build(
      self,
      distribution="MultivariateNormalDiag",
      distribution_kwargs=None,
      transform=True,
      num_samples=None,
  ):
      """Samples from a distribution and optionally performs transformation
          with an MLP layer.
  
          The distribution must be reparameterizable, i.e.,
          `distribution.reparameterization_type = FULLY_REPARAMETERIZED`.
  
          Args:
              distribution: A instance of subclass of
                  :tf_main:`TF Distribution <distributions/Distribution>`,
                  or :tf_hmpg:`tensorflow_probability Distribution <probability>`,
                  Can be a class, its name or module path, or a class instance.
              distribution_kwargs (dict, optional): Keyword arguments for the
                  distribution constructor. Ignored if `distribution` is a
                  class instance.
              transform (bool): Whether to perform MLP transformation of the
                  distribution samples. If `False`, the structure/shape of a
                  sample must match :attr:`output_size`.
              num_samples (optional): An `int` or `int` Tensor. Number of samples
                  to generate. If not given, generate a single sample. Note
                  that if batch size has already been included in
                  `distribution`'s dimensionality, `num_samples` should be
                  left as `None`.
  
          Returns:
              A tuple (output, sample), where
  
              - output: A Tensor or a (nested) tuple of Tensors with the same \
              structure and size of :attr:`output_size`. The batch dimension \
              equals :attr:`num_samples` if specified, or is determined by the \
              distribution dimensionality. If :attr:`transform` is `False`, \
              :attr:`output` will be equal to :attr:`sample`.
              - sample: The sample from the distribution, prior to transformation.
  
          Raises:
              ValueError: If distribution cannot be reparametrized.
              ValueError: The output does not match :attr:`output_size`.
          """
      dstr = check_or_get_instance(
          distribution,
          distribution_kwargs,
          [
              "tensorflow.distributions",
              "tensorflow_probability.distributions",
              "texar.tf.custom",
          ],
      )
  
      if dstr.reparameterization_type == tf_dstr.NOT_REPARAMETERIZED:
          raise ValueError(
              "Distribution is not reparameterized: %s" % dstr.name
          )
  
      if num_samples:
          sample = dstr.sample(num_samples)
      else:
          sample = dstr.sample()
  
      # if dstr.event_shape == []:
      #    sample = tf.reshape(
      #        sample,
      #        sample.shape.concatenate(tf.TensorShape(1)))
  
      # sample = tf.cast(sample, tf.float32)
      if transform:
          fn_modules = [
              "tensorflow",
              "tensorflow.nn",
              "texar.tf.custom",
          ]
          activation_fn = get_function(
              self.hparams.activation_fn, fn_modules
          )
          output = _mlp_transform(
              sample, self._output_size, activation_fn
          )
      else:
          output = sample
  
      _assert_same_size(output, self._output_size)
      if not self._built:
          self._add_internal_trainable_variables()
          self._built = True
  
      return output, sample
  
imports: import numpy
  import tensorflow
  from tensorflow import distributions
  from tensorflow.python.util import nest
  from texar.tf.modules.connectors.connector_base import ConnectorBase
  from texar.tf.core import layers
  from texar.tf.utils.utils import get_function, check_or_get_instance
label_desc: Use image node_id 3 for calling the ReparameterizedStochasticConnector obj's underlying member method code with example usage: obj._build(distribution, distribution_kwargs, transform, num_samples) and returns: output,  sample