philsupertramp/game-math
Loading...
Searching...
No Matches
AdalineSGD.h
Go to the documentation of this file.
1#pragma once
2
3#include "../Matrix.h"
4#include "Classifier.h"
5#include "SGD.h"
6
21{
22public:
24 bool shuffle;
29
30public:
38 explicit AdalineSGD(double _eta = 0.01, int iter = 10, bool _shuffle = false, int _randomState = 0)
39 : ANNClassifier(_eta, iter)
40 , shuffle(_shuffle)
41 , randomState(_randomState) {
43 sgd = SGD(_eta, iter, _shuffle);
44 }
45
52 void fit(const Matrix<double>& X, const Matrix<double>& y) override {
54 sgd.fit(X, y, weights);
55 }
56
62 void partial_fit(const Matrix<double>& X, const Matrix<double>& y) {
64 sgd.partial_fit(X, y, weights);
65 }
66
72 Matrix<double> netInput(const Matrix<double>& X) override { return sgd.netInput(X, weights); }
73
79 double costFunction([[maybe_unused]] const Matrix<double>& X) override { return 0; }
80
86 Matrix<double> activation(const Matrix<double>& X) override { return netInput(X); }
87
94 std::function<bool(double)> condition = [](double x) { return bool(x >= 0.0); };
95 return where(condition, activation(X), { { 1 } }, { { -1 } });
96 }
97};
98
99
Definition: Classifier.h:70
Definition: AdalineSGD.h:21
void partial_fit(const Matrix< double > &X, const Matrix< double > &y)
Definition: AdalineSGD.h:62
bool shuffle
signalizes whether given dataset should be shuffled while fitting
Definition: AdalineSGD.h:24
Matrix< double > predict(const Matrix< double > &X) override
Definition: AdalineSGD.h:93
AdalineSGD(double _eta=0.01, int iter=10, bool _shuffle=false, int _randomState=0)
Definition: AdalineSGD.h:38
Matrix< double > netInput(const Matrix< double > &X) override
Definition: AdalineSGD.h:72
int randomState
initialize weights with random state
Definition: AdalineSGD.h:26
double costFunction(const Matrix< double > &X) override
Definition: AdalineSGD.h:79
Matrix< double > activation(const Matrix< double > &X) override
Definition: AdalineSGD.h:86
void fit(const Matrix< double > &X, const Matrix< double > &y) override
Definition: AdalineSGD.h:52
SGD sgd
algorithmic object to represent fitting algorithm
Definition: AdalineSGD.h:28
bool w_initialized
flag to initialize weights only once
Definition: Classifier.h:25
Matrix< double > weights
Vector holding weights.
Definition: Classifier.h:29
void initialize_weights(size_t numRows, size_t numColumns=1)
Definition: Classifier.h:45
Definition: Matrix.h:42
size_t columns() const
Definition: Matrix.h:198
static void SetSeed(int seed)
Definition: Random.h:30
Definition: SGD.h:12
void partial_fit(const Matrix< double > &X, const Matrix< double > &y, Matrix< double > &weights) const
Definition: SGD.h:86
static Matrix< double > netInput(const Matrix< double > &X, const Matrix< double > &weights)
Definition: SGD.h:132
void fit(const Matrix< double > &X, const Matrix< double > &y, Matrix< double > &weights)
Definition: SGD.h:57
Matrix< T > where(const std::function< bool(T)> &condition, const Matrix< T > &in, const Matrix< T > &valIfTrue, const Matrix< T > &valIfFalse)
Definition: matrix_utils.h:194