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

Min.c « generic - github.com/torch/nn.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: d3309df697f42eab8d3774279bea7495b4d1d43b (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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#ifndef TH_GENERIC_FILE
#define TH_GENERIC_FILE "generic/Min.c"
#else

static int nn_(Min_updateOutput)(lua_State *L)
{
  THTensor *input = luaT_checkudata(L, 2, torch_(Tensor_id));
  int dimension = luaT_getfieldcheckint(L, 1, "dimension")-1;
  THTensor *indices = luaT_getfieldcheckudata(L, 1, "indices", torch_(Tensor_id));
  THTensor *output = luaT_getfieldcheckudata(L, 1, "output", torch_(Tensor_id));

  THLongStorage *dim;
  long i;

  luaL_argcheck(L, dimension >= 0 && dimension < input->nDimension, 2, "dimension out of range");

  dim = THLongStorage_newWithSize(input->nDimension);
  for(i = 0; i < input->nDimension; i++)
    dim->data[i] = input->size[i];
  dim->data[dimension] = 1;
  THTensor_(resize)(output, dim, NULL);
  THTensor_(resize)(indices, dim, NULL);
  THLongStorage_free(dim);

  TH_TENSOR_DIM_APPLY3(real, output, real, input, real, indices, dimension,
                       long theIndex = 0;
                       real theMin = input_data[0];
                       for(i = 1; i < input_size; i++)
                       {
                         if(input_data[i*input_stride] < theMin)
                         {
                           theIndex = i;
                           theMin = input_data[i*input_stride];
                         }
                       }
                       *indices_data = theIndex+1;
                       *output_data = theMin;)

  THTensor_(select)(output, NULL, dimension, 0);

  return 1;
}

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

  THTensor *gradOutputPlusOneDim;
  THLongStorage *dim, *str;
  int i, j;

  THTensor_(resizeAs)(gradInput, input);
  THTensor_(zero)(gradInput);

  dim = THLongStorage_newWithSize(gradOutput->nDimension+1);
  str = THLongStorage_newWithSize(gradOutput->nDimension+1);
  for(i = 0, j =  0; j < gradOutput->nDimension+1; j++)
  {
    if(j == dimension)
    {
      dim->data[j] = input->size[dimension];
      str->data[j] = 0;
      continue;
    }

    dim->data[j] = gradOutput->size[i];
    str->data[j] = gradOutput->stride[i];
    i++;
  }

  gradOutputPlusOneDim = THTensor_(newWithStorage)(gradOutput->storage, gradOutput->storageOffset, dim, str);
  THLongStorage_free(dim);
  THLongStorage_free(str);

  TH_TENSOR_DIM_APPLY3(real, gradInput, real, gradOutputPlusOneDim, real, indices, dimension,
                       gradInput_data[ ((long)(*indices_data)-1)*gradInput_stride ] = *gradOutputPlusOneDim_data;)

  THTensor_(free)(gradOutputPlusOneDim);

  return 1;
}

static const struct luaL_Reg nn_(Min__) [] = {
  {"Min_updateOutput", nn_(Min_updateOutput)},
  {"Min_updateGradInput", nn_(Min_updateGradInput)},
  {NULL, NULL}
};

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

#endif