Joseph Redmon
2014-01-28 b2b7137b6f185ce2f01664d782a09b08d50d5a07
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include "softmax_layer.h"
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
 
softmax_layer *make_softmax_layer(int inputs)
{
    fprintf(stderr, "Softmax Layer: %d inputs\n", inputs);
    softmax_layer *layer = calloc(1, sizeof(softmax_layer));
    layer->inputs = inputs;
    layer->output = calloc(inputs, sizeof(double));
    layer->delta = calloc(inputs, sizeof(double));
    return layer;
}
 
void forward_softmax_layer(const softmax_layer layer, double *input)
{
    int i;
    double sum = 0;
    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;
    }
}
 
void backward_softmax_layer(const softmax_layer layer, double *input, double *delta)
{
    int i;
    for(i = 0; i < layer.inputs; ++i){
        delta[i] = layer.delta[i];
    }
}