-
Notifications
You must be signed in to change notification settings - Fork 0
/
structs.c
42 lines (35 loc) · 1.2 KB
/
structs.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
#include <stdlib.h>
#include "util.h"
#include "structs.h"
// construct a new r by c matrix
Matrix * initMatrix(int r, int c) {
Matrix * m = malloc(sizeof(Matrix));
m->rows = r; m->cols = c;
// allocate row and column arrays
m->at = (float **) malloc(r * sizeof(float *));
for (int i = 0; i < r; i++)
m->at[i] = (float *) calloc(c, sizeof(float));
return m;
}
// construct a new network off of given params
NeuralNetwork * initNN(int numLayers, int * params) {
NeuralNetwork * n = malloc(sizeof(NeuralNetwork)); // allocate network
n->numberOfLayers = numLayers;
n->params = paramCopy(params, numLayers);
n->w = malloc((numLayers - 1) * sizeof(Matrix *)); // allocate array of weight matrices
n->b = malloc((numLayers - 1) * sizeof(Matrix *)); // allocate array of bias vectors
// allocate individual weight and bias matrices
for (int i = 0; i < numLayers - 1; i++) {
n->w[i] = initMatrix(params[i + 1], params[i]);
n->b[i] = initMatrix(params[i + 1], 1);
}
return n;
}
// construct new training set
DataSet * initDataSet(int size) {
DataSet * d = malloc(sizeof(DataSet));
d->size = size;
d->inputs = malloc(size * sizeof(Matrix *));
d->outputs = malloc(size * sizeof(Matrix *));
return d;
}