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";
|
|
|
|
}
|
|
|
|
|
2015-05-11 23:46:49 +03:00
|
|
|
cost_layer make_cost_layer(int batch, int inputs, COST_TYPE cost_type)
|
2014-10-13 11:29:01 +04:00
|
|
|
{
|
|
|
|
fprintf(stderr, "Cost Layer: %d inputs\n", inputs);
|
2015-05-11 23:46:49 +03:00
|
|
|
cost_layer l = {0};
|
|
|
|
l.type = COST;
|
|
|
|
|
|
|
|
l.batch = batch;
|
|
|
|
l.inputs = inputs;
|
|
|
|
l.outputs = inputs;
|
|
|
|
l.cost_type = cost_type;
|
|
|
|
l.delta = calloc(inputs*batch, sizeof(float));
|
|
|
|
l.output = calloc(1, sizeof(float));
|
2014-10-13 11:29:01 +04:00
|
|
|
#ifdef GPU
|
2015-05-11 23:46:49 +03:00
|
|
|
l.delta_gpu = cuda_make_array(l.delta, inputs*batch);
|
2014-10-13 11:29:01 +04:00
|
|
|
#endif
|
2015-05-11 23:46:49 +03:00
|
|
|
return l;
|
2014-10-13 11:29:01 +04:00
|
|
|
}
|
|
|
|
|
2015-05-11 23:46:49 +03:00
|
|
|
void forward_cost_layer(cost_layer l, 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-11 23:46:49 +03:00
|
|
|
if(l.cost_type == MASKED){
|
2015-05-07 00:08:16 +03:00
|
|
|
int i;
|
2015-05-11 23:46:49 +03:00
|
|
|
for(i = 0; i < l.batch*l.inputs; ++i){
|
2015-05-07 00:08:16 +03:00
|
|
|
if(state.truth[i] == 0) state.input[i] = 0;
|
|
|
|
}
|
|
|
|
}
|
2015-05-11 23:46:49 +03:00
|
|
|
copy_cpu(l.batch*l.inputs, state.truth, 1, l.delta, 1);
|
|
|
|
axpy_cpu(l.batch*l.inputs, -1, state.input, 1, l.delta, 1);
|
|
|
|
*(l.output) = dot_cpu(l.batch*l.inputs, l.delta, 1, l.delta, 1);
|
|
|
|
//printf("cost: %f\n", *l.output);
|
2014-10-13 11:29:01 +04:00
|
|
|
}
|
|
|
|
|
2015-05-11 23:46:49 +03:00
|
|
|
void backward_cost_layer(const cost_layer l, network_state state)
|
2014-10-13 11:29:01 +04:00
|
|
|
{
|
2015-05-11 23:46:49 +03:00
|
|
|
copy_cpu(l.batch*l.inputs, l.delta, 1, state.delta, 1);
|
2014-10-13 11:29:01 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef GPU
|
2014-11-28 21:38:26 +03:00
|
|
|
|
2015-05-11 23:46:49 +03:00
|
|
|
void pull_cost_layer(cost_layer l)
|
2015-04-15 11:04:38 +03:00
|
|
|
{
|
2015-05-11 23:46:49 +03:00
|
|
|
cuda_pull_array(l.delta_gpu, l.delta, l.batch*l.inputs);
|
2015-04-15 11:04:38 +03:00
|
|
|
}
|
|
|
|
|
2015-05-11 23:46:49 +03:00
|
|
|
void push_cost_layer(cost_layer l)
|
2015-04-15 11:04:38 +03:00
|
|
|
{
|
2015-05-11 23:46:49 +03:00
|
|
|
cuda_push_array(l.delta_gpu, l.delta, l.batch*l.inputs);
|
2015-04-15 11:04:38 +03:00
|
|
|
}
|
|
|
|
|
2015-05-11 23:46:49 +03:00
|
|
|
void forward_cost_layer_gpu(cost_layer l, 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-11 23:46:49 +03:00
|
|
|
if (l.cost_type == MASKED) {
|
|
|
|
mask_ongpu(l.batch*l.inputs, state.input, state.truth);
|
2015-05-07 00:08:16 +03:00
|
|
|
}
|
2015-03-05 01:56:38 +03:00
|
|
|
|
2015-05-11 23:46:49 +03:00
|
|
|
copy_ongpu(l.batch*l.inputs, state.truth, 1, l.delta_gpu, 1);
|
|
|
|
axpy_ongpu(l.batch*l.inputs, -1, state.input, 1, l.delta_gpu, 1);
|
2014-11-19 00:51:04 +03:00
|
|
|
|
2015-05-11 23:46:49 +03:00
|
|
|
cuda_pull_array(l.delta_gpu, l.delta, l.batch*l.inputs);
|
|
|
|
*(l.output) = dot_cpu(l.batch*l.inputs, l.delta, 1, l.delta, 1);
|
2014-10-13 11:29:01 +04:00
|
|
|
}
|
|
|
|
|
2015-05-11 23:46:49 +03:00
|
|
|
void backward_cost_layer_gpu(const cost_layer l, network_state state)
|
2014-10-13 11:29:01 +04:00
|
|
|
{
|
2015-05-11 23:46:49 +03:00
|
|
|
copy_ongpu(l.batch*l.inputs, l.delta_gpu, 1, state.delta, 1);
|
2014-10-13 11:29:01 +04:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|