class_docstr: A Variational Autoencoder (VAE) implementation.
  
  Paper reference: https://arxiv.org/abs/1312.6114
  
  Arguments
  ---------
  encoder: torch.Module
      the encoder network
  
  decoder: torch.Module
      the decoder network
  
  mean: torch.Module
      the module that computes the mean
  
  log_var: torch.Module
      the module that computes the log variance
  
  mask_value: float
      The value with which outputs and latents will be masked
  
  len_dim: None
      the length dimension
  
  mask_latent: bool
      where to apply the length mask to the latent representation
  
  mask_out: bool
      whether to apply the length mask to the output
  
  out_mask_value: float
      the mask value used for the output
  
  latent_mask_value: float
      the mask value used for the latent representation
  
  latent_stochastic: bool
      if true, the "latent" parameter of VariationalAutoencoderOutput
      will be the latent space sample
  
      if false, it will be the mean
  
  Example
  -------
  The example below shows a very simple implementation of
  VAE, not suitable for actual experiments:
  
  >>> import torch
  >>> from torch import nn
  >>> from speechbrain.nnet.linear import Linear
  >>> vae_enc = Linear(n_neurons=16, input_size=128)
  >>> vae_dec = Linear(n_neurons=128, input_size=16)
  >>> vae_mean = Linear(n_neurons=16, input_size=16)
  >>> vae_log_var = Linear(n_neurons=16, input_size=16)
  >>> vae = VariationalAutoencoder(
  ...     encoder=vae_enc,
  ...     decoder=vae_dec,
  ...     mean=vae_mean,
  ...     log_var=vae_log_var,
  ... )
  >>> x = torch.randn(4, 10, 128)
  
  `train_sample` encodes a single batch and then reconstructs
  it
  
  >>> vae_out = vae.train_sample(x)
  >>> vae_out.rec.shape
  torch.Size([4, 10, 128])
  >>> vae_out.latent.shape
  torch.Size([4, 10, 16])
  >>> vae_out.mean.shape
  torch.Size([4, 10, 16])
  >>> vae_out.log_var.shape
  torch.Size([4, 10, 16])
  >>> vae_out.latent_sample.shape
  torch.Size([4, 10, 16])
  
  .encode() will return the mean corresponding
  to teh sample provided
  
  >>> x_enc = vae.encode(x)
  >>> x_enc.shape
  torch.Size([4, 10, 16])
  
  .reparameterize() performs the reparameterization
  trick
  
  >>> x_enc = vae.encoder(x)
  >>> mean = vae.mean(x_enc)
  >>> log_var = vae.log_var(x_enc)
  >>> x_repar = vae.reparameterize(mean, log_var)
  >>> x_repar.shape
  torch.Size([4, 10, 16])
docstr: Provides a data sample for training the autoencoder
  
  Arguments
  ---------
  x: torch.Tensor
      the source data (in the sample space)
  length: None
      the length (optional). If provided, latents and
      outputs will be masked
  
  
  Returns
  -------
  result: VariationalAutoencoderOutput
      a named tuple with the following values
      rec: torch.Tensor
          the reconstruction
      latent: torch.Tensor
          the latent space sample
      mean: torch.Tensor
          the mean of the latent representation
      log_var: torch.Tensor
          the logarithm of the variance of the latent representation
docstr_tok: ["Provides","a","data","sample","for","training","the","autoencoder","Arguments","--","--","--","--","-","x",":","torch.Tensor","the","source","data","(","in","the","sample","space",")","length",":","None","the","length","(","optional",")",".","If","provided",",","latents","and","outputs","will","be","masked","Returns","--","--","--","-","result",":","VariationalAutoencoderOutput","a","named","tuple","with","the","following","values","rec",":","torch.Tensor","the","reconstruction","latent",":","torch.Tensor","the","latent","space","sample","mean",":","torch.Tensor","the","mean","of","the","latent","representation","log_var",":","torch.Tensor","the","logarithm","of","the","variance","of","the","latent","representation"]
code: def train_sample(
      self, x, length=None, out_mask_value=None, latent_mask_value=None
  ):
      """Provides a data sample for training the autoencoder
  
      Arguments
      ---------
      x: torch.Tensor
          the source data (in the sample space)
      length: None
          the length (optional). If provided, latents and
          outputs will be masked
  
  
      Returns
      -------
      result: VariationalAutoencoderOutput
          a named tuple with the following values
          rec: torch.Tensor
              the reconstruction
          latent: torch.Tensor
              the latent space sample
          mean: torch.Tensor
              the mean of the latent representation
          log_var: torch.Tensor
              the logarithm of the variance of the latent representation
  
      """
      if out_mask_value is None:
          out_mask_value = self.out_mask_value
      if latent_mask_value is None:
          latent_mask_value = self.latent_mask_value
      encoder_out = self.encoder(x)
  
      mean = self.mean(encoder_out)
      log_var = self.log_var(encoder_out)
      latent_sample = self.reparameterize(mean, log_var)
      if self.latent_padding is not None:
          latent_sample, latent_length = self.latent_padding(
              latent_sample, length=length
          )
      else:
          latent_length = length
      if self.mask_latent and length is not None:
          latent_sample = clean_padding(
              latent_sample,
              latent_length,
              self.len_dim,
              latent_mask_value,
          )
      x_rec = self.decode(latent_sample)
      x_rec = trim_as(x_rec, x)
      if self.mask_out and length is not None:
          x_rec = clean_padding(
              x_rec, length, self.len_dim, out_mask_value
          )
  
      if self.latent_stochastic:
          latent = latent_sample
      else:
          latent, latent_length = self.latent_padding(
              mean, length=length
          )
  
      return VariationalAutoencoderOutput(
          x_rec, latent, mean, log_var, latent_sample, latent_length
      )
  
imports: import torch
  from torch import nn
  from collections import namedtuple
  from speechbrain.dataio.dataio import clean_padding
  from speechbrain.utils.data_utils import trim_as
  from speechbrain.processing.features import GlobalNorm
label_desc: Use image node_id 5 for calling the VariationalAutoencoder obj's underlying member method code with example usage: obj.train_sample(x, length, out_mask_value, latent_mask_value) and returns: VariationalAutoencoderOutput