-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGenBP.h
150 lines (122 loc) · 3.61 KB
/
GenBP.h
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
//****************************************************************************
// File Name : GenBP.h
// Purpose : Generic BackPropagation Neural Network Class
// Creator : Young ho Kim<[email protected]>
// MFC Forum <http://stmail.chosun.ac.kr/~melchi/>
//****************************************************************************
#include <stdio.h>
#include <fstream.h>
#include <iostream.h>
#include <stdlib.h>
#include <conio.h>
#include <assert.h>
#include <ctype.h>
#include <time.h>
#include "BackProp.h"
#include "Common.h"
//##ModelId=3A1160D6014B
template<class INPUT_NODE, class HIDDEN_NODE, class OUTPUT_NODE, class LINK>
class Generic_BackProp : public BackProp_Network
{
public:
//##ModelId=3A1160D6017F
Generic_BackProp(char *filename);
//##ModelId=3A1160D60192
Generic_BackProp(double lr, double mt, int layers, ...);
//##ModelId=3A1160D601B1
Generic_BackProp(void) : BackProp_Network(){};
//##ModelId=3A1160D601C4
Generic_BackProp(double lr, double mt, int layers, int nodes[]);
//##ModelId=3A1160D601E2
virtual void Create_Network(void);
//##ModelId=3A1160D60201
char *Get_Name(void)
{
static char name[] = "Generic_BackProp_Network";
return name;
};
};
template<class INPUT_NODE, class HIDDEN_NODE, class OUTPUT_NODE, class LINK>
Generic_BackProp<INPUT_NODE, HIDDEN_NODE, OUTPUT_NODE, LINK>::
Generic_BackProp(char filename[]) :
BackProp_Network()
{
ifstream infile(filename);
Load(infile);
infile.close();
};
//##ModelId=3A1160D60192
template<class INPUT_NODE, class HIDDEN_NODE, class OUTPUT_NODE, class LINK>
Generic_BackProp<INPUT_NODE, HIDDEN_NODE, OUTPUT_NODE, LINK>::
Generic_BackProp(double lr, double mt, int layers, ...) :
BackProp_Network()
{
num_nodes = 0;
num_links = 0;
num_layers = layers;
va_list vl;
va_start(vl, layers);
node_cnt = new int[layers];
for(int i=0; i<layers; i++)
{
node_cnt[i] = va_arg(vl, int);
num_nodes += node_cnt[i];
if( i > 0)
num_links += node_cnt[i-1] * node_cnt[i]; // links between layers
}
va_end(vl);
learning_rate = lr;
momentum_term = mt;
Create_Network();
};
//##ModelId=3A1160D601C4
template<class INPUT_NODE, class HIDDEN_NODE, class OUTPUT_NODE, class LINK>
Generic_BackProp<INPUT_NODE, HIDDEN_NODE, OUTPUT_NODE, LINK>::
Generic_BackProp(double lr, double mt, int layers, int nodes[]) :
BackProp_Network()
{
num_nodes = 0;
num_links = 0;
num_layers = layers;
node_cnt = new int[layers];
for(int i = 0; i<layers; i++)
{
node_cnt[i] = nodes[i];
num_nodes += node_cnt[i];
if(i > 0)
num_links += node_cnt[i-1] * node_cnt[i]; // link between layers
}
learning_rate = lr;
momentum_term = mt;
Create_Network();
};
//##ModelId=3A1160D601E2
template<class INPUT_NODE, class HIDDEN_NODE, class OUTPUT_NODE, class LINK>
void Generic_BackProp<INPUT_NODE, HIDDEN_NODE, OUTPUT_NODE, LINK>::
Create_Network(void)
{
node = new Base_Node*[num_nodes];
link = new Base_Link*[num_links];
int curr = 0;
for(int i = 0; i < node_cnt[0]; i++)
node[curr++] = new INPUT_NODE;
first_hidden_node = curr;
for(i=1; i < num_layers-1; i++)
for(int j = 0 ; j < node_cnt[i]; j++)
node[curr++] = new HIDDEN_NODE(learning_rate, momentum_term);
first_output_node = curr;
for(i = 0; i < node_cnt[num_layers-1] ; i++)
node[curr++] = new OUTPUT_NODE(learning_rate, momentum_term);
for(i = 0; i < num_links; i++)
link[i] = new LINK;
curr = 0;
int layer1 = 0, layer2 = first_hidden_node;
for(i= 0; i < num_layers -1 ; i++)
{
for(int j=0; j<node_cnt[i+1]; j++)
for(int k =0; k < node_cnt[i]; k++)
Connect(node[layer1+k], node[layer2+j], link[curr++]);
layer1 = layer2;
layer2 += node_cnt[i+1];
}
};