-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMlpNetwork.cpp
56 lines (53 loc) · 1.52 KB
/
MlpNetwork.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
#include "MlpNetwork.h"
/*
* Constructor for MlpNetwork.
* @param weights - array of matrices representing the weights of the network.
* @param biases - array of matrices representing the biases of the network.
* @return a new MlpNetwork object.
*/
MlpNetwork::MlpNetwork(Matrix *weights, Matrix *biases)
{
for (int i = 0; i < MLP_SIZE - 1; i++)
{
_layers[i] = new Dense(weights[i], biases[i], activation::relu);
}
_layers[MLP_SIZE - 1] = new Dense(
weights[MLP_SIZE - 1], biases[MLP_SIZE - 1], activation::softmax
);
}
/*
* Destructor for MlpNetwork.
*/
MlpNetwork::~MlpNetwork()
{
for (int i = 0; i < MLP_SIZE; i++)
{
delete _layers[i]; // Free the allocated memory for each Dense object
}
}
/*
* Overloaded operator() for MlpNetwork.
* @param input - a matrix representing the input to the network.
* @return a digit object representing the network's prediction.
*/
digit MlpNetwork::operator() (const Matrix& input) const
{
Matrix returns(input);
// Feed the input through the different layers in the network
for (int i = 0; i < MLP_SIZE; i++)
{
returns = (*_layers[i])(returns);
}
digit res;
res.probability = 0;
// Find the highest probability in the output layer
for (int i = 0; i < returns.get_rows(); i++)
{
if (returns(i, 0) > res.probability)
{
res.probability = returns(i, 0);
res.value = i;
}
}
return res;
}