-
Notifications
You must be signed in to change notification settings - Fork 6
/
Connection.c
69 lines (59 loc) · 1.77 KB
/
Connection.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
#include "Connection.h"
#include <stdio.h>
#include <stdlib.h>
// Build connection
Connection build_connection(int const post, int const pre) {
// Connection struct
Connection c;
// Set shape
c.post = post;
c.pre = pre;
// Allocate memory for weight array
c.w = calloc(post * pre, sizeof(*c.w));
return c;
}
// Init connection
void init_connection(Connection *c) {
// Loop over weights
for (int i = 0; i < c->post; i++) {
for (int j = 0; j < c->pre; j++) {
c->w[i * c->pre + j] = rand() / (float)RAND_MAX;
}
}
}
// Reset connection
// Doesn't actually do anything, just for consistency
void reset_connection(Connection *c) {}
// Load parameters for connection (weights) from a header file
// (using the ConnectionConf struct)
void load_connection_from_header(Connection *c, ConnectionConf const *conf) {
// Check if same shape
if ((c->pre != conf->pre) || (c->post != conf->post)) {
printf("Connection has a different shape than specified in the "
"ConnectionConf!\n");
exit(1);
}
// Loop over weights
// TODO: could also be done by just exchanging pointers to arrays?
for (int i = 0; i < c->post; i++) {
for (int j = 0; j < c->pre; j++) {
c->w[i * c->pre + j] = conf->w[i * c->pre + j];
}
}
}
// Free allocated memory for connection
void free_connection(Connection *c) {
// calloc() was used for weight array
// Only one call, so only one free (as opposed to other methods for 2D arrays)
free(c->w);
}
// Forward
// Spikes as floats to deal with real-valued inputs
void forward_connection(Connection *c, float x[], float const s[]) {
// Loop over weights and multiply with spikes
for (int i = 0; i < c->post; i++) {
for (int j = 0; j < c->pre; j++) {
x[i] += c->w[i * c->pre + j] * s[j];
}
}
}