-
Notifications
You must be signed in to change notification settings - Fork 6
/
Network.c
231 lines (205 loc) · 7.2 KB
/
Network.c
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#include "Network.h"
#include "Connection.h"
#include "Neuron.h"
#include "functional.h"
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Build network: calls build functions for children
Network build_network(int const in_size, int const in_enc_size,
int const hid_size, int const out_size) {
// Network struct
Network net;
// Set encoding type
net.type = BOTH;
// Set decoding scale
net.decoding_scale = 1.0f;
// Set sizes
// Output size has to be 1
if (out_size != 1) {
printf("Network output size should be 1!\n");
exit(1);
}
net.in_size = in_size;
net.in_enc_size = in_enc_size;
net.hid_size = hid_size;
net.out_size = out_size;
// Allocate memory for input placeholders, place cell centers and underlying
// neurons and connections
net.in = calloc(in_size, sizeof(*net.in));
net.in_enc = calloc(in_enc_size, sizeof(*net.in_enc));
net.centers = calloc(in_enc_size, sizeof(*net.centers));
// TODO: is this the best way to do this? Or let network struct consist of
// actual structs instead of pointers to structs?
net.inhid = malloc(sizeof(*net.inhid));
net.hid = malloc(sizeof(*net.hid));
net.hidout = malloc(sizeof(*net.hidout));
net.out = malloc(sizeof(*net.out));
// Call build functions for underlying neurons and connections
*net.inhid = build_connection(hid_size, in_enc_size);
*net.hid = build_neuron(hid_size);
*net.hidout = build_connection(out_size, hid_size);
*net.out = build_neuron(out_size);
return net;
}
// Init network: calls init functions for children
void init_network(Network *net) {
// Loop over input placeholders
for (int i = 0; i < net->in_size; i++) {
net->in[i] = 0.0f;
}
for (int i = 0; i < net->in_enc_size; i++) {
net->in_enc[i] = 0.0f;
net->centers[i] = 0.0f;
}
// Call init functions for children
init_connection(net->inhid);
init_neuron(net->hid);
init_connection(net->hidout);
init_neuron(net->out);
}
// Reset network: calls reset functions for children
void reset_network(Network *net) {
reset_connection(net->inhid);
reset_neuron(net->hid);
reset_connection(net->hidout);
reset_neuron(net->out);
}
// Load parameters for network from header file and call load functions for
// children
void load_network_from_header(Network *net, NetworkConf const *conf) {
// Check shapes
if ((net->in_size != conf->in_size) ||
(net->in_enc_size != conf->in_enc_size) ||
(net->hid_size != conf->hid_size) || (net->out_size != conf->out_size)) {
printf(
"Network has a different shape than specified in the NetworkConf!\n");
exit(1);
}
// Encoding
net->type = conf->type;
// Decoding
net->decoding_scale = conf->decoding_scale;
// Place cell centers (just BS if we don't use them)
for (int i = 0; i < net->in_enc_size; i++) {
net->centers[i] = conf->centers[i];
}
// Connection input -> hidden
load_connection_from_header(net->inhid, conf->inhid);
// Hidden neuron
load_neuron_from_header(net->hid, conf->hid);
// Connection hidden -> output
load_connection_from_header(net->hidout, conf->hidout);
// Output neuron
load_neuron_from_header(net->out, conf->out);
}
// Free allocated memory for network and call free functions for children
void free_network(Network *net) {
// Call free functions for children
// Freeing in a bottom-up manner
// TODO: or should we call this before freeing the network struct members?
free_connection(net->inhid);
free_neuron(net->hid);
free_connection(net->hidout);
free_neuron(net->out);
// calloc() was used for input placeholders and underlying neurons and
// connections
free(net->in);
free(net->in_enc);
free(net->centers);
free(net->inhid);
free(net->hid);
free(net->hidout);
free(net->out);
}
// Print network parameters (for debugging purposes)
void print_network(Network const *net) {
// Encoding type
printf("Encoding type: %d\n", net->type);
printf("Place cell centers:\n");
print_array_1d(net->in_enc_size, net->centers);
// Decoding scale
printf("Decoding scale: %.4f\n\n", net->decoding_scale);
// Input layer
printf("Input layer (raw):\n");
print_array_1d(net->in_size, net->in);
printf("Input layer (encoded):\n");
print_array_1d(net->in_enc_size, net->in_enc);
// Connection input -> hidden
printf("Connection weights input -> hidden:\n");
print_array_2d(net->hid_size, net->in_enc_size, net->inhid->w);
// Hidden layer
print_neuron(net->hid);
// Connection hidden -> output
printf("Connection weights hidden -> output:\n");
print_array_2d(net->out_size, net->hid_size, net->hidout->w);
// Output layer
print_neuron(net->out);
}
// Encode both divergence and its derivative as current
// Called in forward_network(), so has to be put in front (because not in
// header)
// TODO: also load parameters for this?
static void encode_both(int const size, int const enc_size, float x[size],
float x_enc[enc_size]) {
// Repeat inputs, clamp first half to positive, second half to negative
// and make absolute
for (int i = 0; i < enc_size; i++) {
if (i < size) {
x_enc[i] = fmaxf(0.0f, x[i % (size)]);
} else {
x_enc[i] = fabs(fminf(0.0f, x[i % (size)]));
}
}
}
// Encode divergence through nonlinearly distributed place cells
// TODO: also load parameters for this?
static void encode_place(int const size, int const enc_size, float x[size],
float x_enc[enc_size], float centers[enc_size]) {
// Place cell activity/current depends on distance between
// current state and center
// First clamp to [-10, 10]
// TODO: maybe make this more flexible?
for (int i = 0; i < size; i++) {
x[i] = fmaxf(-10.0f, fminf(10.0f, x[i]));
}
// Overlap of activity: total range of inputs divided by # of centers - 1
float sigma = 20.0f / (enc_size - 1);
// Do actual encoding
// 1.0: input scaling
for (int i = 0; i < enc_size; i++) {
x_enc[i] =
1.0f * exp(-(x[0] - centers[i]) * (x[0] - centers[i]) / (2.0f * sigma));
}
}
// Decode from trace
// Mind to take into account the trace scaling
// TODO: also load parameters for this?
static float decode_network(int const size, float const t[size],
float const scale) {
// Scale with output range and maximum trace and apply potential offset
float output = -0.8f + (0.5f + 0.8f) * (t[0] / scale + 0.0f);
return output;
}
// Forward network and call forward functions for children
// Encoding and decoding inside
// TODO: but we still need to check the size of the array we put in net->in
float forward_network(Network *net) {
// Encode input from scalar value to currents
if (net->type == BOTH) {
encode_both(net->in_size, net->in_enc_size, net->in, net->in_enc);
} else if (net->type == PLACE) {
encode_place(net->in_size, net->in_enc_size, net->in, net->in_enc,
net->centers);
}
// Call forward functions for children
forward_connection(net->inhid, net->hid->x, net->in_enc);
forward_neuron(net->hid);
forward_connection(net->hidout, net->out->x, net->hid->s);
forward_neuron(net->out);
// Decode output neuron traces to scalar value
float output =
decode_network(net->out_size, net->out->t, net->decoding_scale);
return output;
}