openGPMP
Open Source Mathematics Package
encoder.hpp
Go to the documentation of this file.
1 /*************************************************************************
2  *
3  * Project
4  * _____ _____ __ __ _____
5  * / ____| __ \| \/ | __ \
6  * ___ _ __ ___ _ __ | | __| |__) | \ / | |__) |
7  * / _ \| '_ \ / _ \ '_ \| | |_ | ___/| |\/| | ___/
8  *| (_) | |_) | __/ | | | |__| | | | | | | |
9  * \___/| .__/ \___|_| |_|\_____|_| |_| |_|_|
10  * | |
11  * |_|
12  *
13  * Copyright (C) Akiel Aries, <akiel@akiel.org>, et al.
14  *
15  * This software is licensed as described in the file LICENSE, which
16  * you should have received as part of this distribution. The terms
17  * among other details are referenced in the official documentation
18  * seen here : https://akielaries.github.io/openGPMP/ along with
19  * important files seen in this project.
20  *
21  * You may opt to use, copy, modify, merge, publish, distribute
22  * and/or sell copies of the Software, and permit persons to whom
23  * the Software is furnished to do so, under the terms of the
24  * LICENSE file. As this is an Open Source effort, all implementations
25  * must be of the same methodology.
26  *
27  *
28  *
29  * This software is distributed on an AS IS basis, WITHOUT
30  * WARRANTY OF ANY KIND, either express or implied.
31  *
32  ************************************************************************/
33 
38 #ifndef ENCODER_HPP
39 #define ENCODER_HPP
40 
41 #include <string>
42 #include <tuple>
43 #include <vector>
44 
45 namespace gpmp {
46 
47 namespace ml {
48 
56 // TODO: this base class could probably use its own gradient descent method to
57 // use across all inheriting classes OR create a class of trainers to use in
58 // `trainers.cpp`?
59 class AutoEncoder {
60  public:
67 
75 
83 
89  double learning_rate;
90 
97  std::vector<std::vector<double>> weights_input_hidden;
98 
105  std::vector<std::vector<double>> weights_hidden_output;
106 
116  int hidden_size,
117  int output_size,
118  double learning_rate);
119 
129  std::vector<double> sigmoid(const std::vector<double> &x);
130 
139  std::vector<double> forward(const std::vector<double> &input);
140 
150  virtual void train(const std::vector<std::vector<double>> &training_data,
151  int epochs);
152 
158  void lrate_set(double initial_rate);
159 
165  virtual void lrate_update(int epoch);
166 
173  void display();
174 
180  virtual void save(const std::string &filename) const;
181 
187  virtual void load(const std::string &filename);
188 };
189 
194  public:
199 
204 
216  int hidden_size,
217  int output_size,
218  double learning_rate,
219  double sparsity_weight,
220  double sparsity_target);
221 
230  void train(const std::vector<std::vector<double>> &training_data,
231  int epochs) override;
232 };
233 
238  public:
244 
256  int hidden_size,
257  int output_size,
258  double learning_rate,
259  double corruption_level);
260 
269  void train(const std::vector<std::vector<double>> &training_data,
270  int epochs) override;
271 };
272 
277  public:
282 
293  int hidden_size,
294  int output_size,
295  double learning_rate,
296  double contractive_weight);
297 
306  virtual void train(const std::vector<std::vector<double>> &training_data,
307  int epochs) override;
308 };
309 
314 class MDLAutoEncoder : public AutoEncoder {
315  public:
319  double mdl_weight;
320 
331  int hidden_size,
332  int output_size,
333  double learning_rate,
334  double mdl_weight);
335 
344  virtual void train(const std::vector<std::vector<double>> &training_data,
345  int epochs) override;
346 };
347 
352  public:
356  double temperature;
357 
369  int hidden_size,
370  int output_size,
371  double learning_rate,
372  double temperature);
373 
383  virtual void train(const std::vector<std::vector<double>> &training_data,
384  int epochs) override;
385 };
386 
391  public:
395  std::vector<double> hidden_mean;
399  std::vector<double> hidden_log_variance;
400 
406  double sample_dist();
407 
415  double reparameterize(double mean, double log_variance);
416 
424  std::vector<double> encoder(const std::vector<double> &input);
425 
433  std::vector<double> decoder(const std::vector<double> &hidden_sampled);
434 
442  void gradient_descent(const std::vector<double> &input,
443  const std::vector<double> &output,
444  const std::vector<double> &hidden_sampled);
445 
455  int hidden_size,
456  int output_size,
457  double learning_rate);
458 
468  virtual void train(const std::vector<std::vector<double>> &training_data,
469  int epochs) override;
470 };
471 
476  public:
480  std::vector<std::vector<double>> weights_recurrent;
481 
491  int hidden_size,
492  int output_size,
493  double learning_rate);
494 
505  virtual void train(const std::vector<std::vector<double>> &training_data,
506  int epochs) override;
507 
515  std::vector<double> recurr_fwd(const std::vector<double> &input,
516  const std::vector<double> &previous_hidden);
517 };
518 
522 class FullAutoEncoder : public AutoEncoder {
523  public:
533  int hidden_size,
534  int output_size,
535  double learning_rate);
536 
546  virtual void train(const std::vector<std::vector<double>> &training_data,
547  int epochs) override;
548 };
549 
550 } // namespace ml
551 
552 } // namespace gpmp
553 
554 #endif
A simple implementation of a vanilla autoencoder.
Definition: encoder.hpp:59
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
virtual void lrate_update(int epoch)
Update the learning rate based on a schedule.
Definition: encoder.cpp:81
void lrate_set(double initial_rate)
Set the initial learning rate.
Definition: encoder.cpp:77
AutoEncoder(int input_size, int hidden_size, int output_size, double learning_rate)
Constructor for the AutoEncoder class.
Definition: encoder.cpp:92
virtual void train(const std::vector< std::vector< double >> &training_data, int epochs)
Train the autoencoder on a dataset.
Definition: encoder.cpp:152
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
virtual void load(const std::string &filename)
Load model weights from a file.
Definition: encoder.cpp:59
void display()
Print the weights of the autoencoder.
Definition: encoder.cpp:203
virtual void save(const std::string &filename) const
Save the model weights to a file.
Definition: encoder.cpp:41
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
ConcreteAutoEncoder class, a derived class from AutoEncoder.
Definition: encoder.hpp:351
virtual void train(const std::vector< std::vector< double >> &training_data, int epochs) override
Trains the Concrete autoencoder on the given training data.
Definition: encoder.cpp:427
double temperature
Temperature parameter for the Concrete distribution.
Definition: encoder.hpp:356
ConcreteAutoEncoder(int input_size, int hidden_size, int output_size, double learning_rate, double temperature)
Constructor for the ConcreteAutoEncoder class.
Definition: encoder.cpp:419
ContractiveAutoEncoder class, a derived class from AutoEncoder.
Definition: encoder.hpp:276
double contractive_weight
Weight for the contractive penalty term.
Definition: encoder.hpp:281
ContractiveAutoEncoder(int input_size, int hidden_size, int output_size, double learning_rate, double contractive_weight)
Constructor for the ContractiveAutoEncoder class.
Definition: encoder.cpp:335
virtual void train(const std::vector< std::vector< double >> &training_data, int epochs) override
Trains the contractive autoencoder on the given training data.
Definition: encoder.cpp:344
DenoisingAutoEncoder class, a derived class from AutoEncoder.
Definition: encoder.hpp:237
DenoisingAutoEncoder(int input_size, int hidden_size, int output_size, double learning_rate, double corruption_level)
Constructor for the DenoisingAutoEncoder class.
Definition: encoder.cpp:283
void train(const std::vector< std::vector< double >> &training_data, int epochs) override
Adds noise to the input data and trains the denoising autoencoder.
Definition: encoder.cpp:292
double corruption_level
Probability of setting an input value to zero during training (dropout rate)
Definition: encoder.hpp:243
FullAutoEncoder class, a derived class from AutoEncoder.
Definition: encoder.hpp:522
virtual void train(const std::vector< std::vector< double >> &training_data, int epochs) override
Trains the fully connected autoencoder on the given training data.
Definition: encoder.cpp:663
FullAutoEncoder(int input_size, int hidden_size, int output_size, double learning_rate)
Constructor for the FullAutoEncoder class.
Definition: encoder.cpp:656
MDLAutoEncoder Minimal Description Length class, a derived class from AutoEncoder.
Definition: encoder.hpp:314
double mdl_weight
Weight for the MDL penalty term.
Definition: encoder.hpp:319
MDLAutoEncoder(int input_size, int hidden_size, int output_size, double learning_rate, double mdl_weight)
Constructor for the MDLAutoEncoder class.
Definition: encoder.cpp:378
virtual void train(const std::vector< std::vector< double >> &training_data, int epochs) override
Trains the MDL autoencoder on the given training data.
Definition: encoder.cpp:386
RecurrentAutoEncoder class, a derived class from AutoEncoder.
Definition: encoder.hpp:475
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
RecurrentAutoEncoder(int input_size, int hidden_size, int output_size, double learning_rate)
Constructor for the RecurrentAutoEncoder class.
Definition: encoder.cpp:579
virtual void train(const std::vector< std::vector< double >> &training_data, int epochs) override
Trains the recurrent autoencoder on the given sequential data.
Definition: encoder.cpp:587
std::vector< std::vector< double > > weights_recurrent
Weight matrix for the recurrent connections.
Definition: encoder.hpp:480
SparseAutoEncoder class, a derived class from AutoEncoder.
Definition: encoder.hpp:193
double sparsity_target
Target sparsity level.
Definition: encoder.hpp:203
double sparsity_weight
Weight for the sparsity penalty term.
Definition: encoder.hpp:198
void train(const std::vector< std::vector< double >> &training_data, int epochs) override
Trains the sparse autoencoder on the given training data.
Definition: encoder.cpp:231
SparseAutoEncoder(int input_size, int hidden_size, int output_size, double learning_rate, double sparsity_weight, double sparsity_target)
Constructor for the SparseAutoEncoder class.
Definition: encoder.cpp:221
VariationalAutoEncoder class, a derived class from AutoEncoder.
Definition: encoder.hpp:390
std::vector< double > encoder(const std::vector< double > &input)
Performs the forward pass for the encoder and returns hidden activations.
Definition: encoder.cpp:490
void gradient_descent(const std::vector< double > &input, const std::vector< double > &output, const std::vector< double > &hidden_sampled)
Performs the backward pass (gradient descent) updating weights.
Definition: encoder.cpp:519
std::vector< double > hidden_log_variance
keeps track of hidden log variance
Definition: encoder.hpp:399
double sample_dist()
Samples from a standard normal distribution.
Definition: encoder.cpp:477
double reparameterize(double mean, double log_variance)
Reparameterization trick for variational autoencoders.
Definition: encoder.cpp:483
std::vector< double > hidden_mean
keeps track of hidden means
Definition: encoder.hpp:395
VariationalAutoEncoder(int input_size, int hidden_size, int output_size, double learning_rate)
Constructor for the VariationalAutoEncoder class.
Definition: encoder.cpp:470
virtual void train(const std::vector< std::vector< double >> &training_data, int epochs) override
Trains the Variational autoencoder on the given training data.
Definition: encoder.cpp:552
std::vector< double > decoder(const std::vector< double > &hidden_sampled)
Performs the forward pass for the decoder and returns output activations.
Definition: encoder.cpp:504
The source C++ openGPMP namespace.