-
Notifications
You must be signed in to change notification settings - Fork 0
/
neuron.c
54 lines (43 loc) · 1.19 KB
/
neuron.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
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <math.h>
#include "../../tensors/tensors.h"
#include "../../activations/activations.h"
#include "../../utils/utils.h"
#include "neuron.h"
Neuron neuron_new_(uint n_inputs, Activation activation) {
Neuron neuron = (Neuron) malloc(sizeof(Neuron));
neuron->w = vector_new_randfloat_(n_inputs);
neuron->b = randfloat();
neuron->activation = activation;
return neuron;
}
void neuron_free(Neuron n) {
vector_free(n->w);
free(n);
}
void neurons_free(int n, Neuron* neurons) {
for (int i = 0; i < n; i++) {
neuron_free(neurons[n]);
}
}
float neuron_forward(Neuron n, Vector x) {
/* z = x.T @ w + b */
float z = dotprod(x, n->w) + n->b;
switch (n->activation) {
case Identity: return z;
case Sigmoid: return sigmoid(z);
case ReLU: return relu(z);
case Tanh: return tanh(z);
default: assert(0);
}
}
Vector neuron_forward_batch(Neuron n, Matrix X, Vector preds) {
assert(n); assert(X); assert(preds);
for (int i = 0; i < X->m; i++) {
Vector x = X->d[i];
preds->d[i] = neuron_forward(n, x);
}
return preds;
}