-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNetwork.cpp
More file actions
90 lines (78 loc) · 1.73 KB
/
Copy pathNetwork.cpp
File metadata and controls
90 lines (78 loc) · 1.73 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
#include "Network.h"
#include <cassert>
#include <ctime>
#include <cstdlib>
#include <chrono>
Network::Network()
{
std::vector<Matrix> weights;
std::vector<Matrix> bias;
std::vector<Matrix> activations;
layer = 0;
}
Network::Network(const Network& other)
{
layer = other.layer;
weights = other.weights;
bias = other.bias;
activations = other.activations;
}
Network::Network(size_t _layer, size_t* sizes)
{
layer = _layer;
size_t* current = sizes;
for (size_t i = 0; i < layer - 1; i++, current++)
{
bias.push_back(Matrix::uniform(Matrix(*(current + 1), 1), 0)); //bias set to 0
weights.push_back(Matrix(*(current + 1), *current, 0, 1)); //weights set between -1 and 1 following a normal distribution
}
}
void Network::FeedForward(const Matrix& input)
{
activations.clear();
activations.push_back(Matrix::sigmoid(Matrix::dot(weights[0], input) + bias[0]));
for (size_t i = 0; i < layer - 2; i++)
{
activations.push_back(Matrix::sigmoid(Matrix::dot(weights[i + 1], activations[i])) + bias[i+1]);
}
}
Matrix Network::GetOutput()
{
return activations[layer - 2];
}
std::vector<Matrix> Network::GetWeights()
{
return weights;
}
Network& Network::operator=(const Network& other)
{
layer = other.layer;
weights = other.weights;
bias = other.bias;
activations = other.activations;
return *this;
}
void Network::DisplayWeights()
{
for (size_t i = 0; i < layer - 1; i++)
{
weights[i].display();
}
}
void Network::DisplayActivations()
{
for (size_t i = 0; i < layer - 1; i++)
{
activations[i].display();
}
}
std::vector<Network> Network::Batch(size_t layer, size_t* sizes, size_t number)
{
std::vector<Network> res;
for (size_t j = 0; j < number; j++)
{
Network nouv(layer, sizes);
res.push_back(nouv);
}
return res;
}