-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathneuron.cpp
More file actions
99 lines (82 loc) · 1.61 KB
/
Copy pathneuron.cpp
File metadata and controls
99 lines (82 loc) · 1.61 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include "neuron.h"
#include <iostream>
#include <math.h>
neuron::neuron(int lId, int nIdL,typeActivation activationFunction)
{
this->activation = activationFunction;
layerID = lId;
neuronIdLayer = nIdL;
}
void neuron::setInputs(std::vector<float> newInputs)
{
if(weights.size() == newInputs.size())
{
inputs = newInputs;
}
else
{
std::cout << "Error newInputs is not at the size than weights"<< std::endl;
}
}
float neuron::getWeightWithIndex(int index)
{
return weights[index];
}
std::vector<float> neuron::getInputs() const
{
return inputs;
}
typeActivation neuron::getActivation() const
{
return activation;
}
float neuron::sigmoid(float x)
{
return 1/(1+exp(-x));
}
float neuron::relu(float x)
{
return x > 0 ? x : 0;
}
void neuron::emptyInputsOutput()
{
inputs.clear();
output = 0;
}
void neuron::learn(float learningRate)
{
for(int i = 0 ; i<(int)weights.size();i++)
{
weights[i] = weights[i] - learningRate * delta;
}
bias -= learningRate * delta;
}
float neuron::preActivation()
{
float sum = 0;
for(int i = 0; i < (int)inputs.size() ; i++)
{
sum += weights[i] * inputs[i];
}
return sum+bias;
}
float neuron::propagate()
{
if(activation == SIGMOID)
{
output = sigmoid(preActivation());
}
else if(activation == RELU)
{
output = relu(preActivation());
}
return output;
}
void neuron::defineWeight(int numberInput)
{
weights.clear();
for(int i = 0 ; i < numberInput; i++)
{
weights.push_back( (float)(( std::rand()%2 )*2-1)/2 );
}
}