-
Notifications
You must be signed in to change notification settings - Fork 0
/
layer.c
68 lines (54 loc) · 1.73 KB
/
layer.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
#include <stdlib.h>
#include <time.h>
#include "layer.h"
LayerPtr Layer_create(int input_size, int size){
srand(time(NULL));
LayerPtr newLayer = (LayerPtr) malloc(sizeof(Layer) + sizeof(NeuronPtr[size]));
newLayer->input_size = input_size;
newLayer->size = size;
newLayer->prev = NULL;
newLayer->next = NULL;
for (int i = 0; i < size; i++) {
newLayer->neurons[i] = Neuron_create(input_size);
}
// to store outputs
newLayer->output = Tensor_create_empty(size);
for(int i = 0; i < size; i++){
newLayer->output->elements[i] = newLayer->neurons[i]->output;
}
return newLayer;
}
// UNTESTED, gmn ya cara ngetes yang bener kwaokwaka
int Layer_forward(LayerPtr layer, TensorPtr tensor){
if(layer->input_size != tensor->size){
printf("layer->input_size and input_size mismatch!\n"); // i really should do checks like these to all lol
return -1;
}
// calculate output for every neuron against the tensor input
for(int i = 0; i < layer->size; i++){
Neuron_forward(layer->neurons[i], tensor);
}
// updating the output pointers to point at Values of the neuron outputs
for(int i = 0; i < layer->size; i++){
layer->output->elements[i] = layer->neurons[i]->output;
}
return 0;
}
int Layer_backward(LayerPtr layer){
// thassit?
for(int i = 0; i < layer->size; i++){
Neuron_backward(layer->neurons[i]);
}
return 0;
}
void Layer_print_output(LayerPtr layer){
Tensor_print(layer->output);
}
void Layer_print_output_grad(LayerPtr layer){
Tensor_print_grad(layer->output);
}
void Layer_print_wandb(LayerPtr layer){
for(int i = 0; i < layer->size; i++){
Neuron_print_w_and_b(layer->neurons[i]);
}
}