-
Notifications
You must be signed in to change notification settings - Fork 106
/
Copy pathnnet.h
173 lines (148 loc) · 4.26 KB
/
nnet.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#pragma once
#include "common.h"
#include "vec.h"
#include "quat.h"
#include "array.h"
#include <assert.h>
#include <stdio.h>
#include <math.h>
#include <vector>
//--------------------------------------
// Very basic feed-forward neural network
// class. Assumes relu activation on every
// layer except the last. Also includes
// normalization and denormalization to make
// preparing the inputs and outputs easier.
struct nnet
{
array1d<float> input_mean;
array1d<float> input_std;
array1d<float> output_mean;
array1d<float> output_std;
std::vector<array2d<float>> weights;
std::vector<array1d<float>> biases;
};
void nnet_load(nnet& nn, const char* filename)
{
FILE* f = fopen(filename, "rb");
assert(f != NULL);
array1d_read(nn.input_mean, f);
array1d_read(nn.input_std, f);
array1d_read(nn.output_mean, f);
array1d_read(nn.output_std, f);
int count;
fread(&count, sizeof(int), 1, f);
nn.weights.resize(count);
nn.biases.resize(count);
for (int i = 0; i < count; i++)
{
array2d_read(nn.weights[i], f);
array1d_read(nn.biases[i], f);
}
fclose(f);
}
//--------------------------------------
static inline void nnet_layer_normalize(
slice1d<float> output,
const slice1d<float> mean,
const slice1d<float> std)
{
for (int i = 0; i < output.size; i++)
{
output(i) = (output(i) - mean(i)) / std(i);
}
}
static inline void nnet_layer_denormalize(
slice1d<float> output,
const slice1d<float> mean,
const slice1d<float> std)
{
for (int i = 0; i < output.size; i++)
{
output(i) = output(i) * std(i) + mean(i);
}
}
// This appears to be the fastest way I found to do basic
// matmul on the CPU. It takes advantage of activations set
// to zero due to relu and compiles well to SIMD. It's important
// for performance that the pointers are labelled restrict.
static inline void nnet_layer_linear(
slice1d<float> output,
const slice1d<float> input,
const slice2d<float> weights,
const slice1d<float> biases)
{
// Copy bias to output
for (int j = 0; j < output.size; j++)
{
output(j) = biases(j);
}
// Accumulate in output the result of matmul
for (int i = 0; i < input.size; i++)
{
// Often activations are zero due to relu
// so this provides a good speedup
if (input(i) != 0.0f)
{
for (int j = 0; j < output.size; j++)
{
output(j) += input(i) * weights(i, j);
}
}
}
}
static inline void nnet_layer_relu(slice1d<float> output)
{
for (int i = 0; i < output.size; i++)
{
output(i) = maxf(output(i), 0.0f);
}
}
//--------------------------------------
// Basic class that can be used to pre-allocate
// the storage required to do network inference
// (i.e. activations).
struct nnet_evaluation
{
std::vector<array1d<float>> layers;
// Resize for a given network
void resize(const nnet& nn)
{
layers.resize(nn.weights.size() + 1);
layers.front().resize(nn.weights.front().rows);
for (int i = 0; i < nn.weights.size(); i++)
{
layers[i+1].resize(nn.weights[i].cols);
}
}
};
// Neural Network evaluation function. Assumes input
// has been placed in first layer of `evaulation`
// object. Puts result in the last layer of the
// `evaluation` object.
void nnet_evaluate(
nnet_evaluation& evaluation,
const nnet& nn)
{
nnet_layer_normalize(
evaluation.layers.front(),
nn.input_mean,
nn.input_std);
for (int i = 0; i < nn.weights.size(); i++)
{
nnet_layer_linear(
evaluation.layers[i+1],
evaluation.layers[i],
nn.weights[i],
nn.biases[i]);
// No relu for final layer
if (i != nn.weights.size() - 1)
{
nnet_layer_relu(evaluation.layers[i+1]);
}
}
nnet_layer_denormalize(
evaluation.layers.back(),
nn.output_mean,
nn.output_std);
}