mirror of https://github.com/pjreddie/darknet.git
72 lines
2.0 KiB
C
72 lines
2.0 KiB
C
#include "logistic_layer.h"
|
|
#include "activations.h"
|
|
#include "blas.h"
|
|
#include "cuda.h"
|
|
|
|
#include <float.h>
|
|
#include <math.h>
|
|
#include <stdlib.h>
|
|
#include <stdio.h>
|
|
#include <assert.h>
|
|
|
|
layer make_logistic_layer(int batch, int inputs)
|
|
{
|
|
fprintf(stderr, "logistic x entropy %4d\n", inputs);
|
|
layer l = {0};
|
|
l.type = LOGXENT;
|
|
l.batch = batch;
|
|
l.inputs = inputs;
|
|
l.outputs = inputs;
|
|
l.loss = calloc(inputs*batch, sizeof(float));
|
|
l.output = calloc(inputs*batch, sizeof(float));
|
|
l.delta = calloc(inputs*batch, sizeof(float));
|
|
l.cost = calloc(1, sizeof(float));
|
|
|
|
l.forward = forward_logistic_layer;
|
|
l.backward = backward_logistic_layer;
|
|
#ifdef GPU
|
|
l.forward_gpu = forward_logistic_layer_gpu;
|
|
l.backward_gpu = backward_logistic_layer_gpu;
|
|
|
|
l.output_gpu = cuda_make_array(l.output, inputs*batch);
|
|
l.loss_gpu = cuda_make_array(l.loss, inputs*batch);
|
|
l.delta_gpu = cuda_make_array(l.delta, inputs*batch);
|
|
#endif
|
|
return l;
|
|
}
|
|
|
|
void forward_logistic_layer(const layer l, network net)
|
|
{
|
|
copy_cpu(l.outputs*l.batch, net.input, 1, l.output, 1);
|
|
activate_array(l.output, l.outputs*l.batch, LOGISTIC);
|
|
if(net.truth){
|
|
logistic_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);
|
|
}
|
|
}
|
|
|
|
void backward_logistic_layer(const layer l, network net)
|
|
{
|
|
axpy_cpu(l.inputs*l.batch, 1, l.delta, 1, net.delta, 1);
|
|
}
|
|
|
|
#ifdef GPU
|
|
|
|
void forward_logistic_layer_gpu(const layer l, network net)
|
|
{
|
|
copy_gpu(l.outputs*l.batch, net.input_gpu, 1, l.output_gpu, 1);
|
|
activate_array_gpu(l.output_gpu, l.outputs*l.batch, LOGISTIC);
|
|
if(net.truth){
|
|
logistic_x_ent_gpu(l.batch*l.inputs, l.output_gpu, net.truth_gpu, l.delta_gpu, l.loss_gpu);
|
|
cuda_pull_array(l.loss_gpu, l.loss, l.batch*l.inputs);
|
|
l.cost[0] = sum_array(l.loss, l.batch*l.inputs);
|
|
}
|
|
}
|
|
|
|
void backward_logistic_layer_gpu(const layer l, network net)
|
|
{
|
|
axpy_gpu(l.batch*l.inputs, 1, l.delta_gpu, 1, net.delta_gpu, 1);
|
|
}
|
|
|
|
#endif
|