openGPMP
Open Source Mathematics Package
Public Member Functions | Public Attributes | List of all members
gpmp::ml::VariationalAutoEncoder Class Reference

VariationalAutoEncoder class, a derived class from AutoEncoder. More...

#include <encoder.hpp>

Inheritance diagram for gpmp::ml::VariationalAutoEncoder:
gpmp::ml::AutoEncoder

Public Member Functions

double sample_dist ()
 Samples from a standard normal distribution. More...
 
double reparameterize (double mean, double log_variance)
 Reparameterization trick for variational autoencoders. More...
 
std::vector< double > encoder (const std::vector< double > &input)
 Performs the forward pass for the encoder and returns hidden activations. More...
 
std::vector< double > decoder (const std::vector< double > &hidden_sampled)
 Performs the forward pass for the decoder and returns output activations. More...
 
void gradient_descent (const std::vector< double > &input, const std::vector< double > &output, const std::vector< double > &hidden_sampled)
 Performs the backward pass (gradient descent) updating weights. More...
 
 VariationalAutoEncoder (int input_size, int hidden_size, int output_size, double learning_rate)
 Constructor for the VariationalAutoEncoder class. More...
 
virtual void train (const std::vector< std::vector< double >> &training_data, int epochs) override
 Trains the Variational autoencoder on the given training data. More...
 
- Public Member Functions inherited from gpmp::ml::AutoEncoder
 AutoEncoder (int input_size, int hidden_size, int output_size, double learning_rate)
 Constructor for the AutoEncoder class. More...
 
std::vector< double > sigmoid (const std::vector< double > &x)
 Sigmoid activation function. More...
 
std::vector< double > forward (const std::vector< double > &input)
 Forward pass through the autoencoder. More...
 
void lrate_set (double initial_rate)
 Set the initial learning rate. More...
 
virtual void lrate_update (int epoch)
 Update the learning rate based on a schedule. More...
 
void display ()
 Print the weights of the autoencoder. More...
 
virtual void save (const std::string &filename) const
 Save the model weights to a file. More...
 
virtual void load (const std::string &filename)
 Load model weights from a file. More...
 

Public Attributes

std::vector< double > hidden_mean
 keeps track of hidden means More...
 
std::vector< double > hidden_log_variance
 keeps track of hidden log variance More...
 
- Public Attributes inherited from gpmp::ml::AutoEncoder
int input_size
 Size of the input layer. More...
 
int hidden_size
 Size of the hidden layer. More...
 
int output_size
 Size of the output layer. More...
 
double learning_rate
 Learning rate for training the autoencoder. More...
 
std::vector< std::vector< double > > weights_input_hidden
 Weights from the input layer to the hidden layer. More...
 
std::vector< std::vector< double > > weights_hidden_output
 Weights from the hidden layer to the output layer. More...
 

Detailed Description

VariationalAutoEncoder class, a derived class from AutoEncoder.

Definition at line 390 of file encoder.hpp.

Constructor & Destructor Documentation

◆ VariationalAutoEncoder()

gpmp::ml::VariationalAutoEncoder::VariationalAutoEncoder ( int  input_size,
int  hidden_size,
int  output_size,
double  learning_rate 
)

Constructor for the VariationalAutoEncoder class.

Parameters
input_sizeThe size of the input layer
hidden_sizeThe size of the hidden layer
output_sizeThe size of the output layer
learning_rateThe learning rate for training

Definition at line 470 of file encoder.cpp.

474  : AutoEncoder(in_size, h_size, out_size, l_rate) {
475 }
AutoEncoder(int input_size, int hidden_size, int output_size, double learning_rate)
Constructor for the AutoEncoder class.
Definition: encoder.cpp:92

Member Function Documentation

◆ decoder()

std::vector< double > gpmp::ml::VariationalAutoEncoder::decoder ( const std::vector< double > &  hidden_sampled)

Performs the forward pass for the decoder and returns output activations.

Parameters
hidden_sampledThe sampled hidden values.
Returns
The output activations.

Definition at line 504 of file encoder.cpp.

505  {
506  std::vector<double> output(output_size);
507 
508  for (int i = 0; i < output_size; ++i) {
509  output[i] = 0;
510  for (int j = 0; j < hidden_size; ++j) {
511  output[i] += hidden_sampled[j] * weights_hidden_output[j][i];
512  }
513  output[i] = sigmoid({output[i]})[0];
514  }
515 
516  return output;
517 }
std::vector< std::vector< double > > weights_hidden_output
Weights from the hidden layer to the output layer.
Definition: encoder.hpp:105
int hidden_size
Size of the hidden layer.
Definition: encoder.hpp:74
int output_size
Size of the output layer.
Definition: encoder.hpp:82
std::vector< double > sigmoid(const std::vector< double > &x)
Sigmoid activation function.
Definition: encoder.cpp:117

◆ encoder()

std::vector< double > gpmp::ml::VariationalAutoEncoder::encoder ( const std::vector< double > &  input)

Performs the forward pass for the encoder and returns hidden activations.

Parameters
inputThe input data.
Returns
The hidden activations.

Definition at line 490 of file encoder.cpp.

490  {
491  std::vector<double> hidden(hidden_size);
492 
493  for (int i = 0; i < hidden_size; ++i) {
494  hidden[i] = 0;
495  for (int j = 0; j < input_size; ++j) {
496  hidden[i] += input[j] * weights_input_hidden[j][i];
497  }
498  hidden[i] = sigmoid({hidden[i]})[0];
499  }
500 
501  return hidden;
502 }
std::vector< std::vector< double > > weights_input_hidden
Weights from the input layer to the hidden layer.
Definition: encoder.hpp:97
int input_size
Size of the input layer.
Definition: encoder.hpp:66

◆ gradient_descent()

void gpmp::ml::VariationalAutoEncoder::gradient_descent ( const std::vector< double > &  input,
const std::vector< double > &  output,
const std::vector< double > &  hidden_sampled 
)

Performs the backward pass (gradient descent) updating weights.

Parameters
inputThe input data.
outputThe output activations.
hidden_sampledThe sampled hidden values.

Definition at line 519 of file encoder.cpp.

522  {
523  for (int i = 0; i < output_size; ++i) {
524  for (int j = 0; j < hidden_size; ++j) {
525  weights_hidden_output[j][i] -=
526  learning_rate * (output[i] - input[i]) * hidden_sampled[j];
527  }
528  }
529 
530  for (int i = 0; i < hidden_size; ++i) {
531  for (int j = 0; j < input_size; ++j) {
532  double error = 0;
533  for (int k = 0; k < output_size; ++k) {
534  error += (output[k] - input[k]) * weights_hidden_output[i][k];
535  }
536  double hidden_gradient =
537  hidden_sampled[i] * (1 - hidden_sampled[i]);
538  // derivative of softplus
539  double log_variance_gradient =
540  1.0 / (1.0 + exp(-hidden_log_variance[i]));
541  weights_input_hidden[j][i] -=
542  learning_rate *
543  (error * hidden_gradient +
544  (hidden_sampled[i] - hidden_mean[i]) * hidden_gradient +
545  (hidden_log_variance[i] - log_variance_gradient) *
546  hidden_gradient) *
547  input[j];
548  }
549  }
550 }
double learning_rate
Learning rate for training the autoencoder.
Definition: encoder.hpp:89
std::vector< double > hidden_log_variance
keeps track of hidden log variance
Definition: encoder.hpp:399
std::vector< double > hidden_mean
keeps track of hidden means
Definition: encoder.hpp:395

◆ reparameterize()

double gpmp::ml::VariationalAutoEncoder::reparameterize ( double  mean,
double  log_variance 
)

Reparameterization trick for variational autoencoders.

Parameters
meanThe mean of the distribution
log_varianceThe log variance of the distribution
Returns
A sample from the specified distribution

Definition at line 483 of file encoder.cpp.

484  {
485  double standard_normal_sample = sample_dist();
486  return mean + exp(0.5 * log_variance) * standard_normal_sample;
487 }
double sample_dist()
Samples from a standard normal distribution.
Definition: encoder.cpp:477

◆ sample_dist()

double gpmp::ml::VariationalAutoEncoder::sample_dist ( )

Samples from a standard normal distribution.

Returns
A sample from the standard normal distribution.

Definition at line 477 of file encoder.cpp.

477  {
478  static std::default_random_engine generator;
479  static std::normal_distribution<double> distribution(0.0, 1.0);
480  return distribution(generator);
481 }

◆ train()

void gpmp::ml::VariationalAutoEncoder::train ( const std::vector< std::vector< double >> &  training_data,
int  epochs 
)
overridevirtual

Trains the Variational autoencoder on the given training data.

Overrides the train method in the base class with Variational autoencoder specifics

Parameters
training_dataThe training data
epochsThe number of training epochs

Reimplemented from gpmp::ml::AutoEncoder.

Definition at line 552 of file encoder.cpp.

554  {
555  std::default_random_engine generator;
556  std::normal_distribution<double> normal_distribution(0.0, 1.0);
557  std::vector<double> hidden_sampled;
558 
559  for (int epoch = 0; epoch < epochs; ++epoch) {
560  for (const auto &input : training_data) {
561  // forward pass (encoder)
562  hidden_mean = encoder(input);
563  hidden_log_variance = encoder(input);
564 
565  for (int i = 0; i < hidden_size; ++i) {
566  hidden_sampled.push_back(
568  }
569 
570  // forward pass (decoder)
571  std::vector<double> output = decoder(hidden_sampled);
572 
573  // backward pass (gradient descent)
574  gradient_descent(input, output, hidden_sampled);
575  }
576  }
577 }
std::vector< double > encoder(const std::vector< double > &input)
Performs the forward pass for the encoder and returns hidden activations.
Definition: encoder.cpp:490
void gradient_descent(const std::vector< double > &input, const std::vector< double > &output, const std::vector< double > &hidden_sampled)
Performs the backward pass (gradient descent) updating weights.
Definition: encoder.cpp:519
double reparameterize(double mean, double log_variance)
Reparameterization trick for variational autoencoders.
Definition: encoder.cpp:483
std::vector< double > decoder(const std::vector< double > &hidden_sampled)
Performs the forward pass for the decoder and returns output activations.
Definition: encoder.cpp:504

Member Data Documentation

◆ hidden_log_variance

std::vector<double> gpmp::ml::VariationalAutoEncoder::hidden_log_variance

keeps track of hidden log variance

Definition at line 399 of file encoder.hpp.

◆ hidden_mean

std::vector<double> gpmp::ml::VariationalAutoEncoder::hidden_mean

keeps track of hidden means

Definition at line 395 of file encoder.hpp.


The documentation for this class was generated from the following files: