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

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

#include <encoder.hpp>

Inheritance diagram for gpmp::ml::SparseAutoEncoder:
gpmp::ml::AutoEncoder

Public Member Functions

 SparseAutoEncoder (int input_size, int hidden_size, int output_size, double learning_rate, double sparsity_weight, double sparsity_target)
 Constructor for the SparseAutoEncoder class. More...
 
void train (const std::vector< std::vector< double >> &training_data, int epochs) override
 Trains the sparse 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 sparsity_weight
 Weight for the sparsity penalty term. More...
 
double sparsity_target
 Target sparsity level. 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

SparseAutoEncoder class, a derived class from AutoEncoder.

Definition at line 193 of file encoder.hpp.

Constructor & Destructor Documentation

◆ SparseAutoEncoder()

gpmp::ml::SparseAutoEncoder::SparseAutoEncoder ( int  input_size,
int  hidden_size,
int  output_size,
double  learning_rate,
double  sparsity_weight,
double  sparsity_target 
)

Constructor for the SparseAutoEncoder 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
sparsity_weightThe weight for the sparsity penalty term
sparsity_targetThe target sparsity level

Definition at line 221 of file encoder.cpp.

227  : AutoEncoder(in_size, h_size, out_size, l_rate), sparsity_weight(s_weight),
228  sparsity_target(s_target) {
229 }
AutoEncoder(int input_size, int hidden_size, int output_size, double learning_rate)
Constructor for the AutoEncoder class.
Definition: encoder.cpp:92
double sparsity_target
Target sparsity level.
Definition: encoder.hpp:203
double sparsity_weight
Weight for the sparsity penalty term.
Definition: encoder.hpp:198

Member Function Documentation

◆ train()

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

Trains the sparse autoencoder on the given training data.

Overrides the train method in the base class with sparsity considerations

Parameters
training_dataThe training data
epochsThe number of training epochs

Reimplemented from gpmp::ml::AutoEncoder.

Definition at line 231 of file encoder.cpp.

233  {
234 
235  const double SPARSITY_TARGET_DECAY = 0.1;
236 
237  for (int epoch = 0; epoch < epochs; ++epoch) {
238  for (const auto &current_input : training_data) {
239  // forward pass
240  std::vector<double> hidden = forward(current_input);
241 
242  // backward pass (gradient descent)
243  for (int i = 0; i < output_size; ++i) {
244  for (int j = 0; j < hidden_size; ++j) {
245  weights_hidden_output[j][i] -=
246  learning_rate * (hidden[i] - current_input[i]) *
247  hidden[j];
248  }
249  }
250 
251  for (int i = 0; i < hidden_size; ++i) {
252  for (int j = 0; j < input_size; ++j) {
253  double error = 0;
254  for (int k = 0; k < output_size; ++k) {
255  error += (hidden[k] - current_input[k]) *
256  weights_hidden_output[i][k];
257  }
258 
259  double sparsity_term =
260  sparsity_weight * (sparsity_target - hidden[i]);
261 
262  weights_input_hidden[j][i] -=
263  learning_rate * (error + sparsity_term) *
264  current_input[j] * (1 - hidden[i]) * hidden[i];
265  }
266  }
267 
268  for (int i = 0; i < hidden_size; ++i) {
269  double average_activation = 0.0;
270  for (const auto &input : training_data) {
271  std::vector<double> current_hidden = forward(input);
272  average_activation += current_hidden[i];
273  }
274  average_activation /= training_data.size();
276  (1.0 - SPARSITY_TARGET_DECAY) * sparsity_target +
277  SPARSITY_TARGET_DECAY * average_activation;
278  }
279  }
280  }
281 }
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

◆ sparsity_target

double gpmp::ml::SparseAutoEncoder::sparsity_target

Target sparsity level.

Definition at line 203 of file encoder.hpp.

◆ sparsity_weight

double gpmp::ml::SparseAutoEncoder::sparsity_weight

Weight for the sparsity penalty term.

Definition at line 198 of file encoder.hpp.


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