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

A simple implementation of a vanilla autoencoder. More...

#include <encoder.hpp>

Inheritance diagram for gpmp::ml::AutoEncoder:
gpmp::ml::ConcreteAutoEncoder gpmp::ml::ContractiveAutoEncoder gpmp::ml::DenoisingAutoEncoder gpmp::ml::FullAutoEncoder gpmp::ml::MDLAutoEncoder gpmp::ml::RecurrentAutoEncoder gpmp::ml::SparseAutoEncoder gpmp::ml::VariationalAutoEncoder

Public Member Functions

 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...
 
virtual void train (const std::vector< std::vector< double >> &training_data, int epochs)
 Train the autoencoder on a dataset. 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

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

A simple implementation of a vanilla autoencoder.

This class represents a basic autoencoder with one hidden layer. It can be used for dimensionality reduction and feature learning

Definition at line 59 of file encoder.hpp.

Constructor & Destructor Documentation

◆ AutoEncoder()

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

Constructor for the AutoEncoder class.

Parameters
input_sizeSize of the input layer
hidden_sizeSize of the hidden layer
output_sizeSize of the output layer
learning_rateLearning rate for training the autoencoder

Definition at line 92 of file encoder.cpp.

96  : input_size(in_size), hidden_size(h_size), output_size(out_size),
97  learning_rate(l_rate) {
98 
99  // initialize weights randomly
100  weights_input_hidden.resize(input_size, std::vector<double>(hidden_size));
101  weights_hidden_output.resize(hidden_size, std::vector<double>(output_size));
102 
103  for (int i = 0; i < input_size; ++i) {
104  for (int j = 0; j < hidden_size; ++j) {
105  // random values between 0 and 1
106  weights_input_hidden[i][j] = (rand() % 1000) / 1000.0;
107  }
108  }
109  for (int i = 0; i < hidden_size; ++i) {
110  for (int j = 0; j < output_size; ++j) {
111  weights_hidden_output[i][j] = (rand() % 1000) / 1000.0;
112  }
113  }
114 }
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

References hidden_size, input_size, output_size, weights_hidden_output, and weights_input_hidden.

Member Function Documentation

◆ display()

void gpmp::ml::AutoEncoder::display ( )

Print the weights of the autoencoder.

Prints the weights from the input layer to the hidden layer and from the hidden layer to the output layer

Definition at line 203 of file encoder.cpp.

203  {
204  std::cout << "Input to Hidden Weights:\n";
205  for (int i = 0; i < input_size; ++i) {
206  for (int j = 0; j < hidden_size; ++j) {
207  std::cout << weights_input_hidden[i][j] << " ";
208  }
209  std::cout << "\n";
210  }
211 
212  std::cout << "\nHidden to Output Weights:\n";
213  for (int i = 0; i < hidden_size; ++i) {
214  for (int j = 0; j < output_size; ++j) {
215  std::cout << weights_hidden_output[i][j] << " ";
216  }
217  std::cout << "\n";
218  }
219 }

Referenced by main().

◆ forward()

std::vector< double > gpmp::ml::AutoEncoder::forward ( const std::vector< double > &  input)

Forward pass through the autoencoder.

Computes the output of the autoencoder given an input vector

Parameters
inputInput vector
Returns
Output vector of the autoencoder

Definition at line 126 of file encoder.cpp.

126  {
127  // forward passes
128  std::vector<double> hidden(hidden_size);
129  std::vector<double> output(output_size);
130 
131  // calculate hidden layer values
132  for (int i = 0; i < hidden_size; ++i) {
133  hidden[i] = 0;
134  for (int j = 0; j < input_size; ++j) {
135  hidden[i] += input[j] * weights_input_hidden[j][i];
136  }
137  hidden[i] = sigmoid({hidden[i]})[0];
138  }
139 
140  // calculate output layer values
141  for (int i = 0; i < output_size; ++i) {
142  output[i] = 0;
143  for (int j = 0; j < hidden_size; ++j) {
144  output[i] += hidden[j] * weights_hidden_output[j][i];
145  }
146  output[i] = sigmoid({output[i]})[0];
147  }
148 
149  return output;
150 }
std::vector< double > sigmoid(const std::vector< double > &x)
Sigmoid activation function.
Definition: encoder.cpp:117

Referenced by main().

◆ load()

void gpmp::ml::AutoEncoder::load ( const std::string &  filename)
virtual

Load model weights from a file.

Parameters
filenameThe name of the file to load the weights from

Definition at line 59 of file encoder.cpp.

59  {
60  std::ifstream file(filename, std::ios::in | std::ios::binary);
61 
62  if (file.is_open()) {
63  file.read(reinterpret_cast<char *>(&weights_input_hidden[0][0]),
64  weights_input_hidden.size() * weights_input_hidden[0].size() *
65  sizeof(double));
66  file.read(reinterpret_cast<char *>(&weights_hidden_output[0][0]),
67  weights_hidden_output.size() *
68  weights_hidden_output[0].size() * sizeof(double));
69 
70  file.close();
71  std::cout << "Model loaded successfully." << std::endl;
72  } else {
73  std::cerr << "Unable to open the file for loading." << std::endl;
74  }
75 }

◆ lrate_set()

void gpmp::ml::AutoEncoder::lrate_set ( double  initial_rate)

Set the initial learning rate.

Parameters
initial_rateThe initial learning rate

Definition at line 77 of file encoder.cpp.

77  {
78  learning_rate = initial_rate;
79 }

◆ lrate_update()

void gpmp::ml::AutoEncoder::lrate_update ( int  epoch)
virtual

Update the learning rate based on a schedule.

Parameters
epochThe current training epoch

Definition at line 81 of file encoder.cpp.

81  {
82  // reduce the learning rate by half every N epochs
83  const int decay_interval = 10;
84  if (epoch % decay_interval == 0) {
85  learning_rate /= 2.0;
86  std::cout << "Learning rate updated to: " << learning_rate
87  << " at epoch " << epoch << std::endl;
88  }
89  // TODO?
90 }

◆ save()

void gpmp::ml::AutoEncoder::save ( const std::string &  filename) const
virtual

Save the model weights to a file.

Parameters
filenameThe name of the file to save the weights to

Definition at line 41 of file encoder.cpp.

41  {
42  std::ofstream file(filename, std::ios::out | std::ios::binary);
43 
44  if (file.is_open()) {
45  file.write(reinterpret_cast<const char *>(&weights_input_hidden[0][0]),
46  weights_input_hidden.size() *
47  weights_input_hidden[0].size() * sizeof(double));
48  file.write(reinterpret_cast<const char *>(&weights_hidden_output[0][0]),
49  weights_hidden_output.size() *
50  weights_hidden_output[0].size() * sizeof(double));
51 
52  file.close();
53  std::cout << "Model saved successfully." << std::endl;
54  } else {
55  std::cerr << "Unable to open the file for saving." << std::endl;
56  }
57 }

References weights_hidden_output, and weights_input_hidden.

◆ sigmoid()

std::vector< double > gpmp::ml::AutoEncoder::sigmoid ( const std::vector< double > &  x)

Sigmoid activation function.

Applies the sigmoid activation function element-wise to the input vector

Parameters
xInput vector
Returns
Result of applying the sigmoid function to each element of the input vector

Definition at line 117 of file encoder.cpp.

117  {
118  std::vector<double> result;
119  for (double val : x) {
120  result.push_back(1.0 / (1.0 + exp(-val)));
121  }
122  return result;
123 }

◆ train()

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

Train the autoencoder on a dataset.

Uses gradient descent to train the autoencoder on the provided training data

Parameters
training_dataMatrix containing training data
epochsNumber of training epochs

Reimplemented in gpmp::ml::FullAutoEncoder, gpmp::ml::RecurrentAutoEncoder, gpmp::ml::VariationalAutoEncoder, gpmp::ml::ConcreteAutoEncoder, gpmp::ml::MDLAutoEncoder, gpmp::ml::ContractiveAutoEncoder, gpmp::ml::DenoisingAutoEncoder, and gpmp::ml::SparseAutoEncoder.

Definition at line 152 of file encoder.cpp.

154  {
155  for (int epoch = 0; epoch < epochs; ++epoch) {
156  for (const auto &input : training_data) {
157  // forward pass
158  std::vector<double> hidden(hidden_size);
159  std::vector<double> output(output_size);
160 
161  // calculate hidden layer values
162  for (int i = 0; i < hidden_size; ++i) {
163  hidden[i] = 0;
164  for (int j = 0; j < input_size; ++j) {
165  hidden[i] += input[j] * weights_input_hidden[j][i];
166  }
167  hidden[i] = sigmoid({hidden[i]})[0];
168  }
169 
170  // calculate output layer values
171  for (int i = 0; i < output_size; ++i) {
172  output[i] = 0;
173  for (int j = 0; j < hidden_size; ++j) {
174  output[i] += hidden[j] * weights_hidden_output[j][i];
175  }
176  output[i] = sigmoid({output[i]})[0];
177  }
178 
179  // backward pass (gradient descent)
180  for (int i = 0; i < output_size; ++i) {
181  for (int j = 0; j < hidden_size; ++j) {
182  weights_hidden_output[j][i] -=
183  learning_rate * (output[i] - input[i]) * hidden[j];
184  }
185  }
186 
187  for (int i = 0; i < hidden_size; ++i) {
188  for (int j = 0; j < input_size; ++j) {
189  double error = 0;
190  for (int k = 0; k < output_size; ++k) {
191  error += (output[k] - input[k]) *
192  weights_hidden_output[i][k];
193  }
194  weights_input_hidden[j][i] -= learning_rate * error *
195  input[j] * (1 - hidden[i]) *
196  hidden[i];
197  }
198  }
199  }
200  }
201 }

Referenced by main().

Member Data Documentation

◆ hidden_size

int gpmp::ml::AutoEncoder::hidden_size

Size of the hidden layer.

The number of nodes in the hidden layer, which forms the compressed representation

Definition at line 74 of file encoder.hpp.

Referenced by AutoEncoder().

◆ input_size

int gpmp::ml::AutoEncoder::input_size

Size of the input layer.

The number of input nodes in the autoencoder

Definition at line 66 of file encoder.hpp.

Referenced by AutoEncoder().

◆ learning_rate

double gpmp::ml::AutoEncoder::learning_rate

Learning rate for training the autoencoder.

The rate at which the weights are updated during the training process

Definition at line 89 of file encoder.hpp.

◆ output_size

int gpmp::ml::AutoEncoder::output_size

Size of the output layer.

The number of output nodes in the autoencoder, which should match the input size

Definition at line 82 of file encoder.hpp.

Referenced by AutoEncoder().

◆ weights_hidden_output

std::vector<std::vector<double> > gpmp::ml::AutoEncoder::weights_hidden_output

Weights from the hidden layer to the output layer.

A matrix representing the weights connecting the hidden layer to the output layer

Definition at line 105 of file encoder.hpp.

Referenced by AutoEncoder(), and save().

◆ weights_input_hidden

std::vector<std::vector<double> > gpmp::ml::AutoEncoder::weights_input_hidden

Weights from the input layer to the hidden layer.

A matrix representing the weights connecting the input layer to the hidden layer

Definition at line 97 of file encoder.hpp.

Referenced by AutoEncoder(), and save().


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