-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPerceptron.cpp
More file actions
69 lines (56 loc) · 2.31 KB
/
Copy pathPerceptron.cpp
File metadata and controls
69 lines (56 loc) · 2.31 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
#include <Eigen\Dense>
#include <vector>
#include <cmath>
#include "Perceptron.hpp"
Perceptron::Perceptron() {
}
Perceptron::Perceptron(std::vector<unsigned int> const & architecture) {
m_architecture = architecture;
for (unsigned int layer_index = 1; layer_index < architecture.size(); layer_index++) {
Mat layer_weights;
layer_weights = Mat::Random(architecture[layer_index], architecture[layer_index-1]);
layer_weights *= 4. * sqrt(6. / (architecture[layer_index-1] + architecture[layer_index]));
m_weights.push_back(layer_weights);
}
for (unsigned int layer_index = 0; layer_index < m_weights.size(); layer_index++) {
for (unsigned int i = 0; i < m_weights[layer_index].cols(); i++) {
for (unsigned int j = 0; j < m_weights[layer_index].rows(); j++) {
m_genotype.push_back(m_weights[layer_index](j, i));
}
}
}
}
Perceptron::Perceptron(std::vector<unsigned int> const & architecture, std::vector<double> const & genotype) : Perceptron(architecture) {
m_genotype = genotype;
int genotype_index = 0;
for (unsigned int layer_index = 1; layer_index < m_weights.size(); layer_index++) {
for (unsigned int i = 0; i < m_weights[layer_index].cols(); i++) {
for (unsigned int j = 0; j < m_weights[layer_index].rows(); j++) {
m_weights[layer_index](j, i) = genotype[genotype_index];
genotype_index += 1;
}
}
}
}
Vec Perceptron::activation(Vec const & inputs) const {
Vec signal = inputs;
for (unsigned int layer_index = 0; layer_index < m_weights.size(); layer_index++) {
signal = m_weights[layer_index] * signal;
for (unsigned int j = 0; j < signal.size(); j++) {
signal(j, 0) = 2. * atan(signal(j, 0)) / M_PI;
}
}
return signal;
}
std::vector<Perceptron> cross_over(Perceptron const & perceptron_1, Perceptron const & perceptron_2, std::string method) {
std::vector<std::vector<double>> new_genotypes = cross_over(perceptron_1.m_genotype, perceptron_2.m_genotype, method);
Perceptron new_perceptron_1(perceptron_1.m_architecture, new_genotypes[0]);
Perceptron new_perceptron_2(perceptron_2.m_architecture, new_genotypes[1]);
std::vector<Perceptron> new_perceptrons(2);
new_perceptrons[0] = new_perceptron_1;
new_perceptrons[1] = new_perceptron_2;
return new_perceptrons;
}
void Perceptron::mutation(){
// See theory
}