summaryrefslogtreecommitdiffstats
path: root/contrib/lua-torch/nn/lib/THNN/generic/L1Cost.c
blob: 53940e89479042864abef2ad58df077981f026c9 (plain)
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
#ifndef TH_GENERIC_FILE
#define TH_GENERIC_FILE "generic/L1Cost.c"
#else

void THNN_(L1Cost_updateOutput)(
          THNNState *state,
          THTensor *input,
          THTensor *output)
{
  THNN_CHECK_DIM_SIZE(output, 1, 0, 1);
  accreal sum = 0;

  TH_TENSOR_APPLY(real, input,
    sum += fabs(*input_data);
  );

  THTensor_(set1d)(output, 0, sum);
}

void THNN_(L1Cost_updateGradInput)(
          THNNState *state,
          THTensor *input,
          THTensor *gradOutput,
          THTensor *gradInput)
{
  THNN_CHECK_NELEMENT(input, gradOutput);
  THTensor_(resizeAs)(gradInput, input);
  TH_TENSOR_APPLY2(real, gradInput, real, input,
    if (*input_data > 0)
      *gradInput_data = 1;
    else if (*input_data < 0)
      *gradInput_data = -1;
    else
      *gradInput_data = 0;
  );
}

#endif