View source: R/latent_sampling.R
| Latent_sample | R Documentation |
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).
Latent_sample(z_mean, z_log_var)
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. |
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.
A TensorFlow tensor of latent samples with the same shape as z_mean.
VAE_train(), encoder_latent(), Encoder_weights(), decoder_model()
# 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)
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)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.