-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathsoftmax_layer.cpp
More file actions
72 lines (63 loc) · 1.95 KB
/
softmax_layer.cpp
File metadata and controls
72 lines (63 loc) · 1.95 KB
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
//========================================================================
// Softmax layer
//========================================================================
// @brief: softmax layer
#include "softmax_layer.h"
// update softmax tree
void softmax_tree(float *input, int batch, int inputs, float temp, tree *hierarchy, float *output)
{
//
for(int b = 0; b < batch; ++b)
{
int count = 0;
for(int i = 0; i < hierarchy->groups; i++)
{
int group_size = hierarchy->group_size[i];
softmax(input+b*inputs + count, group_size, temp, output+b*inputs + count);
count += group_size;
}
}
}
// make softmax layer
softmax_layer make_softmax_layer(int batch, int inputs, int groups)
{
assert(inputs%groups == 0);
fprintf(stderr, "softmax %4d\n", inputs);
softmax_layer l;
init_layer(l);
l.type = SOFTMAX;
l.batch = batch;
l.groups = groups;
l.inputs = inputs;
l.outputs = inputs;
l.output = (float *)calloc(inputs*batch, sizeof(float));
l.delta = (float *)calloc(inputs*batch, sizeof(float));
l.forward = forward_softmax_layer;
l.backward = backward_softmax_layer;
return l;
}
// softmx layer top function
void forward_softmax_layer(const softmax_layer l, network_state state)
{
int inputs = l.inputs / l.groups;
int batch = l.batch * l.groups;
if(l.softmax_tree)
{
softmax_tree(state.input, batch, inputs, l.temperature, l.softmax_tree, l.output);
}
else
{
for(int b = 0; b < batch; b++)
{
softmax(state.input+b*inputs, inputs, l.temperature, l.output+b*inputs);
}
}
}
// backward softmax function
void backward_softmax_layer(const softmax_layer l, network_state state)
{
for(int i = 0; i < l.inputs*l.batch; i++)
{
state.delta[i] += l.delta[i];
}
}