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

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

#include <encoder.hpp>

Inheritance diagram for gpmp::ml::RecurrentAutoEncoder:
gpmp::ml::AutoEncoder

Public Member Functions

 RecurrentAutoEncoder (int input_size, int hidden_size, int output_size, double learning_rate)
 Constructor for the RecurrentAutoEncoder class. More...
 
virtual void train (const std::vector< std::vector< double >> &training_data, int epochs) override
 Trains the recurrent autoencoder on the given sequential data. More...
 
std::vector< double > recurr_fwd (const std::vector< double > &input, const std::vector< double > &previous_hidden)
 Performs a forward pass through the recurrent layer. 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< std::vector< double > > weights_recurrent
 Weight matrix for the recurrent connections. 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

RecurrentAutoEncoder class, a derived class from AutoEncoder.

Definition at line 475 of file encoder.hpp.

Constructor & Destructor Documentation

◆ RecurrentAutoEncoder()

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

Constructor for the RecurrentAutoEncoder 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 579 of file encoder.cpp.

583  : AutoEncoder(in_size, h_size, out_size, l_rate),
584  weights_recurrent(h_size, std::vector<double>(h_size, 0.0)) {
585 }
AutoEncoder(int input_size, int hidden_size, int output_size, double learning_rate)
Constructor for the AutoEncoder class.
Definition: encoder.cpp:92
std::vector< std::vector< double > > weights_recurrent
Weight matrix for the recurrent connections.
Definition: encoder.hpp:480

Member Function Documentation

◆ recurr_fwd()

std::vector< double > gpmp::ml::RecurrentAutoEncoder::recurr_fwd ( const std::vector< double > &  input,
const std::vector< double > &  previous_hidden 
)

Performs a forward pass through the recurrent layer.

Parameters
inputThe input vector
previous_hiddenThe hidden state from the previous time step
Returns
The hidden state for the current time step

Definition at line 633 of file encoder.cpp.

635  {
636  std::vector<double> recurrent_input(hidden_size, 0.0);
637 
638  // sum the weighted contributions from the current input and the previous
639  // hidden state
640  for (int i = 0; i < hidden_size; ++i) {
641  recurrent_input[i] = 0.0;
642  for (int j = 0; j < input_size; ++j) {
643  recurrent_input[i] += weights_input_hidden[j][i] * input[j];
644  }
645  for (int j = 0; j < hidden_size; ++j) {
646  recurrent_input[i] += weights_recurrent[j][i] * previous_hidden[j];
647  }
648  // activation function
649  // recurrent_input[i] = 1.0 / (1.0 + std::exp(-recurrent_input[i]));
650  recurrent_input[i] = sigmoid({recurrent_input[i]})[0];
651  }
652 
653  return recurrent_input;
654 }
std::vector< std::vector< double > > weights_input_hidden
Weights from the input layer to the hidden layer.
Definition: encoder.hpp:97
int hidden_size
Size of the hidden layer.
Definition: encoder.hpp:74
std::vector< double > sigmoid(const std::vector< double > &x)
Sigmoid activation function.
Definition: encoder.cpp:117
int input_size
Size of the input layer.
Definition: encoder.hpp:66

◆ train()

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

Trains the recurrent autoencoder on the given sequential data.

Overrides the train method in the base class for handling temporal dependencies

Parameters
training_dataThe training data, which is a sequence of input vectors
epochsThe number of training epochs

Reimplemented from gpmp::ml::AutoEncoder.

Definition at line 587 of file encoder.cpp.

589  {
590  for (int epoch = 0; epoch < epochs; ++epoch) {
591  std::vector<double> previous_hidden(hidden_size, 0.0);
592 
593  for (const auto &input : training_data) {
594  // forward pass
595  std::vector<double> hidden = recurr_fwd(input, previous_hidden);
596  std::vector<double> output = forward(hidden);
597 
598  // backward pass (gradient descent)
599  for (int i = 0; i < output_size; ++i) {
600  for (int j = 0; j < hidden_size; ++j) {
601  weights_hidden_output[j][i] -=
602  learning_rate * (output[i] - input[i]) * hidden[j];
603  }
604  }
605 
606  for (int i = 0; i < hidden_size; ++i) {
607  for (int j = 0; j < input_size; ++j) {
608  double error = 0;
609  for (int k = 0; k < output_size; ++k) {
610  error += (output[k] - input[k]) *
611  weights_hidden_output[i][k];
612  }
613  weights_input_hidden[j][i] -= learning_rate * error *
614  input[j] * (1 - hidden[i]) *
615  hidden[i];
616  }
617  }
618 
619  // recurrent weights update
620  for (int i = 0; i < hidden_size; ++i) {
621  for (int j = 0; j < hidden_size; ++j) {
622  weights_recurrent[j][i] -=
623  learning_rate * (hidden[i] - previous_hidden[i]) *
624  hidden[j];
625  }
626  }
627 
628  previous_hidden = hidden;
629  }
630  }
631 }
std::vector< std::vector< double > > weights_hidden_output
Weights from the hidden layer to the output layer.
Definition: encoder.hpp:105
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
std::vector< double > recurr_fwd(const std::vector< double > &input, const std::vector< double > &previous_hidden)
Performs a forward pass through the recurrent layer.
Definition: encoder.cpp:633

Referenced by main().

Member Data Documentation

◆ weights_recurrent

std::vector<std::vector<double> > gpmp::ml::RecurrentAutoEncoder::weights_recurrent

Weight matrix for the recurrent connections.

Definition at line 480 of file encoder.hpp.


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