Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Abs.c « generic - github.com/clementfarabet/lua---nnx.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 8c65813912ddebe65d819069a2efebe8948ffddb (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
39
40
41
42
43
#ifndef TH_GENERIC_FILE
#define TH_GENERIC_FILE "generic/Abs.c"
#else

static int nn_(Abs_updateOutput)(lua_State *L)
{
  THTensor *input = luaT_checkudata(L, 2, torch_(Tensor_id));
  THTensor *output = luaT_getfieldcheckudata(L, 1, "output", torch_(Tensor_id));

  THTensor_(resizeAs)(output, input);

  TH_TENSOR_APPLY2(real, output, real, input, \
                   *output_data = fabs(*input_data);)
  return 1;
}

static int nn_(Abs_updateGradInput)(lua_State *L)
{
  THTensor *input = luaT_checkudata(L, 2, torch_(Tensor_id));
  THTensor *gradOutput = luaT_checkudata(L, 3, torch_(Tensor_id));
  THTensor *gradInput = luaT_getfieldcheckudata(L, 1, "gradInput", torch_(Tensor_id));

  THTensor_(resizeAs)(gradInput, input);
  TH_TENSOR_APPLY3(real, gradInput, real, gradOutput, real, input, \
                   real z = *input_data;                              \
                   *gradInput_data = *gradOutput_data * (z >= 0 ? 1 : -1);)
  return 1;
}

static const struct luaL_Reg nn_(Abs__) [] = {
  {"Abs_updateOutput", nn_(Abs_updateOutput)},
  {"Abs_updateGradInput", nn_(Abs_updateGradInput)},
  {NULL, NULL}
};

static void nn_(Abs_init)(lua_State *L)
{
  luaT_pushmetaclass(L, torch_(Tensor_id));
  luaT_registeratname(L, nn_(Abs__), "nn");
  lua_pop(L,1);
}

#endif