2017-06-02 06:31:13 +03:00
|
|
|
#include "activation_layer.h"
|
|
|
|
#include "utils.h"
|
|
|
|
#include "cuda.h"
|
|
|
|
#include "blas.h"
|
|
|
|
#include "gemm.h"
|
2016-01-19 02:40:14 +03:00
|
|
|
|
|
|
|
#include <math.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
layer make_activation_layer(int batch, int inputs, ACTIVATION activation)
|
|
|
|
{
|
|
|
|
layer l = {0};
|
|
|
|
l.type = ACTIVE;
|
|
|
|
|
|
|
|
l.inputs = inputs;
|
|
|
|
l.outputs = inputs;
|
|
|
|
l.batch=batch;
|
|
|
|
|
|
|
|
l.output = calloc(batch*inputs, sizeof(float*));
|
|
|
|
l.delta = calloc(batch*inputs, sizeof(float*));
|
|
|
|
|
2016-09-25 09:12:54 +03:00
|
|
|
l.forward = forward_activation_layer;
|
|
|
|
l.backward = backward_activation_layer;
|
2016-01-19 02:40:14 +03:00
|
|
|
#ifdef GPU
|
2016-09-25 09:12:54 +03:00
|
|
|
l.forward_gpu = forward_activation_layer_gpu;
|
|
|
|
l.backward_gpu = backward_activation_layer_gpu;
|
|
|
|
|
2016-01-19 02:40:14 +03:00
|
|
|
l.output_gpu = cuda_make_array(l.output, inputs*batch);
|
|
|
|
l.delta_gpu = cuda_make_array(l.delta, inputs*batch);
|
|
|
|
#endif
|
|
|
|
l.activation = activation;
|
|
|
|
fprintf(stderr, "Activation Layer: %d inputs\n", inputs);
|
|
|
|
return l;
|
|
|
|
}
|
|
|
|
|
2017-04-10 05:56:42 +03:00
|
|
|
void forward_activation_layer(layer l, network net)
|
2016-01-19 02:40:14 +03:00
|
|
|
{
|
2017-04-10 05:56:42 +03:00
|
|
|
copy_cpu(l.outputs*l.batch, net.input, 1, l.output, 1);
|
2016-01-19 02:40:14 +03:00
|
|
|
activate_array(l.output, l.outputs*l.batch, l.activation);
|
|
|
|
}
|
|
|
|
|
2017-04-10 05:56:42 +03:00
|
|
|
void backward_activation_layer(layer l, network net)
|
2016-01-19 02:40:14 +03:00
|
|
|
{
|
|
|
|
gradient_array(l.output, l.outputs*l.batch, l.activation, l.delta);
|
2017-04-10 05:56:42 +03:00
|
|
|
copy_cpu(l.outputs*l.batch, l.delta, 1, net.delta, 1);
|
2016-01-19 02:40:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef GPU
|
|
|
|
|
2017-04-10 05:56:42 +03:00
|
|
|
void forward_activation_layer_gpu(layer l, network net)
|
2016-01-19 02:40:14 +03:00
|
|
|
{
|
2017-06-18 23:05:37 +03:00
|
|
|
copy_gpu(l.outputs*l.batch, net.input_gpu, 1, l.output_gpu, 1);
|
|
|
|
activate_array_gpu(l.output_gpu, l.outputs*l.batch, l.activation);
|
2016-01-19 02:40:14 +03:00
|
|
|
}
|
|
|
|
|
2017-04-10 05:56:42 +03:00
|
|
|
void backward_activation_layer_gpu(layer l, network net)
|
2016-01-19 02:40:14 +03:00
|
|
|
{
|
2017-06-18 23:05:37 +03:00
|
|
|
gradient_array_gpu(l.output_gpu, l.outputs*l.batch, l.activation, l.delta_gpu);
|
|
|
|
copy_gpu(l.outputs*l.batch, l.delta_gpu, 1, net.delta_gpu, 1);
|
2016-01-19 02:40:14 +03:00
|
|
|
}
|
|
|
|
#endif
|