AlgoPlus v0.1.0
Loading...
Searching...
No Matches
perceptron.h
1#pragma once
2
3#ifdef __cplusplus
4#include <cassert>
5#include <iostream>
6#include <vector>
7#include "../activation/activation_functions.h"
8#include "../metrics/metrics.h"
9#include "nn.h"
10#endif
11
16 private:
17 std::vector<std::vector<double>> data_;
18 std::vector<double> labels_;
19 int epochs_;
20 double learning_rate_;
21 nn::Linear weights_;
22
23 public:
31 explicit perceptron(std::vector<std::vector<double>> const&, const int epochs = 100,
32 const double learning_rate = 0.001);
33
37 void fit();
38
44 double predict(std::vector<double> const&);
45};
46
47inline perceptron::perceptron(std::vector<std::vector<double>> const& data, const int epochs,
48 const double learning_rate)
49 : weights_(data[0].size() - 1, 1, true) {
50 assert(data.size() > 0);
51 assert(epochs > 0);
52 assert(learning_rate > 0);
53 this->epochs_ = epochs;
54 this->data_ = data;
55 this->learning_rate_ = learning_rate;
56 for (std::vector<double>& row : this->data_) {
57 this->labels_.push_back(row.back());
58 row.pop_back();
59 }
60}
61
62inline void perceptron::fit() {
63 for (int epoch = 0; epoch < this->epochs_; epoch++) {
64 std::vector<double> y_pred;
65 for (size_t i = 0; i < this->data_.size(); i++) {
66 double y_pred_ = (this->weights_.forward(this->data_[i])[0] > 0) ? 1.0 : -1.0;
67 y_pred.push_back(y_pred_);
68 double err = y_pred_ - this->labels_[i];
69 if (err != 0) {
70 this->weights_.update_weights(this->data_[i], err, this->learning_rate_);
71 }
72 }
73
74 std::cout << "Epoch: " << epoch + 1 << ": "
75 << "Accuracy: " << metrics::accuracy_score(this->labels_, y_pred)
76 << " | f1_score: " << metrics::f1_score(this->labels_, y_pred)
77 << " | Recall: " << metrics::recall(this->labels_, y_pred)
78 << " | Precision: " << metrics::precision(this->labels_, y_pred) << '\n';
79 }
80}
81
82inline double perceptron::predict(std::vector<double> const& input) {
83 assert(input.size() == this->data_[0].size());
84 return (this->weights_.forward(input)[0] > 0) ? 1.0 : -1.0;
85}
Linear module. This implementation mostly follows PyTorch's implementation.
Definition nn.h:18
void fit()
fit a single perceptron on the input data
Definition perceptron.h:62
double predict(std::vector< double > const &)
performs inference, classifying to 1 or -1
Definition perceptron.h:82
perceptron(std::vector< std::vector< double > > const &, const int epochs=100, const double learning_rate=0.001)
default constructor for perceptron class
Definition perceptron.h:47
double f1_score(const std::vector< double > &y, const std::vector< double > &y_pred)
f1 score function: [2 * precision * recall / precision + recall]
Definition metrics.h:99
double accuracy_score(const std::vector< double > &y, const std::vector< double > &y_pred)
accuracy score function[(tp + tn) / (tp + tn + fp + fn)]
Definition metrics.h:81
double recall(const std::vector< double > &y, const std::vector< double > &y_pred)
recall function[tp / tp + fn]
Definition metrics.h:72
double precision(const std::vector< double > &y, const std::vector< double > &y_pred)
precision function[tp / tp + fp]
Definition metrics.h:90