7#include "../activation/activation_functions.h"
8#include "../metrics/metrics.h"
17 std::vector<std::vector<double>> data_;
18 std::vector<double> labels_;
20 double learning_rate_;
31 explicit perceptron(std::vector<std::vector<double>>
const&,
const int epochs = 100,
32 const double learning_rate = 0.001);
44 double predict(std::vector<double>
const&);
48 const double learning_rate)
49 : weights_(data[0].size() - 1, 1, true) {
50 assert(data.size() > 0);
52 assert(learning_rate > 0);
53 this->epochs_ = epochs;
55 this->learning_rate_ = learning_rate;
56 for (std::vector<double>& row : this->data_) {
57 this->labels_.push_back(row.back());
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];
70 this->weights_.update_weights(this->data_[i], err, this->learning_rate_);
74 std::cout <<
"Epoch: " << epoch + 1 <<
": "
83 assert(input.size() == this->data_[0].size());
84 return (this->weights_.forward(input)[0] > 0) ? 1.0 : -1.0;
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