2023-01-24 18:57:18 +01:00
|
|
|
|
|
|
|
#ifndef MLPP_SVC_H
|
|
|
|
#define MLPP_SVC_H
|
|
|
|
|
2023-01-23 21:13:26 +01:00
|
|
|
//
|
|
|
|
// SVC.hpp
|
|
|
|
//
|
|
|
|
// Created by Marc Melikyan on 10/2/20.
|
|
|
|
//
|
|
|
|
|
|
|
|
// https://towardsdatascience.com/svm-implementation-from-scratch-python-2db2fc52e5c2
|
|
|
|
// Illustratd a practical definition of the Hinge Loss function and its gradient when optimizing with SGD.
|
|
|
|
|
2023-01-27 13:01:16 +01:00
|
|
|
#include "core/math/math_defs.h"
|
|
|
|
|
2023-01-23 21:13:26 +01:00
|
|
|
#include <string>
|
2023-01-24 19:00:54 +01:00
|
|
|
#include <vector>
|
2023-01-23 21:13:26 +01:00
|
|
|
|
2023-01-24 19:20:18 +01:00
|
|
|
|
2023-01-23 21:13:26 +01:00
|
|
|
|
2023-01-25 01:09:37 +01:00
|
|
|
class MLPPSVC {
|
2023-01-24 19:00:54 +01:00
|
|
|
public:
|
2023-01-27 13:01:16 +01:00
|
|
|
MLPPSVC(std::vector<std::vector<real_t>> inputSet, std::vector<real_t> outputSet, real_t C);
|
|
|
|
std::vector<real_t> modelSetTest(std::vector<std::vector<real_t>> X);
|
|
|
|
real_t modelTest(std::vector<real_t> x);
|
|
|
|
void gradientDescent(real_t learning_rate, int max_epoch, bool UI = 1);
|
|
|
|
void SGD(real_t learning_rate, int max_epoch, bool UI = 1);
|
|
|
|
void MBGD(real_t learning_rate, int max_epoch, int mini_batch_size, bool UI = 1);
|
|
|
|
real_t score();
|
2023-01-24 19:00:54 +01:00
|
|
|
void save(std::string fileName);
|
|
|
|
|
|
|
|
private:
|
2023-01-27 13:01:16 +01:00
|
|
|
real_t Cost(std::vector<real_t> y_hat, std::vector<real_t> y, std::vector<real_t> weights, real_t C);
|
2023-01-24 19:00:54 +01:00
|
|
|
|
2023-01-27 13:01:16 +01:00
|
|
|
std::vector<real_t> Evaluate(std::vector<std::vector<real_t>> X);
|
|
|
|
std::vector<real_t> propagate(std::vector<std::vector<real_t>> X);
|
|
|
|
real_t Evaluate(std::vector<real_t> x);
|
|
|
|
real_t propagate(std::vector<real_t> x);
|
2023-01-24 19:00:54 +01:00
|
|
|
void forwardPass();
|
|
|
|
|
2023-01-27 13:01:16 +01:00
|
|
|
std::vector<std::vector<real_t>> inputSet;
|
|
|
|
std::vector<real_t> outputSet;
|
|
|
|
std::vector<real_t> z;
|
|
|
|
std::vector<real_t> y_hat;
|
|
|
|
std::vector<real_t> weights;
|
|
|
|
real_t bias;
|
2023-01-24 19:00:54 +01:00
|
|
|
|
2023-01-27 13:01:16 +01:00
|
|
|
real_t C;
|
2023-01-24 19:00:54 +01:00
|
|
|
int n;
|
|
|
|
int k;
|
|
|
|
|
|
|
|
// UI Portion
|
2023-01-27 13:01:16 +01:00
|
|
|
void UI(int epoch, real_t cost_prev);
|
2023-01-24 19:00:54 +01:00
|
|
|
};
|
2023-01-24 19:20:18 +01:00
|
|
|
|
2023-01-23 21:13:26 +01:00
|
|
|
|
|
|
|
#endif /* SVC_hpp */
|