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

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

#include <encoder.hpp>

Inheritance diagram for gpmp::ml::DenoisingAutoEncoder:
gpmp::ml::AutoEncoder

Public Member Functions

 DenoisingAutoEncoder (int input_size, int hidden_size, int output_size, double learning_rate, double corruption_level)
 Constructor for the DenoisingAutoEncoder class. More...
 
void train (const std::vector< std::vector< double >> &training_data, int epochs) override
 Adds noise to the input data and trains the denoising autoencoder. 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 corruption_level
 Probability of setting an input value to zero during training (dropout rate) 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

DenoisingAutoEncoder class, a derived class from AutoEncoder.

Definition at line 237 of file encoder.hpp.

Constructor & Destructor Documentation

◆ DenoisingAutoEncoder()

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

Constructor for the DenoisingAutoEncoder 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
corruption_levelThe probability of setting an input value to zero during training

Definition at line 283 of file encoder.cpp.

288  : AutoEncoder(in_size, h_size, out_size, l_rate),
289  corruption_level(c_level) {
290 }
AutoEncoder(int input_size, int hidden_size, int output_size, double learning_rate)
Constructor for the AutoEncoder class.
Definition: encoder.cpp:92
double corruption_level
Probability of setting an input value to zero during training (dropout rate)
Definition: encoder.hpp:243

Member Function Documentation

◆ train()

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

Adds noise to the input data and trains the denoising autoencoder.

Overrides the train method in the base class for denoising

Parameters
training_dataThe training data
epochsThe number of training epochs

Reimplemented from gpmp::ml::AutoEncoder.

Definition at line 292 of file encoder.cpp.

294  {
295  std::srand(std::time(0));
296 
297  for (int epoch = 0; epoch < epochs; ++epoch) {
298  for (const auto &input : training_data) {
299  // add noise to the input data
300  std::vector<double> noisy_input = input;
301  for (double &val : noisy_input) {
302  if ((std::rand() / (RAND_MAX + 1.0)) < corruption_level) {
303  // set to zero with probability corruption_level
304  val = 0.0;
305  }
306  }
307 
308  // forward pass
309  std::vector<double> hidden = forward(noisy_input);
310 
311  // backward pass (gradient descent)
312  for (int i = 0; i < output_size; ++i) {
313  for (int j = 0; j < hidden_size; ++j) {
314  weights_hidden_output[j][i] -=
315  learning_rate * (hidden[i] - input[i]) * hidden[j];
316  }
317  }
318 
319  for (int i = 0; i < hidden_size; ++i) {
320  for (int j = 0; j < input_size; ++j) {
321  double error = 0;
322  for (int k = 0; k < output_size; ++k) {
323  error += (hidden[k] - input[k]) *
324  weights_hidden_output[i][k];
325  }
326  weights_input_hidden[j][i] -= learning_rate * error *
327  noisy_input[j] *
328  (1 - hidden[i]) * hidden[i];
329  }
330  }
331  }
332  }
333 }
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
std::vector< double > forward(const std::vector< double > &input)
Forward pass through the autoencoder.
Definition: encoder.cpp:126
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

Referenced by main().

Member Data Documentation

◆ corruption_level

double gpmp::ml::DenoisingAutoEncoder::corruption_level

Probability of setting an input value to zero during training (dropout rate)

Definition at line 243 of file encoder.hpp.


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