-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNeuron.cpp
59 lines (45 loc) · 1.27 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
58
59
#include "Neuron.h"
double Neuron::xavRand(int input, int output)
{
double max = sqrt(6.0 / (input + output));
double f = (double)rand() / RAND_MAX;
return f * (2 * max) - max;
}
double Neuron::dotProduct(const std::vector<double>& a, const std::vector<double>& b) {
double sum = 0;
for (int i = 0; i < a.size(); i++) {
sum += a[i] * b[i];
}
return sum;
}
Neuron::Neuron(int numInputs, int numOutputs, ActivationFunction* activationFunction){
for (int i = 0; i < numInputs; ++i) {
weights.push_back(xavRand(numInputs, numOutputs));
}
bias = xavRand(numInputs, numOutputs);
this->activationFunction = activationFunction;
}
double Neuron::feedForward(const std::vector<double>& inputs) {
return activate(dotProduct(inputs, weights));
}
double Neuron::activate(double x) {
return activationFunction->activate(x);
}
std::vector<double> Neuron::getWeights() const {
return weights;
}
double Neuron::getWeightAtIndex(int index) const {
return weights[index];
}
double Neuron::getBias() const {
return bias;
}
void Neuron::setWeights(const std::vector<double>& newWeights) {
weights = newWeights;
}
void Neuron::setWeightAtIndex(int index, double newWeight) {
weights[index] = newWeight;
}
void Neuron::setBias(double newBias) {
bias = newBias;
}