2023-01-24 18:57:18 +01:00
|
|
|
#ifndef MLPP_ANN_H
|
|
|
|
#define MLPP_ANN_H
|
|
|
|
|
2023-01-23 21:13:26 +01:00
|
|
|
//
|
|
|
|
// ANN.hpp
|
|
|
|
//
|
|
|
|
// Created by Marc Melikyan on 11/4/20.
|
|
|
|
//
|
|
|
|
|
2023-01-27 13:01:16 +01:00
|
|
|
#include "core/math/math_defs.h"
|
|
|
|
|
2023-01-24 19:14:38 +01:00
|
|
|
#include "../hidden_layer/hidden_layer.h"
|
|
|
|
#include "../output_layer/output_layer.h"
|
2023-01-23 21:13:26 +01:00
|
|
|
|
2023-02-07 17:36:03 +01:00
|
|
|
#include "../hidden_layer/hidden_layer_old.h"
|
|
|
|
#include "../output_layer/output_layer_old.h"
|
|
|
|
|
2023-01-23 21:13:26 +01:00
|
|
|
#include <string>
|
2023-01-24 19:00:54 +01:00
|
|
|
#include <tuple>
|
|
|
|
#include <vector>
|
2023-01-23 21:13:26 +01:00
|
|
|
|
2023-01-24 19:29:29 +01:00
|
|
|
class MLPPANN {
|
2023-01-24 19:00:54 +01:00
|
|
|
public:
|
2023-02-12 15:07:26 +01:00
|
|
|
std::vector<real_t> model_set_test(std::vector<std::vector<real_t>> X);
|
|
|
|
real_t model_test(std::vector<real_t> x);
|
|
|
|
|
|
|
|
void gradient_descent(real_t learning_rate, int max_epoch, bool ui = false);
|
|
|
|
void sgd(real_t learning_rate, int max_epoch, bool ui = false);
|
|
|
|
void mbgd(real_t learning_rate, int max_epoch, int mini_batch_size, bool ui = false);
|
|
|
|
void momentum(real_t learning_rate, int max_epoch, int mini_batch_size, real_t gamma, bool nag, bool ui = false);
|
|
|
|
void adagrad(real_t learning_rate, int max_epoch, int mini_batch_size, real_t e, bool ui = false);
|
|
|
|
void adadelta(real_t learning_rate, int max_epoch, int mini_batch_size, real_t b1, real_t e, bool ui = false);
|
|
|
|
void adam(real_t learning_rate, int max_epoch, int mini_batch_size, real_t b1, real_t b2, real_t e, bool ui = false);
|
|
|
|
void adamax(real_t learning_rate, int max_epoch, int mini_batch_size, real_t b1, real_t b2, real_t e, bool ui = false);
|
|
|
|
void nadam(real_t learning_rate, int max_epoch, int mini_batch_size, real_t b1, real_t b2, real_t e, bool ui = false);
|
|
|
|
void amsgrad(real_t learning_rate, int max_epoch, int mini_batch_size, real_t b1, real_t b2, real_t e, bool ui = false);
|
|
|
|
|
2023-01-27 13:01:16 +01:00
|
|
|
real_t score();
|
2023-02-12 15:07:26 +01:00
|
|
|
void save(std::string file_name);
|
|
|
|
|
|
|
|
void set_learning_rate_scheduler(std::string type, real_t decay_constant);
|
|
|
|
void set_learning_rate_scheduler_drop(std::string type, real_t decay_constant, real_t drop_rate);
|
2023-01-24 19:00:54 +01:00
|
|
|
|
2023-02-12 15:07:26 +01:00
|
|
|
void add_layer(int n_hidden, std::string activation, std::string weight_init = "Default", std::string reg = "None", real_t lambda = 0.5, real_t alpha = 0.5);
|
|
|
|
void add_output_layer(std::string activation, std::string loss, std::string weight_init = "Default", std::string reg = "None", real_t lambda = 0.5, real_t alpha = 0.5);
|
2023-01-24 19:00:54 +01:00
|
|
|
|
2023-02-12 15:07:26 +01:00
|
|
|
MLPPANN(std::vector<std::vector<real_t>> inputSet, std::vector<real_t> outputSet);
|
|
|
|
|
|
|
|
MLPPANN();
|
|
|
|
~MLPPANN();
|
2023-01-24 19:00:54 +01:00
|
|
|
|
|
|
|
private:
|
2023-02-12 15:07:26 +01:00
|
|
|
real_t apply_learning_rate_scheduler(real_t learningRate, real_t decayConstant, real_t epoch, real_t dropRate);
|
2023-01-24 19:00:54 +01:00
|
|
|
|
2023-02-12 15:07:26 +01:00
|
|
|
real_t cost(std::vector<real_t> y_hat, std::vector<real_t> y);
|
2023-01-24 19:00:54 +01:00
|
|
|
|
2023-02-12 15:07:26 +01:00
|
|
|
void forward_pass();
|
|
|
|
void update_parameters(std::vector<std::vector<std::vector<real_t>>> hiddenLayerUpdations, std::vector<real_t> outputLayerUpdation, real_t learning_rate);
|
|
|
|
std::tuple<std::vector<std::vector<std::vector<real_t>>>, std::vector<real_t>> compute_gradients(std::vector<real_t> y_hat, std::vector<real_t> outputSet);
|
2023-01-24 19:00:54 +01:00
|
|
|
|
2023-02-12 15:07:26 +01:00
|
|
|
void print_ui(int epoch, real_t cost_prev, std::vector<real_t> y_hat, std::vector<real_t> outputSet);
|
2023-01-24 19:00:54 +01:00
|
|
|
|
2023-01-27 13:01:16 +01:00
|
|
|
std::vector<std::vector<real_t>> inputSet;
|
|
|
|
std::vector<real_t> outputSet;
|
|
|
|
std::vector<real_t> y_hat;
|
2023-01-24 19:00:54 +01:00
|
|
|
|
2023-01-30 16:56:16 +01:00
|
|
|
std::vector<MLPPOldHiddenLayer> network;
|
|
|
|
MLPPOldOutputLayer *outputLayer;
|
2023-01-24 19:00:54 +01:00
|
|
|
|
|
|
|
int n;
|
|
|
|
int k;
|
|
|
|
|
|
|
|
std::string lrScheduler;
|
2023-01-27 13:01:16 +01:00
|
|
|
real_t decayConstant;
|
|
|
|
real_t dropRate;
|
2023-01-24 19:00:54 +01:00
|
|
|
};
|
2023-01-23 21:13:26 +01:00
|
|
|
|
|
|
|
#endif /* ANN_hpp */
|