summaryrefslogtreecommitdiff
path: root/src/TNetLib/.svn/text-base/SharedLinearity.cc.svn-base
blob: 108212c593f3f0031f3647162a8241e8b60f3e74 (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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277


#include "SharedLinearity.h"
#include "cblas.h"

namespace TNet {

void 
SharedLinearity::
PropagateFnc(const Matrix<BaseFloat>& X, Matrix<BaseFloat>& Y)
{
  //precopy bias
  for(int k=0; k<mNInstances; k++) {
    for(size_t r=0; r<X.Rows(); r++) {
      memcpy(Y.pRowData(r)+k*mpBias->Dim(),mpBias->pData(),mpBias->Dim()*sizeof(BaseFloat));
    }
  }
  
  //multiply blockwise
  for(int k=0; k<mNInstances; k++) {
    SubMatrix<BaseFloat> xblock(X,0,X.Rows(),k*mpLinearity->Rows(),mpLinearity->Rows());
    SubMatrix<BaseFloat> yblock(Y,0,Y.Rows(),k*mpLinearity->Cols(),mpLinearity->Cols());
    yblock.BlasGemm(1.0,xblock,NO_TRANS,*mpLinearity,NO_TRANS,1.0);
  }
}


void 
SharedLinearity::
BackpropagateFnc(const Matrix<BaseFloat>& X, Matrix<BaseFloat>& Y)
{
  for(int k=0; k<mNInstances; k++) {
    SubMatrix<BaseFloat> xblock(X,0,X.Rows(),k*mpLinearity->Cols(),mpLinearity->Cols());
    SubMatrix<BaseFloat> yblock(Y,0,Y.Rows(),k*mpLinearity->Rows(),mpLinearity->Rows());
    yblock.BlasGemm(1.0,xblock,NO_TRANS,*mpLinearity,TRANS,1.0);
  }
}

#if 0
void 
SharedLinearity::
AccuUpdate() 
{
  BaseFloat N = 1;
  /* 
  //Not part of the interface!!!
  if(mGradDivFrm) {
    N = static_cast<BaseFloat>(GetInput().Rows());
  }
  */
  BaseFloat mmt_gain = static_cast<BaseFloat>(1.0/(1.0-mMomentum));
  N *= mmt_gain; //compensate higher gradient estimates due to momentum 
  
  //compensate augmented dyn. range of gradient caused by multiple instances
  N *= static_cast<BaseFloat>(mNInstances); 

  const Matrix<BaseFloat>& X = GetInput().Data();
  const Matrix<BaseFloat>& E = GetErrorInput().Data();
  //get gradient of shared linearity
  for(int k=0; k<mNInstances; k++) {
    SubMatrix<BaseFloat> xblock(X,0,X.Rows(),k*mLinearity.Rows(),mLinearity.Rows());
    SubMatrix<BaseFloat> eblock(E,0,E.Rows(),k*mLinearity.Cols(),mLinearity.Cols());
    mLinearityCorrection.BlasGemm(1.0,xblock,TRANS,eblock,NO_TRANS,((k==0)?mMomentum:1.0f));
  }

  //get gradient of shared bias
  mBiasCorrection.Scale(mMomentum);
  for(int r=0; r<E.Rows(); r++) {
    for(int c=0; c<E.Cols(); c++) {
      mBiasCorrection[c%mBiasCorrection.Dim()] += E(r,c);
    }
  }

  //perform update 
  mLinearity.AddScaled(-mLearningRate/N,mLinearityCorrection);
  mBias.AddScaled(-mLearningRate/N,mBiasCorrection);
  
  //regularization weight decay
  mLinearity.AddScaled(-mLearningRate*mWeightcost,mLinearity);
}
#endif

void
SharedLinearity::
ReadFromStream(std::istream& rIn)
{
  //number of instances of shared weights in layer
  rIn >> std::ws >> mNInstances;
  if(mNInstances < 1) {
    std::ostringstream os;
    os << "Bad number of instances:" << mNInstances;
    Error(os.str());
  }
  if(GetNInputs() % mNInstances != 0 || GetNOutputs() % mNInstances != 0) {
    std::ostringstream os;
    os << "Number of Inputs/Outputs must be divisible by number of instances"
       << " Inputs:" << GetNInputs()
       << " Outputs" << GetNOutputs()
       << " Intances:" << mNInstances;
    Error(os.str());
  }
    
  //matrix is stored transposed as SNet does
  BfMatrix transpose;
  rIn >> transpose;
  mLinearity = BfMatrix(transpose, TRANS);
  //biases stored normally
  rIn >> mBias;

  if(transpose.Cols()*transpose.Rows() == 0) {
    Error("Missing linearity matrix in network file");
  }
  if(mBias.Dim() == 0) {
    Error("Missing bias vector in network file");
  }


  if(mLinearity.Cols() != (GetNOutputs() / mNInstances) || 
     mLinearity.Rows() != (GetNInputs() / mNInstances) ||
     mBias.Dim() != (GetNOutputs() / mNInstances)
  ){
    std::ostringstream os;
    os << "Wrong dimensionalities of matrix/vector in network file\n"
       << "Inputs:" << GetNInputs()
       << " Outputs:" << GetNOutputs()
       << "\n"
       << "N-Instances:" << mNInstances
       << "\n"
       << "linearityCols:" << mLinearity.Cols() << "(" << mLinearity.Cols()*mNInstances << ")"
       << " linearityRows:" << mLinearity.Rows() << "(" << mLinearity.Rows()*mNInstances << ")"
       << " biasDims:" << mBias.Dim() << "(" << mBias.Dim()*mNInstances << ")"
       << "\n";
    Error(os.str());
  }

  mLinearityCorrection.Init(mLinearity.Rows(),mLinearity.Cols());
  mBiasCorrection.Init(mBias.Dim());
}

 
void
SharedLinearity::
WriteToStream(std::ostream& rOut)
{
  rOut << mNInstances << std::endl;
  //matrix is stored transposed as SNet does
  BfMatrix transpose(mLinearity, TRANS);
  rOut << transpose;
  //biases stored normally
  rOut << mBias;
  rOut << std::endl;
}


void 
SharedLinearity::
Gradient() 
{
  const Matrix<BaseFloat>& X = GetInput();
  const Matrix<BaseFloat>& E = GetErrorInput();
  //get gradient of shared linearity
  for(int k=0; k<mNInstances; k++) {
    SubMatrix<BaseFloat> xblock(X,0,X.Rows(),k*mpLinearity->Rows(),mpLinearity->Rows());
    SubMatrix<BaseFloat> eblock(E,0,E.Rows(),k*mpLinearity->Cols(),mpLinearity->Cols());
    mLinearityCorrection.BlasGemm(1.0,xblock,TRANS,eblock,NO_TRANS,((k==0)?0.0f:1.0f));
  }

  //get gradient of shared bias
  mBiasCorrection.Set(0.0f);
  for(int r=0; r<E.Rows(); r++) {
    for(int c=0; c<E.Cols(); c++) {
      mBiasCorrection[c%mBiasCorrection.Dim()] += E(r,c);
    }
  }
}


void 
SharedLinearity::
AccuGradient(const UpdatableComponent& src, int thr, int thrN)
{
  //cast the argument
  const SharedLinearity& src_comp = dynamic_cast<const SharedLinearity&>(src);

  //allocate accumulators when needed
  if(mLinearityCorrectionAccu.MSize() == 0) {
    mLinearityCorrectionAccu.Init(mpLinearity->Rows(),mpLinearity->Cols());
  }
  if(mBiasCorrectionAccu.MSize() == 0) {
    mBiasCorrectionAccu.Init(mpBias->Dim());
  }
 

  //assert the dimensions
  /*
  assert(mLinearityCorrection.Rows() == src_comp.mLinearityCorrection.Rows());
  assert(mLinearityCorrection.Cols() == src_comp.mLinearityCorrection.Cols());
  assert(mBiasCorrection.Dim() == src_comp.mBiasCorrection.Dim());
  */

  //need to find out which rows to sum...
  int div = mLinearityCorrection.Rows() / thrN;
  int mod = mLinearityCorrection.Rows() % thrN;

  int origin = thr * div + ((mod > thr)? thr : mod);
  int rows = div + ((mod > thr)? 1 : 0);

  //std::cout << "[S" << thr << "," << origin << "," << rows << "]" << std::flush;

  //create the matrix windows
  const SubMatrix<BaseFloat> src_mat (
    src_comp.mLinearityCorrection, 
    origin, rows, 
    0, mLinearityCorrection.Cols()
  );
  SubMatrix<double> tgt_mat (
    mLinearityCorrectionAccu, 
    origin, rows, 
    0, mLinearityCorrection.Cols()
  );
  //sum the rows
  Add(tgt_mat,src_mat);

  //first thread will always sum the bias correction and adds frame count
  if(thr == 0) {
    //std::cout << "[BS" << thr << "]" << std::flush;
    Add(mBiasCorrectionAccu,src_comp.mBiasCorrection);
  }
}


void 
SharedLinearity::
Update(int thr, int thrN) 
{
  //need to find out which rows to sum...
  int div = mLinearity.Rows() / thrN;
  int mod = mLinearity.Rows() % thrN;

  int origin = thr * div + ((mod > thr)? thr : mod);
  int rows = div + ((mod > thr)? 1 : 0);

  //std::cout << "[P" << thr << "," << origin << "," << rows << "]" << std::flush;

  //get the matrix windows
  SubMatrix<double> src_mat (
    mLinearityCorrectionAccu, 
    origin, rows, 
    0, mLinearityCorrection.Cols()
  );
  SubMatrix<BaseFloat> tgt_mat (
    mLinearity, 
    origin, rows, 
    0, mLinearityCorrection.Cols()
  );

  //TODO perform L2 regularization
  //tgt_mat.AddScaled(tgt_mat, -mWeightcost * num_frames);

  //update weights
  AddScaled(tgt_mat, src_mat, -mLearningRate/static_cast<BaseFloat>(mNInstances));

  //first thread always update bias
  if(thr == 0) {
    //std::cout << "[" << thr << "BP]" << std::flush;
    AddScaled(mBias, mBiasCorrectionAccu, -mLearningRate/static_cast<BaseFloat>(mNInstances));
  }

  //reset the accumulators
  src_mat.Zero();
  if(thr == 0) {
    mBiasCorrectionAccu.Zero();
  }
}

 
} //namespace