2014-10-13 11:29:01 +04:00
|
|
|
#include "cost_layer.h"
|
2014-11-19 00:51:04 +03:00
|
|
|
#include "utils.h"
|
2015-01-23 03:38:24 +03:00
|
|
|
#include "cuda.h"
|
|
|
|
#include "blas.h"
|
2014-10-13 11:29:01 +04:00
|
|
|
#include <math.h>
|
2014-11-28 21:38:26 +03:00
|
|
|
#include <string.h>
|
2014-10-13 11:29:01 +04:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
2014-11-28 21:38:26 +03:00
|
|
|
COST_TYPE get_cost_type(char *s)
|
|
|
|
{
|
|
|
|
if (strcmp(s, "sse")==0) return SSE;
|
2015-05-07 00:08:16 +03:00
|
|
|
if (strcmp(s, "masked")==0) return MASKED;
|
2014-11-28 21:38:26 +03:00
|
|
|
fprintf(stderr, "Couldn't find activation function %s, going with SSE\n", s);
|
|
|
|
return SSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *get_cost_string(COST_TYPE a)
|
|
|
|
{
|
|
|
|
switch(a){
|
|
|
|
case SSE:
|
|
|
|
return "sse";
|
2015-05-07 00:08:16 +03:00
|
|
|
case MASKED:
|
|
|
|
return "masked";
|
2014-11-28 21:38:26 +03:00
|
|
|
}
|
|
|
|
return "sse";
|
|
|
|
}
|
|
|
|
|
|
|
|
cost_layer *make_cost_layer(int batch, int inputs, COST_TYPE type)
|
2014-10-13 11:29:01 +04:00
|
|
|
{
|
|
|
|
fprintf(stderr, "Cost Layer: %d inputs\n", inputs);
|
|
|
|
cost_layer *layer = calloc(1, sizeof(cost_layer));
|
|
|
|
layer->batch = batch;
|
|
|
|
layer->inputs = inputs;
|
2014-11-28 21:38:26 +03:00
|
|
|
layer->type = type;
|
2014-10-13 11:29:01 +04:00
|
|
|
layer->delta = calloc(inputs*batch, sizeof(float));
|
|
|
|
layer->output = calloc(1, sizeof(float));
|
|
|
|
#ifdef GPU
|
2015-01-23 03:38:24 +03:00
|
|
|
layer->delta_gpu = cuda_make_array(layer->delta, inputs*batch);
|
2014-10-13 11:29:01 +04:00
|
|
|
#endif
|
|
|
|
return layer;
|
|
|
|
}
|
|
|
|
|
2015-03-12 08:20:15 +03:00
|
|
|
void forward_cost_layer(cost_layer layer, network_state state)
|
2014-10-13 11:29:01 +04:00
|
|
|
{
|
2015-03-12 08:20:15 +03:00
|
|
|
if (!state.truth) return;
|
2015-05-07 00:08:16 +03:00
|
|
|
if(layer.type == MASKED){
|
|
|
|
int i;
|
|
|
|
for(i = 0; i < layer.batch*layer.inputs; ++i){
|
|
|
|
if(state.truth[i] == 0) state.input[i] = 0;
|
|
|
|
}
|
|
|
|
}
|
2015-03-12 08:20:15 +03:00
|
|
|
copy_cpu(layer.batch*layer.inputs, state.truth, 1, layer.delta, 1);
|
|
|
|
axpy_cpu(layer.batch*layer.inputs, -1, state.input, 1, layer.delta, 1);
|
2014-10-13 11:29:01 +04:00
|
|
|
*(layer.output) = dot_cpu(layer.batch*layer.inputs, layer.delta, 1, layer.delta, 1);
|
2014-12-04 10:20:29 +03:00
|
|
|
//printf("cost: %f\n", *layer.output);
|
2014-10-13 11:29:01 +04:00
|
|
|
}
|
|
|
|
|
2015-03-12 08:20:15 +03:00
|
|
|
void backward_cost_layer(const cost_layer layer, network_state state)
|
2014-10-13 11:29:01 +04:00
|
|
|
{
|
2015-03-12 08:20:15 +03:00
|
|
|
copy_cpu(layer.batch*layer.inputs, layer.delta, 1, state.delta, 1);
|
2014-10-13 11:29:01 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef GPU
|
2014-11-28 21:38:26 +03:00
|
|
|
|
2015-04-15 11:04:38 +03:00
|
|
|
void pull_cost_layer(cost_layer layer)
|
|
|
|
{
|
|
|
|
cuda_pull_array(layer.delta_gpu, layer.delta, layer.batch*layer.inputs);
|
|
|
|
}
|
|
|
|
|
|
|
|
void push_cost_layer(cost_layer layer)
|
|
|
|
{
|
|
|
|
cuda_push_array(layer.delta_gpu, layer.delta, layer.batch*layer.inputs);
|
|
|
|
}
|
|
|
|
|
2015-03-12 08:20:15 +03:00
|
|
|
void forward_cost_layer_gpu(cost_layer layer, network_state state)
|
2014-10-13 11:29:01 +04:00
|
|
|
{
|
2015-03-12 08:20:15 +03:00
|
|
|
if (!state.truth) return;
|
2015-05-07 00:08:16 +03:00
|
|
|
if (layer.type == MASKED) {
|
|
|
|
mask_ongpu(layer.batch*layer.inputs, state.input, state.truth);
|
|
|
|
}
|
2015-03-05 01:56:38 +03:00
|
|
|
|
2015-03-12 08:20:15 +03:00
|
|
|
copy_ongpu(layer.batch*layer.inputs, state.truth, 1, layer.delta_gpu, 1);
|
|
|
|
axpy_ongpu(layer.batch*layer.inputs, -1, state.input, 1, layer.delta_gpu, 1);
|
2014-11-19 00:51:04 +03:00
|
|
|
|
2015-01-23 03:38:24 +03:00
|
|
|
cuda_pull_array(layer.delta_gpu, layer.delta, layer.batch*layer.inputs);
|
2014-10-13 11:29:01 +04:00
|
|
|
*(layer.output) = dot_cpu(layer.batch*layer.inputs, layer.delta, 1, layer.delta, 1);
|
|
|
|
}
|
|
|
|
|
2015-03-12 08:20:15 +03:00
|
|
|
void backward_cost_layer_gpu(const cost_layer layer, network_state state)
|
2014-10-13 11:29:01 +04:00
|
|
|
{
|
2015-03-12 08:20:15 +03:00
|
|
|
copy_ongpu(layer.batch*layer.inputs, layer.delta_gpu, 1, state.delta, 1);
|
2014-10-13 11:29:01 +04:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|