2013-12-03 04:41:40 +04:00
|
|
|
#include "softmax_layer.h"
|
2014-08-08 23:04:15 +04:00
|
|
|
#include "mini_blas.h"
|
2013-12-03 04:41:40 +04:00
|
|
|
#include <math.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
2014-03-13 08:57:34 +04:00
|
|
|
softmax_layer *make_softmax_layer(int batch, int inputs)
|
2013-12-03 04:41:40 +04:00
|
|
|
{
|
2013-12-06 01:17:16 +04:00
|
|
|
fprintf(stderr, "Softmax Layer: %d inputs\n", inputs);
|
2013-12-03 04:41:40 +04:00
|
|
|
softmax_layer *layer = calloc(1, sizeof(softmax_layer));
|
2014-03-13 08:57:34 +04:00
|
|
|
layer->batch = batch;
|
2013-12-03 04:41:40 +04:00
|
|
|
layer->inputs = inputs;
|
2014-03-13 08:57:34 +04:00
|
|
|
layer->output = calloc(inputs*batch, sizeof(float));
|
|
|
|
layer->delta = calloc(inputs*batch, sizeof(float));
|
2014-08-08 23:04:15 +04:00
|
|
|
layer->jacobian = calloc(inputs*inputs*batch, sizeof(float));
|
2013-12-03 04:41:40 +04:00
|
|
|
return layer;
|
|
|
|
}
|
|
|
|
|
2014-01-29 04:28:42 +04:00
|
|
|
/* UNSTABLE!
|
|
|
|
void forward_softmax_layer(const softmax_layer layer, float *input)
|
2013-12-03 04:41:40 +04:00
|
|
|
{
|
|
|
|
int i;
|
2014-01-29 04:28:42 +04:00
|
|
|
float sum = 0;
|
2013-12-03 04:41:40 +04:00
|
|
|
for(i = 0; i < layer.inputs; ++i){
|
|
|
|
sum += exp(input[i]);
|
|
|
|
}
|
|
|
|
for(i = 0; i < layer.inputs; ++i){
|
|
|
|
layer.output[i] = exp(input[i])/sum;
|
|
|
|
}
|
|
|
|
}
|
2014-01-29 04:28:42 +04:00
|
|
|
*/
|
|
|
|
void forward_softmax_layer(const softmax_layer layer, float *input)
|
|
|
|
{
|
2014-03-13 08:57:34 +04:00
|
|
|
int i,b;
|
|
|
|
for(b = 0; b < layer.batch; ++b){
|
|
|
|
float sum = 0;
|
|
|
|
float largest = 0;
|
|
|
|
for(i = 0; i < layer.inputs; ++i){
|
|
|
|
if(input[i+b*layer.inputs] > largest) largest = input[i+b*layer.inputs];
|
|
|
|
}
|
|
|
|
for(i = 0; i < layer.inputs; ++i){
|
|
|
|
sum += exp(input[i+b*layer.inputs]-largest);
|
|
|
|
//printf("%f, ", input[i]);
|
|
|
|
}
|
|
|
|
//printf("\n");
|
|
|
|
if(sum) sum = largest+log(sum);
|
|
|
|
else sum = largest-100;
|
|
|
|
for(i = 0; i < layer.inputs; ++i){
|
|
|
|
layer.output[i+b*layer.inputs] = exp(input[i+b*layer.inputs]-sum);
|
|
|
|
}
|
2014-01-29 04:28:42 +04:00
|
|
|
}
|
|
|
|
}
|
2013-12-03 04:41:40 +04:00
|
|
|
|
2014-01-29 04:28:42 +04:00
|
|
|
void backward_softmax_layer(const softmax_layer layer, float *input, float *delta)
|
2013-12-03 04:41:40 +04:00
|
|
|
{
|
2014-08-08 23:04:15 +04:00
|
|
|
/*
|
|
|
|
int i,j,b;
|
|
|
|
for(b = 0; b < layer.batch; ++b){
|
|
|
|
for(i = 0; i < layer.inputs; ++i){
|
|
|
|
for(j = 0; j < layer.inputs; ++j){
|
|
|
|
int d = (i==j);
|
|
|
|
layer.jacobian[b*layer.inputs*layer.inputs + i*layer.inputs + j] =
|
|
|
|
layer.output[b*layer.inputs + i] * (d - layer.output[b*layer.inputs + j]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for(b = 0; b < layer.batch; ++b){
|
|
|
|
int M = layer.inputs;
|
|
|
|
int N = 1;
|
|
|
|
int K = layer.inputs;
|
|
|
|
float *A = layer.jacobian + b*layer.inputs*layer.inputs;
|
|
|
|
float *B = layer.delta + b*layer.inputs;
|
|
|
|
float *C = delta + b*layer.inputs;
|
|
|
|
gemm(0,0,M,N,K,1,A,K,B,N,0,C,N);
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
2013-12-03 04:41:40 +04:00
|
|
|
int i;
|
2014-03-13 08:57:34 +04:00
|
|
|
for(i = 0; i < layer.inputs*layer.batch; ++i){
|
2013-12-03 04:41:40 +04:00
|
|
|
delta[i] = layer.delta[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|