-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.c
More file actions
158 lines (121 loc) · 4.58 KB
/
main.c
File metadata and controls
158 lines (121 loc) · 4.58 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <math.h>
#include "NeuralNetwork.h"
/*
This example demonstrates a 3 layer 5 node 2 input feed forward multilayer neural network using the backpropagation learning rule
that models the XOR logic function. The first layer of any neural network is always an input layer whose units only have one input
and do not acutally compute anything but rather transfer the value of their input to their output to act as either data compression
or data expansion, in this case the input nodes expand the data.
*/
/*
XOR Truth Table
-------------------
In In Out
1 1 0
0 1 1
1 0 1
0 0 0
*/
/*
Neural Network Model Diagram
--------------------------------
Out
|
O <- Output layer
/ \
O O <- Hidden layer
/\ /\
\/ \/
O O <- Input layer
| |
In In
*/
#define pattern_size 7
bool gpu = false;
void reccurent() {
int numOfPerceptrons[3] = {1,1,1};
int pattern[pattern_size] = {0,1,1,0,1,1,0};
float** sequence = malloc(sizeof(float)*(pattern_size-1));
float** targets = malloc(sizeof(float)*(pattern_size-1));
for(int i=0;i<pattern_size-1;i++) {
sequence[i] = malloc(sizeof(float));
targets[i] = malloc(sizeof(float));
sequence[i][0] = pattern[i];
targets[i][0] = pattern[i+1];
}
NeuralNetwork brain = CreateNeuralNetwork(3, numOfPerceptrons, 0.15, 0.1, kNetworkTypeElman, kNetworkFunctionLogistic, kNetworkLearningModeBackpropagation, gpu);
TrainNeuralNetwork(&brain, sequence, targets, pattern_size-1, 1000000, 0.005, false);
bool flag = true;
for(int i=0;i<pattern_size-1;i++) {
brain.inputs[0] = sequence[i][0];
UpdateNeuralNetwork(&brain);
printf("Input:%i Actual Output:%f Target:%i Output:%i\n", pattern[i], brain.outputs[0], pattern[i+1], (brain.outputs[0] > 0.5));
if((brain.outputs[0] > 0.5) != pattern[i+1]) {
flag = false;
}
}
if(flag) {
printf("\nMATCH\n");
} else {
printf("\nINCORRECT\n");
}
printf("\nMSE:%f Training Time:%f Execution Time:%f\n\n", brain.error, brain.trainingTime, brain.executionTime);
ReleaseNeuralNetwork(&brain);
}
int main (int argc, const char * argv[]) {
/*reccurent();
gpu = true;
reccurent();
exit(0);*/
int nodes[3] = {2,2,1};
// Create the network
NeuralNetwork brain = CreateNeuralNetwork(3, nodes, 0.5, 0.8, kNetworkTypeStandard, kNetworkFunctionLogistic, kNetworkLearningModeBackpropagation, gpu); // The learning rate is trial and error same with momentum high values can be used
// with this simple net because it does not have to generalize at all
float** samples = malloc(sizeof(float*) * 4);
float** targets = malloc(sizeof(float*) * 4);
for(int i=0;i<4;i++) {
samples[i] = malloc(sizeof(float) * 2);
targets[i] = malloc(sizeof(float));
}
samples[0][0] = -1.0;
samples[0][1] = -1.0;
targets[0][0] = 0.0;
samples[1][0] = 1.0;
samples[1][1] = -1.0;
targets[1][0] = 1.0;
samples[2][0] = -1.0;
samples[2][1] = 1.0;
targets[2][0] = 1.0;
samples[3][0] = 1.0;
samples[3][1] = 1.0;
targets[3][0] = 0.0;
TrainNeuralNetwork(&brain, samples, targets, 4, 10000, /*0.0005*/-1, false);
// Output the results to determine if the network was adequately trained
// the outputs will never actually converge to 0 or 1 completely
brain.inputs[0] = -1.0;
brain.inputs[1] = -1.0;
UpdateNeuralNetwork(&brain);
printf("Output:%f\n", brain.outputs[0]);
brain.inputs[0] = 1;
brain.inputs[1] = -1.0;
UpdateNeuralNetwork(&brain);
printf("Output:%f\n", brain.outputs[0]);
brain.inputs[0] = -1.0;
brain.inputs[1] = 1;
UpdateNeuralNetwork(&brain);
printf("Output:%f\n", brain.outputs[0]);
brain.inputs[0] = 1;
brain.inputs[1] = 1;
UpdateNeuralNetwork(&brain);
printf("Output:%f\n\n", brain.outputs[0]);
// When finished free the allocated memory that the network used
ReleaseNeuralNetwork(&brain);
if(gpu != true) {
gpu = true;
//main(argc, argv);
}
return 0;
}