-
Notifications
You must be signed in to change notification settings - Fork 0
/
init_store_load.c
61 lines (53 loc) · 1.98 KB
/
init_store_load.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
#include "../src/architecture.h"
#include "../src/activations.h"
#include "../src/persistence.h"
#include "../src/nnc_utils.h"
#include "../src/pass.h"
int example_init_display()
{
srand(time(NULL)); // Init randomness
int ret;
network nk;
uint16_t n_layers = 3;
printf("[INFO] Initializing and displaying a network with 3 layers:\n");
init_network(&nk, n_layers);
// First layer with input of size 5, 7 neurons, Glorot Gaussian weights & biases initialization and ReLu activation:
addinit_layer(&nk, 5, 7, GLOROT_UNIFORM_INIT, RELU_ACT);
// Input size is unrelevant for the following layers, will be set to output size of previous layer anyway
addinit_layer(&nk, 0, 3, GLOROT_UNIFORM_INIT, RELU_ACT);
addinit_layer(&nk, 0, 1, GLOROT_GAUSSIAN_INIT, SIGMOID_ACT);
print_network(stdout, &nk);
// Persist network
char *network_fp = "examples/network.txt";
printf("[INFO] Persisting network at %s\n", network_fp);
persist_network(&nk, network_fp);
// Load network
printf("[INFO] Loading network at %s\n", network_fp);
network loaded_nk;
load_network(&loaded_nk, network_fp);
printf("[INFO] network loaded:\n");
print_network(stdout, &loaded_nk);
// Forward pass
uint16_t batch_s = 3;
uint16_t input_s = 5;
float batch[batch_s][input_s];
for (uint16_t i = 0; i < batch_s; i++)
for (uint16_t j = 0; j < input_s; j++)
batch[i][j] = -0.4 + 0.2 * (float)i + (float)j / (float)input_s;
uint16_t output_s = 1;
float output[batch_s][output_s];
batch_forward(&loaded_nk, batch_s, input_s, batch, output_s, output);
printf("Batch (before forward):\n");
print_matrix(stdout, (size_t)batch_s, (size_t)input_s, batch);
printf("Output (after forward):\n");
print_matrix(stdout, (size_t)batch_s, (size_t)output_s, output);
free_network(&nk);
free_network(&loaded_nk);
return EXIT_SUCCESS;
}
int main()
{
int ret;
ret = example_init_display();
return ret;
}