-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCCrossEntropyLayer.cpp
36 lines (30 loc) · 1011 Bytes
/
CCrossEntropyLayer.cpp
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
35
36
//
// Created by sonu
//
#include "common.h"
#include "CinputOutput.h"
#include "CCrossEntropyLayer.h"
template<size_t N>
void CCrossEntropyLayer<N>::train(const int label, const double mb_size) {
// Note that there is no actual need to calculate the loss at this point.
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-variable"
double loss = -log(this->previous_layer->output[0][0][label]);
#pragma GCC diagnostic pop
this->downstream_deriv = 0;
this->downstream_deriv[0][0][label] = -1/(this->previous_layer->output[0][0][label]);
this->previous_layer->backprop(this->downstream_deriv, mb_size);
}
template<size_t N>
void CCrossEntropyLayer<N>::update_weights(const double) {
// No weights in this layer, and this layer has no output.
}
template<size_t N>
double CCrossEntropyLayer<N>::loss( Input &in, int label) {
return -std::log(in[0][0][label]);
}
template<size_t N>
int CCrossEntropyLayer<N>::predict(Input &) {
assert(false);
return -1;
}