-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrun_network.c
113 lines (105 loc) · 2.41 KB
/
run_network.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include "neural.h"
void back_propagationIH(t_network *network)
{
t_neural *node_in = network->input_l->start;
while (node_in)
{
t_neural *node_hi = network->hidden_l->start;
int i = 0;
while (node_hi)
{
node_in->link[i] += node_hi->d_error/* * node_in->value*/ * node_in->r_learn;
node_hi = node_hi->next;
i++;
}
node_in = node_in->next;
}
}
void process_hidden_l_error(t_network *network)
{
t_neural *node_hi = network->hidden_l->start;
while (node_hi)
{
t_neural *node_out = network->output_l->start;
int i = 0;
double tmp = 0;
while (node_out)
{
tmp += node_out->d_error * node_hi->link[i];
node_out = node_out->next;
i++;
}
node_hi->d_error = node_hi->value * (1 - node_hi->value) * tmp;
node_hi = node_hi->next;
}
}
void back_propagationHO(t_network *network)
{
t_neural *node_out = network->output_l->start;
int i = 0;
while (node_out)
{
node_out->d_error = node_out->value * (1 - node_out->value) * (node_out->target - node_out->value);
t_neural *node_hi = network->hidden_l->start;
while (node_hi)
{
double delta_w = node_hi->r_learn * node_out->d_error * node_hi->value;
node_hi->link[i] = node_hi->link[i] + delta_w;/* + (INERTIA * node_hi->prev_delta);*/
node_hi->prev_delta = delta_w;
node_hi = node_hi->next;
}
node_out = node_out->next;
i++;
}
}
void get_value_out(t_network *network)
{
t_neural *node_out = network->output_l->start;
int i = 0;
while (node_out)
{
t_neural *node_hi = network->hidden_l->start;
while (node_hi)
{
node_out->w_sum += node_hi->link[i] * node_hi->value;
node_hi = node_hi->next;
}
node_out->value = sigmoid_func(node_out->w_sum);
node_out->w_sum = 0;
node_out = node_out->next;
i++;
}
}
void get_value_hid(t_network *network)
{
t_neural *node_hi = network->hidden_l->start;
int i = 0;
while (node_hi)
{
t_neural *node_in = network->input_l->start;
while (node_in)
{
node_hi->w_sum += (node_in->link[i] * node_in->value);
node_in = node_in->next;
}
node_hi->value = sigmoid_func(node_hi->w_sum);
node_hi->w_sum = 0;
node_hi = node_hi->next;
i++;
}
}
void run_network(t_network *network, int nb_it)
{
int i = 0;
while (i < nb_it)
{
get_value_hid(network);
get_value_out(network);
back_propagationHO(network);
process_hidden_l_error(network);
back_propagationIH(network);
i++;
}
printf("Final result:\n");
print_layer(network->output_l);
}