-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtest_xor.cpp
60 lines (47 loc) · 1.28 KB
/
test_xor.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
/*
* test :
* a simple implementation of neural network on xor example
*
*/
#include <iostream>
#include "neural_network.hpp"
#include <vector>
#include <cstdio>
int main()
{
// creating neural network
// 2 input neurons, 3 hidden neurons and 1 output neuron
std::vector<uint32_t> topology = {2,3,1};
sp::SimpleNeuralNetwork nn(topology, 0.1);
//sample dataset
std::vector<std::vector<float>> targetInputs = {
{0.0f, 0.0f},
{1.0f, 1.0f},
{1.0f, 0.0f},
{0.0f, 1.0f}
};
std::vector<std::vector<float>> targetOutputs = {
{0.0f},
{0.0f},
{1.0f},
{1.0f}
};
uint32_t epoch = 100000;
//training the neural network with randomized data
std::cout << "training start\n";
for(uint32_t i = 0; i < epoch; i++)
{
uint32_t index = rand() % 4;
nn.feedForword(targetInputs[index]);
nn.backPropagate(targetOutputs[index]);
}
std::cout << "training complete\n";
//testing the neural network
for( std::vector<float> input : targetInputs)
{
nn.feedForword(input);
std::vector<float> preds = nn.getPredictions();
std::cout << input[0] << "," << input[1] <<" => " << preds[0] << std::endl;
}
return 0;
}