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

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

#include <encoder.hpp>

Inheritance diagram for gpmp::ml::ConcreteAutoEncoder:
gpmp::ml::AutoEncoder

Public Member Functions

 ConcreteAutoEncoder (int input_size, int hidden_size, int output_size, double learning_rate, double temperature)
 Constructor for the ConcreteAutoEncoder class. More...
 
virtual void train (const std::vector< std::vector< double >> &training_data, int epochs) override
 Trains the Concrete 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

double temperature
 Temperature parameter for the Concrete distribution. 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

ConcreteAutoEncoder class, a derived class from AutoEncoder.

Definition at line 351 of file encoder.hpp.

Constructor & Destructor Documentation

◆ ConcreteAutoEncoder()

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

Constructor for the ConcreteAutoEncoder 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
temperatureThe temperature parameter for the Concrete distribution

Definition at line 419 of file encoder.cpp.

424  : AutoEncoder(in_size, h_size, out_size, l_rate), temperature(temp) {
425 }
AutoEncoder(int input_size, int hidden_size, int output_size, double learning_rate)
Constructor for the AutoEncoder class.
Definition: encoder.cpp:92
double temperature
Temperature parameter for the Concrete distribution.
Definition: encoder.hpp:356

Member Function Documentation

◆ train()

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

Trains the Concrete autoencoder on the given training data.

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

Parameters
training_dataThe training data
epochsThe number of training epochs

Reimplemented from gpmp::ml::AutoEncoder.

Definition at line 427 of file encoder.cpp.

429  {
430  std::default_random_engine generator;
431  std::uniform_real_distribution<double> uniform_distribution(0.0, 1.0);
432 
433  for (int epoch = 0; epoch < epochs; ++epoch) {
434  for (const auto &input : training_data) {
435  // forward pass with Concrete distribution
436  std::vector<double> hidden;
437  for (int i = 0; i < hidden_size; ++i) {
438  double u = uniform_distribution(generator);
439  // Gumbel noise
440  double g = -log(-log(u));
441  double s = (input[i] + g) / temperature;
442  double p = 1.0 / (1.0 + exp(-s));
443  hidden.push_back(p);
444  }
445 
446  // backward pass (gradient descent)
447  for (int i = 0; i < output_size; ++i) {
448  for (int j = 0; j < hidden_size; ++j) {
449  weights_hidden_output[j][i] -=
450  learning_rate * (hidden[i] - input[i]) * hidden[j];
451  }
452  }
453 
454  for (int i = 0; i < hidden_size; ++i) {
455  for (int j = 0; j < input_size; ++j) {
456  double error = 0;
457  for (int k = 0; k < output_size; ++k) {
458  error += (hidden[k] - input[k]) *
459  weights_hidden_output[i][k];
460  }
461  weights_input_hidden[j][i] -= learning_rate * error *
462  input[j] * (1 - hidden[i]) *
463  hidden[i];
464  }
465  }
466  }
467  }
468 }
std::vector< std::vector< double > > weights_input_hidden
Weights from the input layer to the hidden layer.
Definition: encoder.hpp:97
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
double learning_rate
Learning rate for training the autoencoder.
Definition: encoder.hpp:89
int input_size
Size of the input layer.
Definition: encoder.hpp:66
static GLfloat u

References u.

Member Data Documentation

◆ temperature

double gpmp::ml::ConcreteAutoEncoder::temperature

Temperature parameter for the Concrete distribution.

Definition at line 356 of file encoder.hpp.


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