-
Notifications
You must be signed in to change notification settings - Fork 0
/
neuron.cpp
57 lines (46 loc) · 1.51 KB
/
neuron.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <cmath>
#include "neuron.hpp"
#include <numeric>
#include <iostream>
Neuron::Neuron() : bias(0), activationFunc(std::make_shared<Sigmoid>()) {}
Neuron::Neuron(const std::vector<double>& weights, double bias, std::shared_ptr<ActivationFunction> func) : weights(weights), bias(bias), activationFunc(func) {}
std::vector<double> Neuron::getWeights() const {
return weights;
}
void Neuron::setWeights(const std::vector<double>& weights) {
this->weights = weights;
}
std::vector<double>& Neuron::getWeightsRef() {
return weights;
}
void Neuron::updateWeights(const std::vector<double>& newWeights) {
if (newWeights.size() != weights.size()) {
throw std::invalid_argument("Size of new weights does not match size of current weights");
}
weights = newWeights;
}
double Neuron::getBias() const {
return bias;
}
void Neuron::setBias(double bias) {
this->bias = bias;
}
double Neuron::calculateOutput(const std::vector<double>& inputs) {
if(inputs.size() != weights.size()){
throw std::invalid_argument("Size of inputs does not match size of weights");
}
double sum = std::inner_product(inputs.begin(), inputs.end(), weights.begin(), 0.0);
output = activationFunc->apply(sum + bias);
return output;
}
double Neuron::getOutput() const {
return output;
}
// debug
void Neuron::printWeightsAndBias() const {
std::cout << "Weights: ";
for (auto weight : weights) {
std::cout << weight << " ";
}
std::cout << "Bias: " << bias << std::endl;
}