Example source: https://github.com/fdavidcl/ruta/blob/master/vignettes/examples/autoencoder_variational.R


This example demonstrates the use of variational autoencoders with the Ruta package.

Define a variational autoencoder with 3-variable latent space. The encoding of a variational autoencoder is defined with variational_block.

library(magrittr)
library(keras)
library(ruta)

network <-
  input() +
  dense(256, "elu") +
  variational_block(3, seed = 42) +
  dense(256, "elu") +
  output("sigmoid")

learner <- autoencoder_variational(network, loss = "binary_crossentropy")

Load MNIST and normalize

Train

model <- learner %>% train(x_train, epochs = 5)

Sample the trained model

samples <- model %>% generate(dimensions = c(2, 3), fixed_values = 0.5)

Utility functions for plotting

Plot samples

Creating an animation from a sampling