-
Notifications
You must be signed in to change notification settings - Fork 0
/
neuron.c
90 lines (77 loc) · 2.42 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
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
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "mylib.h"
#include "neuron.h"
/* Struct for a single Neuron.
* layer: the depth of the neuron in the network begining at 0.
* num_inputs: the number of values that affect the neurons output.
* bais: a constant adding to the neurons output.
* weights: the 'significance' of each input value.
*/
struct neuronrec {
int layer;
int num_inputs;
double bais;
double *weights;
double result;
};
/* Allocates memory and initializes values for weights and bais as random value
* between -1 and 1.
* parameter - layuer: the position of the neuron in the network.
* parameter - num_inputs: the number of input values the neuron has.
* returns - n: the new neuron.
*/
neuron neuron_new(int layer, int num_inputs) {
neuron n = emalloc(sizeof * n);
int i;
srand(time(NULL));
n->layer = layer;
n->result = 0.0;
n->num_inputs = num_inputs;
n->bais = (double)rand()/RAND_MAX*2.0-1.0;
n->weights = emalloc(n->num_inputs * sizeof n->weights[0]);
for (i = 0; i < num_inputs; i++) {
n->weights[i] = (double)rand()/RAND_MAX*2.0-1.0;
}
return n;
}
/* Simple infomation about the neuron for testing.
* parameter - n: the neuron that we wish to see info for.
*/
void neuron_print(neuron n) {
int i;
printf("Neuron at layer %d has %d inputs\n", n->layer, n->num_inputs);
for (i = 0; i < n->num_inputs; i++) {
printf("Input %d has weight %f \n", i, n->weights[i]);
}
printf("Bais: %f \n", n->bais);
}
/* Deallocated memory for neuron
* parameter - n: the neuron to free.
*/
void neuron_free(neuron n) {
free(n->weights);
free(n);
}
/* Computes the output value of a given neuron for given input values.
* parameter - inputs: the input values (either the raw data, or the results of
* the last layers calculation).
* parameter - n: the neuron we are findong the output for.
* returns - result: the output value of the calculation (note that the result
* has not been put through the sigmoid function, this is because the
* back_propogate function needs the raw predicition value to compute the
* partial derivitive).
*/
void forward_pass(double *inputs, neuron n) {
int i;
double result;
result = n->bais;
for (i = 0; i < n->num_inputs; i++) {
result += inputs[i] * n->weights[i];
}
n->result = sigmoid(result);
}
double get_result(neuron n) {
return n->result;
}