darknet/src/softmax_layer.c

108 lines
3.5 KiB
C
Raw Normal View History

2017-06-02 06:31:13 +03:00
#include "softmax_layer.h"
#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);
2016-11-16 11:15:46 +03:00
fprintf(stderr, "softmax %4d\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.loss = calloc(inputs*batch, sizeof(float));
2015-05-11 23:46:49 +03:00
l.output = calloc(inputs*batch, sizeof(float));
l.delta = calloc(inputs*batch, sizeof(float));
l.cost = calloc(1, sizeof(float));
l.forward = forward_softmax_layer;
l.backward = backward_softmax_layer;
2014-10-22 01:49:18 +04:00
#ifdef GPU
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.loss_gpu = cuda_make_array(l.loss, inputs*batch);
2015-05-11 23:46:49 +03:00
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
}
void forward_softmax_layer(const softmax_layer l, network net)
2016-11-06 00:09:21 +03:00
{
if(l.softmax_tree){
2016-11-06 00:09:21 +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_cpu(net.input + count, group_size, l.batch, l.inputs, 1, 0, 1, l.temperature, l.output + count);
2016-11-06 00:09:21 +03:00
count += group_size;
}
2016-10-21 23:16:43 +03:00
} else {
softmax_cpu(net.input, l.inputs/l.groups, l.batch, l.inputs, l.groups, l.inputs/l.groups, 1, l.temperature, l.output);
}
if(net.truth){
softmax_x_ent_cpu(l.batch*l.inputs, l.output, net.truth, l.delta, l.loss);
l.cost[0] = sum_array(l.loss, l.batch*l.inputs);
}
}
2013-12-03 04:41:40 +04:00
void backward_softmax_layer(const softmax_layer l, network net)
2013-12-03 04:41:40 +04:00
{
axpy_cpu(l.inputs*l.batch, 1, l.delta, 1, net.delta, 1);
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 net)
2016-10-21 23:16:43 +03:00
{
if(l.softmax_tree){
softmax_tree(net.input_gpu, 1, l.batch, l.inputs, l.temperature, l.output_gpu, *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(net.input_gpu + count, group_size, l.batch, l.inputs, 1, 0, 1, l.temperature, l.output_gpu + count);
2016-10-24 23:32:49 +03:00
count += group_size;
2016-10-21 23:16:43 +03:00
}
*/
2016-10-21 23:16:43 +03:00
} else {
2017-05-28 07:41:55 +03:00
if(l.spatial){
softmax_gpu(net.input_gpu, l.c, l.batch*l.c, l.inputs/l.c, l.w*l.h, 1, l.w*l.h, 1, l.output_gpu);
}else{
softmax_gpu(net.input_gpu, l.inputs/l.groups, l.batch, l.inputs, l.groups, l.inputs/l.groups, 1, l.temperature, l.output_gpu);
}
2016-10-21 23:16:43 +03:00
}
if(net.truth){
softmax_x_ent_gpu(l.batch*l.inputs, l.output_gpu, net.truth_gpu, l.delta_gpu, l.loss_gpu);
if(l.softmax_tree){
mask_gpu(l.batch*l.inputs, l.delta_gpu, SECRET_NUM, net.truth_gpu, 0);
mask_gpu(l.batch*l.inputs, l.loss_gpu, SECRET_NUM, net.truth_gpu, 0);
}
cuda_pull_array(l.loss_gpu, l.loss, l.batch*l.inputs);
l.cost[0] = sum_array(l.loss, l.batch*l.inputs);
}
2016-10-21 23:16:43 +03:00
}
void backward_softmax_layer_gpu(const softmax_layer layer, network net)
2016-10-21 23:16:43 +03:00
{
2017-06-18 23:05:37 +03:00
axpy_gpu(layer.batch*layer.inputs, 1, layer.delta_gpu, 1, net.delta_gpu, 1);
2016-10-21 23:16:43 +03:00
}
#endif