2013-12-03 04:41:40 +04:00
|
|
|
#include "softmax_layer.h"
|
2015-01-23 03:38:24 +03:00
|
|
|
#include "blas.h"
|
|
|
|
#include "cuda.h"
|
2014-10-22 01:49:18 +04:00
|
|
|
#include <float.h>
|
2013-12-03 04:41:40 +04:00
|
|
|
#include <math.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
2015-02-24 05:52:05 +03:00
|
|
|
#include <assert.h>
|
2013-12-03 04:41:40 +04:00
|
|
|
|
2015-05-11 23:46:49 +03:00
|
|
|
softmax_layer make_softmax_layer(int batch, int inputs, int groups)
|
2013-12-03 04:41:40 +04:00
|
|
|
{
|
2015-02-24 05:52:05 +03:00
|
|
|
assert(inputs%groups == 0);
|
2013-12-06 01:17:16 +04:00
|
|
|
fprintf(stderr, "Softmax Layer: %d inputs\n", inputs);
|
2015-05-11 23:46:49 +03:00
|
|
|
softmax_layer l = {0};
|
|
|
|
l.type = SOFTMAX;
|
|
|
|
l.batch = batch;
|
|
|
|
l.groups = groups;
|
|
|
|
l.inputs = inputs;
|
|
|
|
l.outputs = inputs;
|
|
|
|
l.output = calloc(inputs*batch, sizeof(float));
|
|
|
|
l.delta = calloc(inputs*batch, sizeof(float));
|
2016-09-25 09:12:54 +03:00
|
|
|
|
|
|
|
l.forward = forward_softmax_layer;
|
|
|
|
l.backward = backward_softmax_layer;
|
2014-10-22 01:49:18 +04:00
|
|
|
#ifdef GPU
|
2016-09-25 09:12:54 +03:00
|
|
|
l.forward_gpu = forward_softmax_layer_gpu;
|
|
|
|
l.backward_gpu = backward_softmax_layer_gpu;
|
|
|
|
|
2015-05-11 23:46:49 +03:00
|
|
|
l.output_gpu = cuda_make_array(l.output, inputs*batch);
|
|
|
|
l.delta_gpu = cuda_make_array(l.delta, inputs*batch);
|
2014-10-22 01:49:18 +04:00
|
|
|
#endif
|
2015-05-11 23:46:49 +03:00
|
|
|
return l;
|
2013-12-03 04:41:40 +04:00
|
|
|
}
|
|
|
|
|
2015-05-11 23:46:49 +03:00
|
|
|
void forward_softmax_layer(const softmax_layer l, network_state state)
|
2014-01-29 04:28:42 +04:00
|
|
|
{
|
2015-02-24 05:52:05 +03:00
|
|
|
int b;
|
2015-05-11 23:46:49 +03:00
|
|
|
int inputs = l.inputs / l.groups;
|
|
|
|
int batch = l.batch * l.groups;
|
2016-10-21 23:16:43 +03:00
|
|
|
if(l.softmax_tree){
|
|
|
|
for(b = 0; b < batch; ++b){
|
|
|
|
int i;
|
|
|
|
int count = 0;
|
|
|
|
for(i = 0; i < l.softmax_tree->groups; ++i){
|
|
|
|
int group_size = l.softmax_tree->group_size[i];
|
|
|
|
softmax(state.input+b*inputs + count, group_size, l.temperature, l.output+b*inputs + count);
|
|
|
|
count += group_size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for(b = 0; b < batch; ++b){
|
|
|
|
softmax(state.input+b*inputs, inputs, l.temperature, l.output+b*inputs);
|
|
|
|
}
|
2014-01-29 04:28:42 +04:00
|
|
|
}
|
|
|
|
}
|
2013-12-03 04:41:40 +04:00
|
|
|
|
2015-05-11 23:46:49 +03:00
|
|
|
void backward_softmax_layer(const softmax_layer l, network_state state)
|
2013-12-03 04:41:40 +04:00
|
|
|
{
|
|
|
|
int i;
|
2015-05-11 23:46:49 +03:00
|
|
|
for(i = 0; i < l.inputs*l.batch; ++i){
|
2015-07-22 02:09:33 +03:00
|
|
|
state.delta[i] += l.delta[i];
|
2013-12-03 04:41:40 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-21 23:16:43 +03:00
|
|
|
#ifdef GPU
|
|
|
|
|
|
|
|
void pull_softmax_layer_output(const softmax_layer layer)
|
|
|
|
{
|
|
|
|
cuda_pull_array(layer.output_gpu, layer.output, layer.inputs*layer.batch);
|
|
|
|
}
|
|
|
|
|
|
|
|
void forward_softmax_layer_gpu(const softmax_layer l, network_state state)
|
|
|
|
{
|
|
|
|
int inputs = l.inputs / l.groups;
|
|
|
|
int batch = l.batch * l.groups;
|
|
|
|
if(l.softmax_tree){
|
2016-10-24 23:32:49 +03:00
|
|
|
int i;
|
|
|
|
int count = 0;
|
|
|
|
for (i = 0; i < l.softmax_tree->groups; ++i) {
|
|
|
|
int group_size = l.softmax_tree->group_size[i];
|
|
|
|
softmax_gpu(state.input+count, group_size, inputs, batch, l.temperature, l.output_gpu + count);
|
|
|
|
count += group_size;
|
2016-10-21 23:16:43 +03:00
|
|
|
}
|
|
|
|
} else {
|
2016-10-24 23:32:49 +03:00
|
|
|
softmax_gpu(state.input, inputs, inputs, batch, l.temperature, l.output_gpu);
|
2016-10-21 23:16:43 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void backward_softmax_layer_gpu(const softmax_layer layer, network_state state)
|
|
|
|
{
|
|
|
|
axpy_ongpu(layer.batch*layer.inputs, 1, layer.delta_gpu, 1, state.delta, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|