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

propagator.h « src - github.com/moses-smt/nplm.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 9f214de5ddc06a2ee6c75fce3858001e00620c9d (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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
#ifndef NETWORK_H
#define NETWORK_H

#include "neuralClasses.h"
#include "util.h"

namespace nplm
{

// is this cheating?
using Eigen::Matrix;
using Eigen::MatrixBase;
using Eigen::Dynamic;

class propagator {
    int minibatch_size;
    model *pnn;

public:
    Node<Input_word_embeddings> input_layer_node;
    Node<Linear_layer> first_hidden_linear_node;
    Node<Activation_function> first_hidden_activation_node;
    Node<Linear_layer> second_hidden_linear_node;
    Node<Activation_function> second_hidden_activation_node;
    Node<Output_word_embeddings> output_layer_node;
    bool skip_hidden;

public:
    propagator () : minibatch_size(0), pnn(0) { }

    propagator (model &nn, int minibatch_size)
      :
        pnn(&nn),
        input_layer_node(&nn.input_layer, minibatch_size),
	first_hidden_linear_node(&nn.first_hidden_linear, minibatch_size),
	first_hidden_activation_node(&nn.first_hidden_activation, minibatch_size),
        second_hidden_linear_node(&nn.second_hidden_linear, minibatch_size),
	second_hidden_activation_node(&nn.second_hidden_activation, minibatch_size),
	output_layer_node(&nn.output_layer, minibatch_size),
	minibatch_size(minibatch_size)
    {
        skip_hidden = (nn.num_hidden == 0);
    }

    // This must be called if the underlying model is resized.
    void resize(int minibatch_size) {
      this->minibatch_size = minibatch_size;
      input_layer_node.resize(minibatch_size);
      first_hidden_linear_node.resize(minibatch_size);
      first_hidden_activation_node.resize(minibatch_size);
      second_hidden_linear_node.resize(minibatch_size);
      second_hidden_activation_node.resize(minibatch_size);
      output_layer_node.resize(minibatch_size);
    }

    void resize() { resize(minibatch_size); }

    template <typename Derived>
    void fProp(const MatrixBase<Derived> &data)
    {
        if (!pnn->premultiplied)
	{
            start_timer(0);
	    input_layer_node.param->fProp(data, input_layer_node.fProp_matrix);
	    stop_timer(0);
	    
	    start_timer(1);
	    first_hidden_linear_node.param->fProp(input_layer_node.fProp_matrix, 
						  first_hidden_linear_node.fProp_matrix);
	} 
	else
	{
	    int n_inputs = first_hidden_linear_node.param->n_inputs();
	    USCMatrix<double> sparse_data;
	    input_layer_node.param->munge(data, sparse_data);

	    start_timer(1);
	    first_hidden_linear_node.param->fProp(sparse_data,
						  first_hidden_linear_node.fProp_matrix);
	}
	first_hidden_activation_node.param->fProp(first_hidden_linear_node.fProp_matrix,
						  first_hidden_activation_node.fProp_matrix);
  //std::cerr<<"in fprop first hidden activation node fprop is "<<first_hidden_activation_node.fProp_matrix<<std::endl;
  //std::getchar();
	stop_timer(1);
    

        if (!skip_hidden) {
	start_timer(2);
	second_hidden_linear_node.param->fProp(first_hidden_activation_node.fProp_matrix,
					       second_hidden_linear_node.fProp_matrix);
	second_hidden_activation_node.param->fProp(second_hidden_linear_node.fProp_matrix,
						   second_hidden_activation_node.fProp_matrix);
	stop_timer(2);
        }

	// The propagation stops here because the last layer is very expensive.
    }

    // Dense version (for standard log-likelihood)
    template <typename DerivedIn, typename DerivedOut>
    void bProp(const MatrixBase<DerivedIn> &data,
	       const MatrixBase<DerivedOut> &output,
	       double learning_rate,
         double momentum,
         double L2_reg,
         std::string &parameter_update,
         double conditioning_constant,
         double decay) 
    {
        // Output embedding layer

        start_timer(7);
        output_layer_node.param->bProp(output,
				       output_layer_node.bProp_matrix);
	stop_timer(7);
	
	start_timer(8);
  Node<Activation_function> & final_hidden_activation_node = skip_hidden ? first_hidden_activation_node : second_hidden_activation_node;
  if (parameter_update == "SGD") {
    output_layer_node.param->computeGradient(final_hidden_activation_node.fProp_matrix,
               output,
               learning_rate,
               momentum);
  } else if (parameter_update == "ADA") {
    output_layer_node.param->computeGradientAdagrad(final_hidden_activation_node.fProp_matrix,
               output,
               learning_rate);
  } else if (parameter_update == "ADAD") {
    //std::cerr<<"Adadelta gradient"<<endl;
    int current_minibatch_size = final_hidden_activation_node.fProp_matrix.cols();
    output_layer_node.param->computeGradientAdadelta(final_hidden_activation_node.fProp_matrix,
               output,
               1.0/current_minibatch_size,
               conditioning_constant,
               decay);
  } else {
    std::cerr<<"Parameter update :"<<parameter_update<<" is unrecognized"<<std::endl;
  }
	stop_timer(8);

	bPropRest(data, 
      learning_rate,
      momentum,
      L2_reg,
      parameter_update,
      conditioning_constant,
      decay);
    }

    // Sparse version (for NCE log-likelihood)
    template <typename DerivedIn, typename DerivedOutI, typename DerivedOutV>
    void bProp(const MatrixBase<DerivedIn> &data,
	       const MatrixBase<DerivedOutI> &samples,
         const MatrixBase<DerivedOutV> &weights,
	       double learning_rate,
         double momentum,
         double L2_reg,
         std::string &parameter_update,
         double conditioning_constant,
         double decay) 
    {

        // Output embedding layer

        start_timer(7);
        output_layer_node.param->bProp(samples,
            weights, 
				    output_layer_node.bProp_matrix);
	stop_timer(7);
	

	start_timer(8);
  Node<Activation_function> & final_hidden_activation_node = skip_hidden ? first_hidden_activation_node : second_hidden_activation_node;
  if (parameter_update == "SGD") {
    output_layer_node.param->computeGradient(final_hidden_activation_node.fProp_matrix,
               samples,
               weights,
               learning_rate,
               momentum);
  } else if (parameter_update == "ADA") {
    output_layer_node.param->computeGradientAdagrad(final_hidden_activation_node.fProp_matrix,
               samples,
               weights,
               learning_rate);
  } else if (parameter_update == "ADAD") {
    int current_minibatch_size = final_hidden_activation_node.fProp_matrix.cols();
    //std::cerr<<"Adadelta gradient"<<endl;
    output_layer_node.param->computeGradientAdadelta(final_hidden_activation_node.fProp_matrix,
               samples,
               weights,
               1.0/current_minibatch_size,
               conditioning_constant,
               decay);
  } else {
    std::cerr<<"Parameter update :"<<parameter_update<<" is unrecognized"<<std::endl;
  }

	stop_timer(8);

	bPropRest(data,
      learning_rate,
      momentum,
      L2_reg,
      parameter_update,
      conditioning_constant,
      decay);
    }

private:
    template <typename DerivedIn>
    void bPropRest(const MatrixBase<DerivedIn> &data,
		   double learning_rate, double momentum, double L2_reg,
       std::string &parameter_update,
       double conditioning_constant,
       double decay) 
    {
	// Second hidden layer


  
  // All the compute gradient functions are together and the backprop
  // functions are together
  ////////BACKPROP////////////
        start_timer(9);
  if (skip_hidden)
  {
        start_timer(9);
        first_hidden_activation_node.param->bProp(output_layer_node.bProp_matrix,
                                                first_hidden_activation_node.bProp_matrix,
                                                first_hidden_linear_node.fProp_matrix,
                                                first_hidden_activation_node.fProp_matrix);

        first_hidden_linear_node.param->bProp(first_hidden_activation_node.bProp_matrix,
                                                first_hidden_linear_node.bProp_matrix);
        stop_timer(9);

  }
  else
  {
        second_hidden_activation_node.param->bProp(output_layer_node.bProp_matrix,
                                           second_hidden_activation_node.bProp_matrix,
                                           second_hidden_linear_node.fProp_matrix,
                                           second_hidden_activation_node.fProp_matrix);


	second_hidden_linear_node.param->bProp(second_hidden_activation_node.bProp_matrix,
					       second_hidden_linear_node.bProp_matrix);
	stop_timer(9);

	start_timer(11);
	first_hidden_activation_node.param->bProp(second_hidden_linear_node.bProp_matrix,
						  first_hidden_activation_node.bProp_matrix,
						  first_hidden_linear_node.fProp_matrix,
						  first_hidden_activation_node.fProp_matrix);

        first_hidden_linear_node.param->bProp(first_hidden_activation_node.bProp_matrix,
					      first_hidden_linear_node.bProp_matrix);
	stop_timer(11);
  }
  //std::cerr<<"First hidden layer node backprop matrix is"<<first_hidden_linear_node.bProp_matrix<<std::endl;
  //std::getchar();
  ////COMPUTE GRADIENT/////////
  if (parameter_update == "SGD") {
    if (!skip_hidden)
    {
    start_timer(10);
    second_hidden_linear_node.param->computeGradient(second_hidden_activation_node.bProp_matrix,
                 first_hidden_activation_node.fProp_matrix,
                 learning_rate,
                 momentum,
                 L2_reg);
    stop_timer(10);
    }

    // First hidden layer

    
    start_timer(12);
    first_hidden_linear_node.param->computeGradient(first_hidden_activation_node.bProp_matrix,
                input_layer_node.fProp_matrix,
                learning_rate, momentum, L2_reg);
    stop_timer(12);

    // Input word embeddings
    
    start_timer(13);
    input_layer_node.param->computeGradient(first_hidden_linear_node.bProp_matrix,
              data,
              learning_rate, momentum, L2_reg);
    stop_timer(13);
  } else if (parameter_update == "ADA") {
    if (!skip_hidden)
    {
    start_timer(10);
    second_hidden_linear_node.param->computeGradientAdagrad(second_hidden_activation_node.bProp_matrix,
                 first_hidden_activation_node.fProp_matrix,
                 learning_rate,
                 L2_reg);
    stop_timer(10);
    }

    // First hidden layer

    
    start_timer(12);
    first_hidden_linear_node.param->computeGradientAdagrad(first_hidden_activation_node.bProp_matrix,
                input_layer_node.fProp_matrix,
                learning_rate,
                L2_reg);
    stop_timer(12);

    // Input word embeddings
     
    start_timer(13);
    input_layer_node.param->computeGradientAdagrad(first_hidden_linear_node.bProp_matrix,
              data,
              learning_rate, 
              L2_reg);
    stop_timer(13);
  } else if (parameter_update == "ADAD") {
    int current_minibatch_size = first_hidden_activation_node.fProp_matrix.cols();
    //std::cerr<<"Adadelta gradient"<<endl;
    if (!skip_hidden)
    {
    start_timer(10);
    second_hidden_linear_node.param->computeGradientAdadelta(second_hidden_activation_node.bProp_matrix,
                 first_hidden_activation_node.fProp_matrix,
                 1.0/current_minibatch_size,
                 L2_reg,
                 conditioning_constant,
                 decay);
    stop_timer(10);
    }
    //std::cerr<<"Finished gradient for second hidden linear layer"<<std::endl;

    // First hidden layer

    
    start_timer(12);
    first_hidden_linear_node.param->computeGradientAdadelta(first_hidden_activation_node.bProp_matrix,
                input_layer_node.fProp_matrix,
                1.0/current_minibatch_size,
                L2_reg,
                conditioning_constant,
                decay);
    stop_timer(12);

    //std::cerr<<"Finished gradient for first hidden linear layer"<<std::endl;
    // Input word embeddings
     
    start_timer(13);
    input_layer_node.param->computeGradientAdadelta(first_hidden_linear_node.bProp_matrix,
              data,
              1.0/current_minibatch_size, 
              L2_reg,
              conditioning_constant,
              decay);
    stop_timer(13);
  
    //std::cerr<<"Finished gradient for first input layer"<<std::endl;
  } else {
    std::cerr<<"Parameter update :"<<parameter_update<<" is unrecognized"<<std::endl;
  }

    }
};

} // namespace nplm

#endif