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
37
38
39
40
41
42
43
44
|
#ifndef TH_GENERIC_FILE
#define TH_GENERIC_FILE "generic/DistKLDivCriterion.c"
#else
void THNN_(DistKLDivCriterion_updateOutput)(
THNNState *state,
THTensor *input,
THTensor *target,
THTensor *output,
bool sizeAverage)
{
THNN_CHECK_NELEMENT(input, target);
THNN_CHECK_DIM_SIZE(output, 1, 0, 1);
real sum = 0;
TH_TENSOR_APPLY2(real, input, real, target,
sum += *target_data > 0 ? *target_data * (log(*target_data) - *input_data) : 0;
);
if (sizeAverage)
sum /= THTensor_(nElement)(input);
THTensor_(set1d)(output, 0, sum);
}
void THNN_(DistKLDivCriterion_updateGradInput)(
THNNState *state,
THTensor *input,
THTensor *target,
THTensor *gradInput,
bool sizeAverage)
{
THNN_CHECK_NELEMENT(input, target);
real norm = (sizeAverage ? 1./((real)THTensor_(nElement)(input)) : 1.);
THTensor_(resizeAs)(gradInput, input);
TH_TENSOR_APPLY3(real, gradInput, real, input, real, target,
*gradInput_data = *target_data > 0 ? norm * (-*target_data) : 0;
);
}
#endif
|