-
Notifications
You must be signed in to change notification settings - Fork 0
/
Neuron.cpp
82 lines (67 loc) · 1.91 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include "NeuralNetwork.h"
/*****************************************************/
/********************** Neuron ***********************/
/*****************************************************/
nn::Neuron::Neuron(int nin) {
this->setNin(nin);
}
nn::Neuron::Neuron(nn::Neuron &neuron) {
this->_nin = neuron._nin;
for (int i = 0; i < neuron._weigh.size(); i++) {
this->_weigh.push_back(neuron._weigh[i]);
}
}
nn::Neuron::~Neuron() {
}
void nn::Neuron::operator = (Neuron &neuron) {
this->_nin = neuron._nin;
for (int i = 0; i < neuron._weigh.size(); i++) {
this->_weigh.push_back(neuron._weigh[i]);
}
}
float nn::Neuron::operator [] (int i) {
return this->_weigh[i];
}
void nn::Neuron::setParent(nn::NeuronLayer *p) {
this->_parent = p;
}
void nn::Neuron::setNin(int nin) {
this->_weigh.resize(nin);
this->_nin = nin;
}
void nn::Neuron::initWeigh(void) {
for (int i = 0; i < this->_weigh.size(); i++)
this->_weigh[i] = (float)rand() / RAND_MAX;
}
float nn::Neuron::get(int i) {
return this->_weigh[i];
}
void nn::Neuron::setInput(vector<float> &v) {
this->_preWeightSum = this->_curWeightSum;
this->_curWeightSum = 0;
if (this->_weigh.size() == v.size()) {
for (int i = 0; i < v.size(); i++) {
this->_curWeightSum += this->_weigh[i] * v[i];
}
this->_preOut = this->_curOut;
this->_curOut = this->_curWeightSum;
} else {
cout << "ERROR: this->_weigh.size != v.size" << endl;
}
}
float nn::Neuron::getWeightSum(void) {
return this->_curWeightSum;
}
float nn::Neuron::getOut() {
return this->_curOut;
}
int nn::Neuron::size(void) {
return this->_weigh.size();
}
void nn::Neuron::print(void) {
cout << "Total: " << this->_nin << endl;
cout << "[";
for (int i = 0; i < this->_weigh.size(); i++)
cout << this->_weigh[i] << " ";
cout << "]" << endl << endl;
}