-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathexample.cpp
85 lines (68 loc) · 2.24 KB
/
example.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
83
84
/**********************************************************************************************************
DEMO CODE: XOR Backpropagation Example
File: example.cpp
Version: 0.1
Copyright(C) NeuroAI (http://www.learnartificialneuralnetworks.com)
Documentation: http://www.learnartificialneuralnetworks.com/neural-network-software/backpropagation-source-code/
NeuroAI Licence:
Permision granted to use this source code only for non commercial and educational purposes.
You can distribute this file but you can't take ownership of it or modify it.
You can include this file as a link on any website but you must link directly to NeuroAI website
(http://www.learnartificialneuralnetworks.com)
Written by Daniel Rios <[email protected]> , June 2013
/*********************************************************************************************************/
#include <iostream>
#include "bpnet.h"
using namespace std;
#define PATTERN_COUNT 4
#define PATTERN_SIZE 2
#define NETWORK_INPUTNEURONS 3
#define NETWORK_OUTPUT 1
#define HIDDEN_LAYERS 0
#define EPOCHS 20000
int main()
{
//Create some patterns
//playing with xor
//XOR input values
float pattern[PATTERN_COUNT][PATTERN_SIZE]=
{
{0,0},
{0,1},
{1,0},
{1,1}
};
//XOR desired output values
float desiredout[PATTERN_COUNT][NETWORK_OUTPUT]=
{
{0},
{1},
{1},
{0}
};
bpnet net;//Our neural network object
int i,j;
float error;
//We create the network
net.create(PATTERN_SIZE,NETWORK_INPUTNEURONS,NETWORK_OUTPUT,HIDDEN_LAYERS,HIDDEN_LAYERS);
//Start the neural network training
for(i=0;i<EPOCHS;i++)
{
error=0;
for(j=0;j<PATTERN_COUNT;j++)
{
error+=net.train(desiredout[j],pattern[j],0.2f,0.1f);
}
error/=PATTERN_COUNT;
//display error
cout << "ERROR:" << error << "\r";
}
//once trained test all patterns
for(i=0;i<PATTERN_COUNT;i++)
{
net.propagate(pattern[i]);
//display result
cout << "TESTED PATTERN " << i << " DESIRED OUTPUT: " << *desiredout[i] << " NET RESULT: "<< net.getOutput().neurons[0]->output << endl;
}
return 0;
}