julien.c 11.2 KB
Newer Older
Vivien Kraus's avatar
Vivien Kraus committed
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
/*
 * julien -- train a multi-layer perceptron
 * Copyright (C) 2019  Vivien Kraus

 * Licensed under the Apache License, Version 2.0 (the "License"); you
 * may not use this file except in compliance with the License.  You
 * may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied.  See the License for the specific language governing
 * permissions and limitations under the License.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif /* HAVE_CONFIG_H */

#include <stdlib.h>
#include "julien.h"
#include <assert.h>
#include <stdio.h>

static inline void *
xmalloc (size_t sz)
{
  void *ret = malloc (sz);
  if (ret == NULL)
    {
      abort ();
    }
  return ret;
}

struct julien
{
  size_t dim_input;
  size_t dim_output;
  size_t n_hidden;
  size_t *restrict dim_hidden;
  double *restrict parameters;
  double *restrict forward;
  double *restrict backward;
};

static size_t
count_parameters (const struct julien *julien)
{
  size_t i;
  size_t ret = 0;
  if (julien->n_hidden == 0)
    {
      return julien->dim_input * julien->dim_output;
    }
  ret += julien->dim_input * julien->dim_hidden[0];
  ret += julien->dim_hidden[julien->n_hidden - 1] * julien->dim_output;
  for (i = 1; i < julien->n_hidden; i++)
    {
      ret += julien->dim_hidden[i - 1] * julien->dim_hidden[i];
    }
  return ret;
}

static size_t
count_hidden_neurons (const struct julien *julien)
{
  size_t ret = 0;
  size_t i_layer;
  for (i_layer = 0; i_layer < julien->n_hidden; i_layer++)
    {
      ret += julien->dim_hidden[i_layer];
    }
  return ret;
}

struct julien *
julien_alloc (size_t dim_input, size_t dim_output, size_t n_hidden,
	      const size_t *dim_hidden)
{
  struct julien *ret = xmalloc (sizeof (struct julien));
  size_t i, n_parameters;
  double initial_value;
  size_t n_activations = dim_input + dim_output;
  ret->dim_input = dim_input;
  ret->dim_output = dim_output;
  ret->n_hidden = n_hidden;
  ret->dim_hidden = xmalloc (n_hidden * sizeof (size_t));
  for (i = 0; i < n_hidden; i++)
    {
      ret->dim_hidden[i] = dim_hidden[i];
    }
  n_parameters = count_parameters (ret);
  ret->parameters = xmalloc (n_parameters * sizeof (double));
  initial_value = 1.0 / n_parameters;
  for (i = 0; i < n_parameters; i++)
    {
Vivien Kraus's avatar
Vivien Kraus committed
100
101
102
103
104
105
      double sign = 1;
      if (i % 3 == 0)
	{
	  sign = -1;
	}
      ret->parameters[i] = sign * initial_value;
Vivien Kraus's avatar
Vivien Kraus committed
106
107
108
109
110
111
112
113
114
115
    }
  n_activations += count_hidden_neurons (ret);
  ret->forward = xmalloc (n_activations * sizeof (double));
  ret->backward = xmalloc (n_activations * sizeof (double));
  return ret;
}

void
julien_free (struct julien *julien)
{
Vivien Kraus's avatar
Vivien Kraus committed
116
117
118
119
120
121
122
  if (julien != NULL)
    {
      free (julien->parameters);
      free (julien->dim_hidden);
      free (julien->forward);
      free (julien->backward);
    }
Vivien Kraus's avatar
Vivien Kraus committed
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
  free (julien);
}

const double *
julien_data (const struct julien *julien, size_t *n_parameters)
{
  if (n_parameters)
    {
      *n_parameters = count_parameters (julien);
    }
  return julien->parameters;
}

void
julien_set_data (struct julien *julien, size_t start, size_t n,
		 const double *restrict parameters)
{
  size_t n_total = count_parameters (julien);
  size_t i;
  for (i = 0; i < n && i + start < n_total; i++)
    {
      julien->parameters[i + start] = parameters[i];
    }
}

static void
forward_layer (size_t n_inputs, const double *restrict inputs,
	       size_t n_outputs, double *restrict outputs,
	       const double *restrict weights, int first)
{
  size_t i_output;
  size_t i_weight = 0;
  for (i_output = 0; i_output < n_outputs; i_output++)
    {
      size_t i_link;
      outputs[i_output] = 0;
      for (i_link = 0; i_link < n_inputs; i_link++)
	{
	  if (first || inputs[i_link] >= 0)
	    {
	      outputs[i_output] += weights[i_weight] * inputs[i_link];
	    }
	  i_weight++;
	}
    }
  assert (i_weight == n_outputs * n_inputs);
}

static void
backward_layer (size_t n_inputs, double *restrict inputs,
		size_t n_outputs, const double *restrict outputs,
		const double *restrict weights,
		const double *restrict activations, int relu)
{
  size_t i_input, i_output;
  size_t i_weight = 0;
  for (i_input = 0; i_input < n_inputs; i_input++)
    {
      inputs[i_input] = 0;
    }
  for (i_output = 0; i_output < n_outputs; i_output++)
    {
      for (i_input = 0; i_input < n_inputs; i_input++)
	{
	  const double weight = weights[i_weight++];
	  if (!relu || activations[i_input] > 0)
	    {
	      inputs[i_input] += weight * outputs[i_output];
	    }
	}
    }
}

static void
gradient_layer (size_t n_inputs, const double *restrict inputs,
		size_t n_outputs, const double *restrict outputs,
		double *restrict weights, double learning_rate,
		double *restrict sum_updates, double *restrict n_updates,
		int relu_in)
{
  size_t i_input, i_output;
  size_t i_weight = 0;
  for (i_output = 0; i_output < n_outputs; i_output++)
    {
      for (i_input = 0; i_input < n_inputs; i_input++)
	{
	  double in_value = inputs[i_input];
	  double error_value = outputs[i_output];
	  double g, abs_g;
	  if (relu_in && in_value < 0)
	    {
	      in_value = 0;
	    }
	  g = in_value * error_value;
	  abs_g = g;
	  if (abs_g < 0)
	    {
	      abs_g = -g;
	    }
	  weights[i_weight++] -= learning_rate * g;
Vivien Kraus's avatar
Vivien Kraus committed
223
	  *sum_updates += learning_rate * abs_g;
Vivien Kraus's avatar
Vivien Kraus committed
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
	  *n_updates += 1.0;
	}
    }
}

static inline void
forward_net_aux (struct julien *julien, size_t *restrict i_neuron,
		 size_t *restrict i_parameter, size_t layer_dim_input,
		 size_t layer_dim_output, double *restrict activations,
		 int *restrict first)
{
  forward_layer (layer_dim_input, &(activations[*i_neuron]), layer_dim_output,
		 &(activations[*i_neuron + layer_dim_input]),
		 &(julien->parameters[*i_parameter]), *first);
  *first = 0;
  *i_neuron += layer_dim_input;
  *i_parameter += (layer_dim_input) * (layer_dim_output);
}

static inline size_t
forward_net (struct julien *julien, size_t dim_input,
	     const double *restrict input)
{
  size_t i_neuron, i_parameter, i_layer;
  size_t layer_dim_input = julien->dim_input;
  size_t layer_dim_output;
  int first = 1;
  for (i_neuron = 0; i_neuron < dim_input && i_neuron < julien->dim_input;
       i_neuron++)
    {
      julien->forward[i_neuron] = input[i_neuron];
    }
  for (i_neuron = dim_input; i_neuron < julien->dim_input; i_neuron++)
    {
      julien->forward[i_neuron] = 0;
    }
  i_neuron = 0;
  i_parameter = 0;
  for (i_layer = 0; i_layer < julien->n_hidden; i_layer++)
    {
      layer_dim_output = julien->dim_hidden[i_layer];
      forward_net_aux (julien, &i_neuron, &i_parameter, layer_dim_input,
		       layer_dim_output, julien->forward, &first);
      layer_dim_input = layer_dim_output;
    }
  layer_dim_output = julien->dim_output;
  forward_net_aux (julien, &i_neuron, &i_parameter, layer_dim_input,
		   layer_dim_output, julien->forward, &first);
  assert (i_parameter == count_parameters (julien));
  return i_neuron + layer_dim_output;
}

static inline void
backward_net_aux (struct julien *julien, size_t *restrict i_neuron,
		  size_t *restrict i_parameter, size_t layer_dim_input,
		  size_t layer_dim_output, double *restrict activations,
		  int relu)
{
  *i_neuron -= layer_dim_input;
  *i_parameter -= layer_dim_input * layer_dim_output;
  backward_layer (layer_dim_input, &(activations[*i_neuron]),
		  layer_dim_output,
		  &(activations[*i_neuron + layer_dim_input]),
		  &(julien->parameters[*i_parameter]),
		  &(julien->forward[*i_neuron]), relu);
}

static inline size_t
backward_net (struct julien *julien, size_t dim_output,
	      const double *restrict output)
{
  size_t n_activations =
    julien->dim_input + julien->dim_output + count_hidden_neurons (julien);
  size_t i_neuron = n_activations, i_parameter =
    count_parameters (julien), j, i_layer;
  size_t layer_dim_input;
  size_t layer_dim_output = julien->dim_output;
  /* The errors for the last layer is easy, since we are doing least
   * squares regression: we simply subtract the target */
  i_neuron -= layer_dim_output;
  for (j = 0; j < julien->dim_output; j++)
    {
      julien->backward[n_activations - j - 1] =
	julien->forward[n_activations - j - 1];
    }
  for (j = 0; j < dim_output && j < julien->dim_output; j++)
    {
      julien->backward[i_neuron + j] -= output[j];
    }
  for (i_layer = julien->n_hidden; i_layer != 0; i_layer--)
    {
      layer_dim_input = julien->dim_hidden[i_layer - 1];
      backward_net_aux (julien, &i_neuron, &i_parameter, layer_dim_input,
			layer_dim_output, julien->backward, 1);
      layer_dim_output = layer_dim_input;
    }
  layer_dim_input = julien->dim_input;
  backward_net_aux (julien, &i_neuron, &i_parameter, layer_dim_input,
		    layer_dim_output, julien->backward, 0);
  assert (i_parameter == 0);
  return i_neuron;
}

static inline double
gradient_net (struct julien *julien, double learning_rate)
{
  size_t layer_dim_input = julien->dim_input;
  size_t layer_dim_output;
  double n_updates = 0;
  double sum_updates = 0;
  size_t i_layer;
  size_t i_neuron = 0, i_parameter = 0;
  int relu_in = 0;
  for (i_layer = 0; i_layer < julien->n_hidden; i_layer++)
    {
      layer_dim_output = julien->dim_hidden[i_layer];
      gradient_layer (layer_dim_input, &(julien->forward[i_neuron]),
		      layer_dim_output,
		      &(julien->backward[i_neuron + layer_dim_input]),
		      &(julien->parameters[i_parameter]), learning_rate,
		      &sum_updates, &n_updates, relu_in);
      relu_in = 1;
      i_neuron += layer_dim_input;
      i_parameter += layer_dim_input * layer_dim_output;
      layer_dim_input = layer_dim_output;
    }
  layer_dim_output = julien->dim_output;
  gradient_layer (layer_dim_input, &(julien->forward[i_neuron]),
		  layer_dim_output,
		  &(julien->backward[i_neuron + layer_dim_input]),
		  &(julien->parameters[i_parameter]), learning_rate,
		  &sum_updates, &n_updates, relu_in);
  i_neuron += layer_dim_input;
  i_parameter += layer_dim_input * layer_dim_output;
  assert (i_neuron == count_hidden_neurons (julien) + julien->dim_input);
  assert (i_parameter == count_parameters (julien));
  return sum_updates / n_updates;
}

double
julien_learn (struct julien *julien, size_t dim_input,
	      const double *restrict input, size_t dim_output,
	      const double *restrict output, double learning_rate)
{
  size_t n_activations =
    julien->dim_input + julien->dim_output + count_hidden_neurons (julien);
  if (forward_net (julien, dim_input, input) != n_activations)
    {
      assert (0);
    }
  /* activations are progressively replaced with the errors */
  if (backward_net (julien, dim_output, output) != 0)
    {
      assert (0);
    }
  return gradient_net (julien, learning_rate);
}

size_t
julien_predict (struct julien *julien, size_t dim_input, const double *input,
		size_t max_output, size_t start_output,
		double *restrict output)
{
  size_t n_activations =
    julien->dim_input + julien->dim_output + count_hidden_neurons (julien);
  size_t j;
  if (forward_net (julien, dim_input, input) != n_activations)
    {
      assert (0);
    }
  for (j = 0; j < max_output && j + start_output < julien->dim_output; j++)
    {
      output[j] =
	julien->forward[n_activations - julien->dim_output + start_output +
			j];
    }
  return julien->dim_output;
}