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

SparseLinear.c « generic - github.com/torch/nn.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: a7d0e3601e876e2e556a7c0ca74ef770838081a0 (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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#ifndef TH_GENERIC_FILE
#define TH_GENERIC_FILE "generic/SparseLinear.c"
#else

static int nn_(SparseLinear_updateOutput)(lua_State *L)
{
  long i;
  THTensor * input = luaT_checkudata(L, 2, torch_Tensor);
  THTensor * weight = luaT_getfieldcheckudata(L, 1, "weight", torch_Tensor);
  THTensor * bias = luaT_getfieldcheckudata(L, 1, "bias", torch_Tensor);
  THTensor * output = luaT_getfieldcheckudata(L, 1, "output", torch_Tensor);
  long dim = weight->size[1]; /* number of weights.. */

  THTensor_(copy)(output, bias);
  for(i = 0; i < input->size[0]; i++)
  {
    long offset = (long)(THTensor_(get2d)(input, i, 0)) - 1;
    if(offset >= 0 && offset < dim) /* make sure indices are in bounds.. */
    {
        real val = THTensor_(get2d)(input, i, 1);
        THBlas_(axpy)(output->size[0], 
                      val, 
                      THTensor_(data)(weight)+offset*weight->stride[1],
                      weight->stride[0], 
                      THTensor_(data)(output), 
                      output->stride[0]);
    }
    else {
        printf("\nOutput: %d not between 0 and %d\n", offset, dim-1);
        luaL_error(L, "index out of bound");
    }
  }
  return 1;
}

static int nn_(SparseLinear_accGradParameters)(lua_State *L)
{
  long i;
  THTensor * input = luaT_checkudata(L, 2, torch_Tensor);
  THTensor * gradOutput = luaT_checkudata(L, 3, torch_Tensor);
  real scale = luaL_optnumber(L, 4, 1);
  THTensor * weight = luaT_getfieldcheckudata(L, 1, "weight", torch_Tensor);
  THTensor * gradBias = luaT_getfieldcheckudata(L, 1, "gradBias", torch_Tensor);
  THTensor * gradWeight = luaT_getfieldcheckudata(L, 1, "gradWeight", torch_Tensor);
  THTensor * lastInput = luaT_getfieldcheckudata(L, 1, "lastInput", torch_Tensor);
  real weightDecay = luaT_getfieldchecknumber(L, 1, "weightDecay");
  long dim = gradWeight->size[1]; /* number of weights.. */

  for(i = 0; i < input->size[0]; i++)
  {
      long offset = (long)(THTensor_(get2d)(input, i, 0)) - 1;

      if(offset >= 0 && offset < dim) /* make sure indices are in bounds.. */
      {
          real val = scale*THTensor_(get2d)(input, i, 1);
          THBlas_(scal)(gradOutput->size[0],
                        0, 
                        THTensor_(data)(gradWeight)+offset*gradWeight->stride[1],
                        gradWeight->stride[0]); /* zero */
          
          THBlas_(axpy)(gradOutput->size[0], 
                        val, 
                        THTensor_(data)(gradOutput), 
                        gradOutput->stride[0], 
                        THTensor_(data)(gradWeight)+offset*gradWeight->stride[1], 
                        gradWeight->stride[0]);
      }
      else {
          printf("\nAccG: %d not between 0 and %d\n", offset, dim-1);
          luaL_error(L, "index out of bound");
      }
  }
  
  THTensor_(cadd)(gradBias, gradBias, 1, gradOutput); 
  
  if(weightDecay != 0)
    THTensor_(cadd)(gradWeight, gradWeight, weightDecay, weight);
  
  THTensor_(resizeAs)(lastInput, input);
  THTensor_(copy)(lastInput, input);
  
  return 0;
}

int nn_(SparseLinear_updateParameters)(lua_State *L)
{
  long i;
  real learningRate = luaL_checknumber(L, 2);
  THTensor * weight = luaT_getfieldcheckudata(L, 1, "weight", torch_Tensor);
  THTensor * bias = luaT_getfieldcheckudata(L, 1, "bias", torch_Tensor);
  THTensor * gradBias = luaT_getfieldcheckudata(L, 1, "gradBias", torch_Tensor);
  THTensor * gradWeight = luaT_getfieldcheckudata(L, 1, "gradWeight", torch_Tensor);
  THTensor * lastInput = luaT_getfieldcheckudata(L, 1, "lastInput", torch_Tensor);
  
  long dim = weight->size[1]; /* number of weights.. */
  THTensor_(cadd)(bias, bias, -learningRate, gradBias);
  
  for(i = 0; i < lastInput->size[0]; i++) 
  {
      long offset = (long)(THTensor_(get2d)(lastInput, i, 0)) - 1;
      
      if(offset >= 0 && offset < dim) /* make sure indices are in bounds.. */
      {
          THBlas_(axpy)(bias->size[0], 
                        -learningRate, 
                        THTensor_(data)(gradWeight)+offset*gradWeight->stride[1], 
                        gradWeight->stride[0], 
                        THTensor_(data)(weight)+offset*weight->stride[1], 
                        weight->stride[0]);
      }
      else {
          printf("\nUpdateP: %d not between 0 and %d\n", offset, dim-1);
          luaL_error(L, "index out of bound");
      }
  }
  return 0;
}

static const struct luaL_Reg nn_(SparseLinear__) [] = {
  {"SparseLinear_updateOutput", nn_(SparseLinear_updateOutput)},
  {"SparseLinear_accGradParameters", nn_(SparseLinear_accGradParameters)},
  {"SparseLinear_updateParameters", nn_(SparseLinear_updateParameters)},
  {NULL, NULL}
};

void nn_(SparseLinear_init)(lua_State *L)
{
  luaT_pushmetatable(L, torch_Tensor);
  luaT_registeratname(L, nn_(SparseLinear__), "nn");
  lua_pop(L,1);
}

#endif