Module ml::autoencoder
autoencoders: plain, denoising, sparse, variational (VAE, beta-VAE, conditional VAE) and vector-quantized (VQ-VAE). MLP encoders and decoders over flattened rows, trained with Autograd::get -- or convolutional ones (ConvAutoencoder, ConvVae) built from convnet.h's ConvNet.
Each model's loss(x, y) method is its training loss, so each is a LossModel (module.h) and module.h's generic functions train any of them:
struct Optimizer opt = Optimizer::new(OPT_ADAM, (float)0.001);
model_add_params(&vae, &opt);
model_fit(&vae, &opt, &data, &cfg);
'y' is ignored except by a conditional VAE, which reads class indices from it.
Reconstruction losses: RECON_MSE for real-valued data, RECON_BCE for data in [0, 1] (images). With RECON_BCE the decoder outputs logits and the *_decode / *_reconstruct functions apply the sigmoid.
Structs
| AutoencoderConfig | x -> enc -> z (latent) -> dec -> x'. Variants are switched on in the config and can be combined: |
| Autoencoder | |
| VaeConfig | Kingma & Welling 2014. The encoder outputs mu and log sigma^2 of a diagonal Gaussian q(z|x); z = mu + sigma * eps (the reparameterization trick) is decoded back. Loss per sample: |
| Vae | |
| VqVaeConfig | van den Oord et al. 2017. The encoder output z_e is replaced by its nearest codebook vector z_q (a discrete code per row); gradients pass from z_q back to z_e unchanged (straight-through). Loss: |
| VqVae | |
| ConvAutoencoder | Plain convolutional autoencoder: the encoder's output is the code (end it with enc.dense(latent, ...) for a vector code). |
| ConvVae | Convolutional VAE: linear mu / log sigma^2 heads on the flattened encoder features, the loss of Vae. Reads latent, recon, beta, classes and seed from the config (hidden, hiddenLayers and act describe the nets you built and are ignored). classes > 0 makes it a conditional VAE: the label goes to the encoder as 'classes' extra input channels (one constant plane per class, see tensor_append_label_channels) and to the decoder as a one-hot appended to the code. So for C-channel images the encoder takes C + classes channels, the decoder latent + classes inputs and outputs C channels. 'y' holds the class indices, as for Vae. |
Macros
| RECON_MSE | |
| RECON_BCE |