Learn R Programming

autotab (version 0.1.3)

Latent_sample: Sample from the latent space

Description

Draws a stochastic sample from the latent space of a trained VAE given the mean (z_mean) and log-variance (z_log_var) outputs of the encoder. This operation implements the reparameterization trick: $$z = \mu + \sigma \odot \epsilon$$ where \(\epsilon \sim \mathcal{N}(0, I)\).

Usage

Latent_sample(z_mean, z_log_var)

Value

A TensorFlow tensor of latent samples with the same shape as z_mean.

Arguments

z_mean

TensorFlow tensor or R matrix. The mean values of the latent space.

z_log_var

TensorFlow tensor or R matrix. The log-variances of the latent space.

Details

The function is used internally within VAE_train() but can also be called directly to sample latent points and decode synthetic output. Typically, z_mean and z_log_var are obtained via encoder_latent() and the corresponding weights extracted using Encoder_weights().

  • The log-variance (z_log_var) is clamped between -10 and 10 to prevent numerical overflow or vanishing variance during training.

  • The standard deviation is lower-bounded by 1e-3 for stability.

This function returns a TensorFlow tensor representing the sampled latent points. Use as.matrix() or as.data.frame() to convert to an R matrix or data frame before passing to decoder_model() or other R functions.

See Also

VAE_train(), encoder_latent(), Encoder_weights(), decoder_model()

Examples

Run this code
# Suppose encoder_latent() returns z_mean and z_log_var
z_mean    <- matrix(rnorm(10), ncol = 5)
z_log_var <- matrix(rnorm(10), ncol = 5)

# \donttest{
if (reticulate::py_module_available("tensorflow")) {
  # Sample from latent space
  z_sample <- Latent_sample(z_mean, z_log_var)

  # Convert to R matrix for decoder prediction
  z_mat <- as.matrix(z_sample)

  # Suppose the computational graph was rebuilt using `decoder_model()`
  # and assigned to an object named `decoder`:
  # decoder_output <- predict(decoder, z_mat)
}
# }

Run the code above in your browser using DataLab