Logo ROOT   6.14/05
Reference Guide
GeneralLayer.h
Go to the documentation of this file.
1 // @(#)root/tmva/tmva/dnn:$Id$
2 // Author: Vladimir Ilievski
3 
4 /**********************************************************************************
5  * Project: TMVA - a Root-integrated toolkit for multivariate data analysis *
6  * Package: TMVA *
7  * Class : TGeneralLayer *
8  * Web : http://tmva.sourceforge.net *
9  * *
10  * Description: *
11  * General Deep Neural Network Layer *
12  * *
13  * Authors (alphabetical): *
14  * Vladimir Ilievski <ilievski.vladimir@live.com> - CERN, Switzerland *
15  * *
16  * Copyright (c) 2005-2015: *
17  * CERN, Switzerland *
18  * U. of Victoria, Canada *
19  * MPI-K Heidelberg, Germany *
20  * U. of Bonn, Germany *
21  * *
22  * Redistribution and use in source and binary forms, with or without *
23  * modification, are permitted according to the terms listed in LICENSE *
24  * (http://tmva.sourceforge.net/LICENSE) *
25  **********************************************************************************/
26 
27 #ifndef TMVA_DNN_GENERALLAYER
28 #define TMVA_DNN_GENERALLAYER
29 
30 #include <iostream>
31 
32 // for xml
33 #include "TMVA/Tools.h"
34 
35 namespace TMVA {
36 namespace DNN {
37 
38 /** \class VGeneralLayer
39  Generic General Layer class.
40 
41  This class represents the general class for all layers in the Deep Learning
42  Module.
43  */
44 template <typename Architecture_t>
46  using Matrix_t = typename Architecture_t::Matrix_t;
47  using Scalar_t = typename Architecture_t::Scalar_t;
48 
49 protected:
50  size_t fBatchSize; ///< Batch size used for training and evaluation
51 
52  size_t fInputDepth; ///< The depth of the previous layer or input.
53  size_t fInputHeight; ///< The height of the previous layer or input.
54  size_t fInputWidth; ///< The width of the previous layer or input.
55 
56  size_t fDepth; ///< The depth of the layer.
57  size_t fHeight; ///< The height of the layer.
58  size_t fWidth; ///< The width of this layer.
59 
60  bool fIsTraining; ///< Flag indicatig the mode
61 
62  std::vector<Matrix_t> fWeights; ///< The weights associated to the layer.
63  std::vector<Matrix_t> fBiases; ///< The biases associated to the layer.
64 
65  std::vector<Matrix_t> fWeightGradients; ///< Gradients w.r.t. the weights of the layer.
66  std::vector<Matrix_t> fBiasGradients; ///< Gradients w.r.t. the bias values of the layer.
67 
68  std::vector<Matrix_t> fOutput; ///< Activations of this layer.
69  std::vector<Matrix_t> fActivationGradients; ///< Gradients w.r.t. the activations of this layer.
70 
71  EInitialization fInit; ///< The initialization method.
72 
73 public:
74  /*! Constructor */
75  VGeneralLayer(size_t BatchSize, size_t InputDepth, size_t InputHeight, size_t InputWidth, size_t Depth,
76  size_t Height, size_t Width, size_t WeightsNSlices, size_t WeightsNRows, size_t WeightsNCols,
77  size_t BiasesNSlices, size_t BiasesNRows, size_t BiasesNCols, size_t OutputNSlices, size_t OutputNRows,
78  size_t OutputNCols, EInitialization Init);
79 
80  /*! General Constructor with different weights dimension */
81  VGeneralLayer(size_t BatchSize, size_t InputDepth, size_t InputHeight, size_t InputWidth, size_t Depth,
82  size_t Height, size_t Width, size_t WeightsNSlices, std::vector<size_t> WeightsNRows,
83  std::vector<size_t> WeightsNCols, size_t BiasesNSlices, std::vector<size_t> BiasesNRows,
84  std::vector<size_t> BiasesNCols, size_t OutputNSlices, size_t OutputNRows, size_t OutputNCols,
86 
87  /*! Copy the layer provided as a pointer */
89 
90  /*! Copy Constructor */
92 
93  /*! Virtual Destructor. */
94  virtual ~VGeneralLayer();
95 
96  /*! Initialize the weights and biases according to the given initialization method. */
97  void Initialize();
98 
99  /*! Computes activation of the layer for the given input. The input
100  * must be in 3D tensor form with the different matrices corresponding to
101  * different events in the batch. */
102  virtual void Forward(std::vector<Matrix_t> &input, bool applyDropout = false) = 0;
103 
104  /*! Backpropagates the error. Must only be called directly at the corresponding
105  * call to Forward(...). */
106  virtual void Backward(std::vector<Matrix_t> &gradients_backward, const std::vector<Matrix_t> &activations_backward,
107  std::vector<Matrix_t> &inp1, std::vector<Matrix_t> &inp2) = 0;
108 
109  /*! Updates the weights and biases, given the learning rate */
110  void Update(const Scalar_t learningRate);
111 
112  /*! Updates the weights, given the gradients and the learning rate, */
113  void UpdateWeights(const std::vector<Matrix_t> &weightGradients, const Scalar_t learningRate);
114 
115  /*! Updates the biases, given the gradients and the learning rate. */
116  void UpdateBiases(const std::vector<Matrix_t> &biasGradients, const Scalar_t learningRate);
117 
118  /*! Updates the weight gradients, given some other weight gradients and learning rate. */
119  void UpdateWeightGradients(const std::vector<Matrix_t> &weightGradients, const Scalar_t learningRate);
120 
121  /*! Updates the bias gradients, given some other weight gradients and learning rate. */
122  void UpdateBiasGradients(const std::vector<Matrix_t> &biasGradients, const Scalar_t learningRate);
123 
124  /*! Copies the weights provided as an input. */
125  void CopyWeights(const std::vector<Matrix_t> &otherWeights);
126 
127  /*! Copies the biases provided as an input. */
128  void CopyBiases(const std::vector<Matrix_t> &otherBiases);
129 
130  /*! Prints the info about the layer. */
131  virtual void Print() const = 0;
132 
133  /*! Writes the information and the weights about the layer in an XML node. */
134  virtual void AddWeightsXMLTo(void *parent) = 0;
135 
136  /*! Read the information and the weights about the layer from XML node. */
137  virtual void ReadWeightsFromXML(void *parent) = 0;
138 
139  /*! Getters */
140  size_t GetBatchSize() const { return fBatchSize; }
141  size_t GetInputDepth() const { return fInputDepth; }
142  size_t GetInputHeight() const { return fInputHeight; }
143  size_t GetInputWidth() const { return fInputWidth; }
144  size_t GetDepth() const { return fDepth; }
145  size_t GetHeight() const { return fHeight; }
146  size_t GetWidth() const { return fWidth; }
147  bool IsTraining() const { return fIsTraining; }
148 
149  const std::vector<Matrix_t> &GetWeights() const { return fWeights; }
150  std::vector<Matrix_t> &GetWeights() { return fWeights; }
151 
152  const Matrix_t &GetWeightsAt(size_t i) const { return fWeights[i]; }
153  Matrix_t &GetWeightsAt(size_t i) { return fWeights[i]; }
154 
155  const std::vector<Matrix_t> &GetBiases() const { return fBiases; }
156  std::vector<Matrix_t> &GetBiases() { return fBiases; }
157 
158  const Matrix_t &GetBiasesAt(size_t i) const { return fBiases[i]; }
159  Matrix_t &GetBiasesAt(size_t i) { return fBiases[i]; }
160 
161  const std::vector<Matrix_t> &GetWeightGradients() const { return fWeightGradients; }
162  std::vector<Matrix_t> &GetWeightGradients() { return fWeightGradients; }
163 
164  const Matrix_t &GetWeightGradientsAt(size_t i) const { return fWeightGradients[i]; }
165  Matrix_t &GetWeightGradientsAt(size_t i) { return fWeightGradients[i]; }
166 
167  const std::vector<Matrix_t> &GetBiasGradients() const { return fBiasGradients; }
168  std::vector<Matrix_t> &GetBiasGradients() { return fBiasGradients; }
169 
170  const Matrix_t &GetBiasGradientsAt(size_t i) const { return fBiasGradients[i]; }
171  Matrix_t &GetBiasGradientsAt(size_t i) { return fBiasGradients[i]; }
172 
173  const std::vector<Matrix_t> &GetOutput() const { return fOutput; }
174  std::vector<Matrix_t> &GetOutput() { return fOutput; }
175 
176  const std::vector<Matrix_t> &GetActivationGradients() const { return fActivationGradients; }
177  std::vector<Matrix_t> &GetActivationGradients() { return fActivationGradients; }
178 
179  Matrix_t &GetOutputAt(size_t i) { return fOutput[i]; }
180  const Matrix_t &GetOutputAt(size_t i) const { return fOutput[i]; }
181 
182  Matrix_t &GetActivationGradientsAt(size_t i) { return fActivationGradients[i]; }
183  const Matrix_t &GetActivationGradientsAt(size_t i) const { return fActivationGradients[i]; }
184 
186 
187  /*! Setters */
188  void SetBatchSize(size_t batchSize) { fBatchSize = batchSize; }
189  void SetInputDepth(size_t inputDepth) { fInputDepth = inputDepth; }
190  void SetInputHeight(size_t inputHeight) { fInputHeight = inputHeight; }
191  void SetInputWidth(size_t inputWidth) { fInputWidth = inputWidth; }
192  void SetDepth(size_t depth) { fDepth = depth; }
193  void SetHeight(size_t height) { fHeight = height; }
194  void SetWidth(size_t width) { fWidth = width; }
195  void SetIsTraining(bool isTraining) { fIsTraining = isTraining; }
196 
197  /// helper functions for XML
198  void WriteTensorToXML( void * node, const char * name, const std::vector<Matrix_t> & tensor);
199  void WriteMatrixToXML( void * node, const char * name, const Matrix_t & matrix);
200 
201  void ReadMatrixXML( void * node, const char * name, Matrix_t & matrix);
202 
203 };
204 
205 //
206 //
207 // The General Layer Class - Implementation
208 //_________________________________________________________________________________________________
209 template <typename Architecture_t>
210 VGeneralLayer<Architecture_t>::VGeneralLayer(size_t batchSize, size_t inputDepth, size_t inputHeight, size_t inputWidth,
211  size_t depth, size_t height, size_t width, size_t weightsNSlices,
212  size_t weightsNRows, size_t weightsNCols, size_t biasesNSlices,
213  size_t biasesNRows, size_t biasesNCols, size_t outputNSlices,
214  size_t outputNRows, size_t outputNCols, EInitialization init)
215  : fBatchSize(batchSize), fInputDepth(inputDepth), fInputHeight(inputHeight), fInputWidth(inputWidth), fDepth(depth),
216  fHeight(height), fWidth(width), fIsTraining(true), fWeights(), fBiases(), fWeightGradients(), fBiasGradients(),
218 {
219 
220  for (size_t i = 0; i < weightsNSlices; i++) {
221  fWeights.emplace_back(weightsNRows, weightsNCols);
222  fWeightGradients.emplace_back(weightsNRows, weightsNCols);
223  }
224 
225  for (size_t i = 0; i < biasesNSlices; i++) {
226  fBiases.emplace_back(biasesNRows, biasesNCols);
227  fBiasGradients.emplace_back(biasesNRows, biasesNCols);
228  }
229 
230  for (size_t i = 0; i < outputNSlices; i++) {
231  fOutput.emplace_back(outputNRows, outputNCols);
232  fActivationGradients.emplace_back(outputNRows, outputNCols);
233  }
234 }
235 
236 //_________________________________________________________________________________________________
237 template <typename Architecture_t>
238 VGeneralLayer<Architecture_t>::VGeneralLayer(size_t batchSize, size_t inputDepth, size_t inputHeight, size_t inputWidth,
239  size_t depth, size_t height, size_t width, size_t weightsNSlices,
240  std::vector<size_t> weightsNRows, std::vector<size_t> weightsNCols,
241  size_t biasesNSlices, std::vector<size_t> biasesNRows,
242  std::vector<size_t> biasesNCols, size_t outputNSlices, size_t outputNRows,
243  size_t outputNCols, EInitialization init)
244  : fBatchSize(batchSize), fInputDepth(inputDepth), fInputHeight(inputHeight), fInputWidth(inputWidth), fDepth(depth),
245  fHeight(height), fWidth(width), fIsTraining(true), fWeights(), fBiases(), fWeightGradients(), fBiasGradients(),
247 {
248 
249  for (size_t i = 0; i < weightsNSlices; i++) {
250  fWeights.emplace_back(weightsNRows[i], weightsNCols[i]);
251  fWeightGradients.emplace_back(weightsNRows[i], weightsNCols[i]);
252  }
253 
254  for (size_t i = 0; i < biasesNSlices; i++) {
255  fBiases.emplace_back(biasesNRows[i], biasesNCols[i]);
256  fBiasGradients.emplace_back(biasesNRows[i], biasesNCols[i]);
257  }
258 
259  for (size_t i = 0; i < outputNSlices; i++) {
260  fOutput.emplace_back(outputNRows, outputNCols);
261  fActivationGradients.emplace_back(outputNRows, outputNCols);
262  }
263 }
264 
265 //_________________________________________________________________________________________________
266 template <typename Architecture_t>
269  fInputWidth(layer->GetInputWidth()), fDepth(layer->GetDepth()), fHeight(layer->GetHeight()),
270  fWidth(layer->GetWidth()), fIsTraining(layer->IsTraining()), fWeights(), fBiases(), fWeightGradients(),
272 {
273  size_t weightsNSlices = (layer->GetWeights()).size();
274  size_t weightsNRows = 0;
275  size_t weightsNCols = 0;
276 
277  for (size_t i = 0; i < weightsNSlices; i++) {
278  weightsNRows = (layer->GetWeightsAt(i)).GetNrows();
279  weightsNCols = (layer->GetWeightsAt(i)).GetNcols();
280 
281  fWeights.emplace_back(weightsNRows, weightsNCols);
282  fWeightGradients.emplace_back(weightsNRows, weightsNCols);
283 
285  }
286 
287  size_t biasesNSlices = (layer->GetBiases()).size();
288  size_t biasesNRows = 0;
289  size_t biasesNCols = 0;
290 
291  for (size_t i = 0; i < biasesNSlices; i++) {
292  biasesNRows = (layer->GetBiasesAt(i)).GetNrows();
293  biasesNCols = (layer->GetBiasesAt(i)).GetNcols();
294 
295  fBiases.emplace_back(biasesNRows, biasesNCols);
296  fBiasGradients.emplace_back(biasesNRows, biasesNCols);
297 
299  }
300 
301  size_t outputNSlices = (layer->GetOutput()).size();
302  size_t outputNRows = 0;
303  size_t outputNCols = 0;
304 
305  for (size_t i = 0; i < outputNSlices; i++) {
306  outputNRows = (layer->GetOutputAt(i)).GetNrows();
307  outputNCols = (layer->GetOutputAt(i)).GetNcols();
308 
309  fOutput.emplace_back(outputNRows, outputNCols);
310  fActivationGradients.emplace_back(outputNRows, outputNCols);
311  }
312 }
313 
314 //_________________________________________________________________________________________________
315 template <typename Architecture_t>
318  fInputWidth(layer.fInputWidth), fDepth(layer.fDepth), fHeight(layer.fHeight), fWidth(layer.fWidth),
321 {
322  size_t weightsNSlices = layer.fWeights.size();
323  size_t weightsNRows = 0;
324  size_t weightsNCols = 0;
325 
326  for (size_t i = 0; i < weightsNSlices; i++) {
327  weightsNRows = (layer.fWeights[i]).GetNrows();
328  weightsNCols = (layer.fWeights[i]).GetNcols();
329 
330  fWeights.emplace_back(weightsNRows, weightsNCols);
331  fWeightGradients.emplace_back(weightsNRows, weightsNCols);
332 
333  Architecture_t::Copy(fWeights[i], layer.fWeights[i]);
334  }
335 
336  size_t biasesNSlices = layer.fBiases.size();
337  size_t biasesNRows = 0;
338  size_t biasesNCols = 0;
339 
340  for (size_t i = 0; i < biasesNSlices; i++) {
341  biasesNRows = (layer.fBiases[i]).GetNrows();
342  biasesNCols = (layer.fBiases[i]).GetNcols();
343 
344  fBiases.emplace_back(biasesNRows, biasesNCols);
345  fBiasGradients.emplace_back(biasesNRows, biasesNCols);
346 
347  Architecture_t::Copy(fBiases[i], layer.fBiases[i]);
348  }
349 
350  size_t outputNSlices = layer.fOutput.size();
351  size_t outputNRows = 0;
352  size_t outputNCols = 0;
353 
354  for (size_t i = 0; i < outputNSlices; i++) {
355  outputNRows = (layer.fOutput[i]).GetNrows();
356  outputNCols = (layer.fOutput[i]).GetNcols();
357 
358  fOutput.emplace_back(outputNRows, outputNCols);
359  fActivationGradients.emplace_back(outputNRows, outputNCols);
360  }
361 }
362 
363 //_________________________________________________________________________________________________
364 template <typename Architecture_t>
366 {
367  // Nothing to do here.
368 }
369 
370 //_________________________________________________________________________________________________
371 template <typename Architecture_t>
373 {
374  for (size_t i = 0; i < fWeights.size(); i++) {
375  initialize<Architecture_t>(fWeights[i], this->GetInitialization());
376  initialize<Architecture_t>(fWeightGradients[i], EInitialization::kZero);
377  }
378 
379  for (size_t i = 0; i < fBiases.size(); i++) {
380  initialize<Architecture_t>(fBiases[i], EInitialization::kZero);
381  initialize<Architecture_t>(fBiasGradients[i], EInitialization::kZero);
382  }
383 }
384 
385 //_________________________________________________________________________________________________
386 template <typename Architecture_t>
387 auto VGeneralLayer<Architecture_t>::Update(const Scalar_t learningRate) -> void
388 {
389  this->UpdateWeights(fWeightGradients, learningRate);
390  this->UpdateBiases(fBiasGradients, learningRate);
391 }
392 
393 //_________________________________________________________________________________________________
394 template <typename Architecture_t>
395 auto VGeneralLayer<Architecture_t>::UpdateWeights(const std::vector<Matrix_t> &weightGradients,
396  const Scalar_t learningRate) -> void
397 {
398  for (size_t i = 0; i < fWeights.size(); i++) {
399  Architecture_t::ScaleAdd(fWeights[i], weightGradients[i], -learningRate);
400  }
401 }
402 
403 //_________________________________________________________________________________________________
404 template <typename Architecture_t>
405 auto VGeneralLayer<Architecture_t>::UpdateBiases(const std::vector<Matrix_t> &biasGradients,
406  const Scalar_t learningRate) -> void
407 {
408  for (size_t i = 0; i < fBiases.size(); i++) {
409  Architecture_t::ScaleAdd(fBiases[i], biasGradients[i], -learningRate);
410  }
411 }
412 
413 //_________________________________________________________________________________________________
414 template <typename Architecture_t>
415 auto VGeneralLayer<Architecture_t>::UpdateWeightGradients(const std::vector<Matrix_t> &weightGradients,
416  const Scalar_t learningRate) -> void
417 {
418  for (size_t i = 0; i < fWeightGradients.size(); i++) {
419  Architecture_t::ScaleAdd(fWeightGradients[i], weightGradients[i], -learningRate);
420  }
421 }
422 
423 //_________________________________________________________________________________________________
424 template <typename Architecture_t>
425 auto VGeneralLayer<Architecture_t>::UpdateBiasGradients(const std::vector<Matrix_t> &biasGradients,
426  const Scalar_t learningRate) -> void
427 {
428  for (size_t i = 0; i < fBiasGradients.size(); i++) {
429  Architecture_t::ScaleAdd(fBiasGradients[i], biasGradients[i], -learningRate);
430  }
431 }
432 
433 //_________________________________________________________________________________________________
434 template <typename Architecture_t>
435 auto VGeneralLayer<Architecture_t>::CopyWeights(const std::vector<Matrix_t> &otherWeights) -> void
436 {
437 
438  for (size_t i = 0; i < fWeights.size(); i++) {
439  Architecture_t::Copy(fWeights[i], otherWeights[i]);
440  }
441 }
442 
443 //_________________________________________________________________________________________________
444 template <typename Architecture_t>
445 auto VGeneralLayer<Architecture_t>::CopyBiases(const std::vector<Matrix_t> &otherBiases) -> void
446 {
447  for (size_t i = 0; i < fBiases.size(); i++) {
448  Architecture_t::Copy(fBiases[i], otherBiases[i]);
449  }
450 }
451 
452 
453 //_________________________________________________________________________________________________
454 template <typename Architecture_t>
455 auto VGeneralLayer<Architecture_t>::WriteTensorToXML(void * node, const char * name, const std::vector<Matrix_t> & tensor) -> void
456 {
457  auto xmlengine = gTools().xmlengine();
458  void* matnode = xmlengine.NewChild(node, 0, name);
459  if (tensor.size() == 0) return;
460  xmlengine.NewAttr(matnode,0,"Depth", gTools().StringFromInt(tensor.size()) );
461  // assume same number of rows and columns for every matrix in std::vector
462  xmlengine.NewAttr(matnode,0,"Rows", gTools().StringFromInt(tensor[0].GetNrows()) );
463  xmlengine.NewAttr(matnode,0,"Columns", gTools().StringFromInt(tensor[0].GetNcols()) );
464  std::stringstream s;
465  for (size_t i = 0; i < tensor.size(); ++i) {
466  auto & mat = tensor[i];
467  for (Int_t row = 0; row < mat.GetNrows(); row++) {
468  for (Int_t col = 0; col < mat.GetNcols(); col++) {
469  TString tmp = TString::Format( "%5.15e ", (mat)(row,col) );
470  s << tmp.Data();
471  }
472  }
473  }
474  xmlengine.AddRawLine( matnode, s.str().c_str() );
475 }
476 
477 //_________________________________________________________________________________________________
478 template <typename Architecture_t>
479 auto VGeneralLayer<Architecture_t>::WriteMatrixToXML(void * node, const char * name, const Matrix_t & matrix) -> void
480 {
481  auto xmlengine = gTools().xmlengine();
482  void* matnode = xmlengine.NewChild(node, 0, name);
483 
484  xmlengine.NewAttr(matnode,0,"Rows", gTools().StringFromInt(matrix.GetNrows()) );
485  xmlengine.NewAttr(matnode,0,"Columns", gTools().StringFromInt(matrix.GetNcols()) );
486  std::stringstream s;
487  s.precision( 16 );
488  size_t nrows = matrix.GetNrows();
489  size_t ncols = matrix.GetNcols();
490  for (size_t row = 0; row < nrows; row++) {
491  for (size_t col = 0; col < ncols; col++) {
492  //TString tmp = TString::Format( "%5.15e ", matrix(row,col) );
493  s << std::scientific << matrix(row,col) << " ";
494  }
495  }
496 
497  xmlengine.AddRawLine( matnode, s.str().c_str() );
498 }
499 
500 //_________________________________________________________________________________________________
501 template <typename Architecture_t>
502 auto VGeneralLayer<Architecture_t>::ReadMatrixXML(void * node, const char * name, Matrix_t & matrix) -> void
503 {
504  void *matrixXML = gTools().GetChild(node, name);
505  size_t rows, cols;
506  gTools().ReadAttr(matrixXML, "Rows", rows);
507  gTools().ReadAttr(matrixXML, "Columns", cols);
508 
509  R__ASSERT((size_t) matrix.GetNrows() == rows);
510  R__ASSERT((size_t) matrix.GetNcols() == cols);
511 
512  const char * matrixString = gTools().xmlengine().GetNodeContent(matrixXML);
513  std::stringstream matrixStringStream(matrixString);
514 
515  for (size_t i = 0; i < rows; i++)
516  {
517  for (size_t j = 0; j < cols; j++)
518  {
519 #ifndef R__HAS_TMVAGPU
520  matrixStringStream >> matrix(i,j);
521 #else
522  Scalar_t value;
523  matrixStringStream >> value;
524  matrix(i,j) = value;
525 #endif
526 
527  }
528  }
529 }
530 
531 } // namespace DNN
532 } // namespace TMVA
533 
534 #endif
void SetWidth(size_t width)
Definition: GeneralLayer.h:194
size_t GetDepth() const
Definition: GeneralLayer.h:144
size_t GetInputWidth() const
Definition: GeneralLayer.h:143
Generic General Layer class.
Definition: GeneralLayer.h:45
size_t fDepth
The depth of the layer.
Definition: GeneralLayer.h:56
void CopyBiases(const std::vector< Matrix_t > &otherBiases)
Copies the biases provided as an input.
Definition: GeneralLayer.h:445
TXMLEngine & xmlengine()
Definition: Tools.h:270
void Update(const Scalar_t learningRate)
Updates the weights and biases, given the learning rate.
Definition: GeneralLayer.h:387
const std::vector< Matrix_t > & GetWeightGradients() const
Definition: GeneralLayer.h:161
void SetBatchSize(size_t batchSize)
Setters.
Definition: GeneralLayer.h:188
size_t fHeight
The height of the layer.
Definition: GeneralLayer.h:57
size_t fWidth
The width of this layer.
Definition: GeneralLayer.h:58
void ReadMatrixXML(void *node, const char *name, Matrix_t &matrix)
Definition: GeneralLayer.h:502
const Matrix_t & GetBiasesAt(size_t i) const
Definition: GeneralLayer.h:158
void SetDepth(size_t depth)
Definition: GeneralLayer.h:192
image html pict1_TGaxis_012 png width
Define new text attributes for the label number "labNum".
Definition: TGaxis.cxx:2551
EInitialization GetInitialization() const
Definition: GeneralLayer.h:185
Matrix_t & GetWeightsAt(size_t i)
Definition: GeneralLayer.h:153
virtual void Backward(std::vector< Matrix_t > &gradients_backward, const std::vector< Matrix_t > &activations_backward, std::vector< Matrix_t > &inp1, std::vector< Matrix_t > &inp2)=0
Backpropagates the error.
std::vector< Matrix_t > fWeights
The weights associated to the layer.
Definition: GeneralLayer.h:62
#define R__ASSERT(e)
Definition: TError.h:96
VGeneralLayer(size_t BatchSize, size_t InputDepth, size_t InputHeight, size_t InputWidth, size_t Depth, size_t Height, size_t Width, size_t WeightsNSlices, size_t WeightsNRows, size_t WeightsNCols, size_t BiasesNSlices, size_t BiasesNRows, size_t BiasesNCols, size_t OutputNSlices, size_t OutputNRows, size_t OutputNCols, EInitialization Init)
Constructor.
Definition: GeneralLayer.h:210
Basic string class.
Definition: TString.h:131
virtual void Forward(std::vector< Matrix_t > &input, bool applyDropout=false)=0
Computes activation of the layer for the given input.
EInitialization fInit
The initialization method.
Definition: GeneralLayer.h:71
int Int_t
Definition: RtypesCore.h:41
const Matrix_t & GetOutputAt(size_t i) const
Definition: GeneralLayer.h:180
std::vector< Matrix_t > fWeightGradients
Gradients w.r.t. the weights of the layer.
Definition: GeneralLayer.h:65
virtual void Print() const =0
Prints the info about the layer.
const char * GetNodeContent(XMLNodePointer_t xmlnode)
get contents (if any) of xmlnode
UInt_t Depth(const Node< T > *node)
Definition: NodekNN.h:213
EInitialization
Definition: Functions.h:70
const std::vector< Matrix_t > & GetWeights() const
Definition: GeneralLayer.h:149
Matrix_t & GetOutputAt(size_t i)
Definition: GeneralLayer.h:179
static TString Format(const char *fmt,...)
Static method which formats a string using a printf style format descriptor and return a TString...
Definition: TString.cxx:2286
size_t fInputDepth
The depth of the previous layer or input.
Definition: GeneralLayer.h:52
virtual void ReadWeightsFromXML(void *parent)=0
Read the information and the weights about the layer from XML node.
const std::vector< Matrix_t > & GetBiasGradients() const
Definition: GeneralLayer.h:167
void Init(TClassEdit::TInterpreterLookupHelper *helper)
Definition: TClassEdit.cxx:121
void * GetChild(void *parent, const char *childname=0)
get child node
Definition: Tools.cxx:1162
size_t GetBatchSize() const
Getters.
Definition: GeneralLayer.h:140
Matrix_t & GetBiasesAt(size_t i)
Definition: GeneralLayer.h:159
std::vector< Matrix_t > fOutput
Activations of this layer.
Definition: GeneralLayer.h:68
size_t GetHeight() const
Definition: GeneralLayer.h:145
const std::vector< Matrix_t > & GetActivationGradients() const
Definition: GeneralLayer.h:176
std::vector< Matrix_t > & GetWeightGradients()
Definition: GeneralLayer.h:162
void UpdateBiasGradients(const std::vector< Matrix_t > &biasGradients, const Scalar_t learningRate)
Updates the bias gradients, given some other weight gradients and learning rate.
Definition: GeneralLayer.h:425
std::vector< Matrix_t > & GetBiases()
Definition: GeneralLayer.h:156
std::vector< Matrix_t > fBiasGradients
Gradients w.r.t. the bias values of the layer.
Definition: GeneralLayer.h:66
void UpdateWeightGradients(const std::vector< Matrix_t > &weightGradients, const Scalar_t learningRate)
Updates the weight gradients, given some other weight gradients and learning rate.
Definition: GeneralLayer.h:415
virtual ~VGeneralLayer()
Virtual Destructor.
Definition: GeneralLayer.h:365
void UpdateWeights(const std::vector< Matrix_t > &weightGradients, const Scalar_t learningRate)
Updates the weights, given the gradients and the learning rate,.
Definition: GeneralLayer.h:395
void Initialize()
Initialize the weights and biases according to the given initialization method.
Definition: GeneralLayer.h:372
const Matrix_t & GetBiasGradientsAt(size_t i) const
Definition: GeneralLayer.h:170
typename Architecture_t::Matrix_t Matrix_t
Definition: GeneralLayer.h:46
const std::vector< Matrix_t > & GetBiases() const
Definition: GeneralLayer.h:155
void SetInputWidth(size_t inputWidth)
Definition: GeneralLayer.h:191
size_t GetInputHeight() const
Definition: GeneralLayer.h:142
const Matrix_t & GetActivationGradientsAt(size_t i) const
Definition: GeneralLayer.h:183
size_t GetInputDepth() const
Definition: GeneralLayer.h:141
Matrix_t & GetBiasGradientsAt(size_t i)
Definition: GeneralLayer.h:171
void SetInputHeight(size_t inputHeight)
Definition: GeneralLayer.h:190
std::vector< Matrix_t > & GetActivationGradients()
Definition: GeneralLayer.h:177
void ReadAttr(void *node, const char *, T &value)
read attribute from xml
Definition: Tools.h:335
Tools & gTools()
std::vector< Matrix_t > & GetBiasGradients()
Definition: GeneralLayer.h:168
const Matrix_t & GetWeightsAt(size_t i) const
Definition: GeneralLayer.h:152
Matrix_t & GetActivationGradientsAt(size_t i)
Definition: GeneralLayer.h:182
void Copy(void *source, void *dest)
std::vector< Matrix_t > fActivationGradients
Gradients w.r.t. the activations of this layer.
Definition: GeneralLayer.h:69
static Int_t init()
std::vector< Matrix_t > fBiases
The biases associated to the layer.
Definition: GeneralLayer.h:63
size_t fBatchSize
Batch size used for training and evaluation.
Definition: GeneralLayer.h:50
std::vector< Matrix_t > & GetWeights()
Definition: GeneralLayer.h:150
static constexpr double s
void UpdateBiases(const std::vector< Matrix_t > &biasGradients, const Scalar_t learningRate)
Updates the biases, given the gradients and the learning rate.
Definition: GeneralLayer.h:405
Matrix_t & GetWeightGradientsAt(size_t i)
Definition: GeneralLayer.h:165
void SetInputDepth(size_t inputDepth)
Definition: GeneralLayer.h:189
std::vector< Matrix_t > & GetOutput()
Definition: GeneralLayer.h:174
void SetHeight(size_t height)
Definition: GeneralLayer.h:193
Abstract ClassifierFactory template that handles arbitrary types.
void WriteTensorToXML(void *node, const char *name, const std::vector< Matrix_t > &tensor)
helper functions for XML
Definition: GeneralLayer.h:455
const Matrix_t & GetWeightGradientsAt(size_t i) const
Definition: GeneralLayer.h:164
void SetIsTraining(bool isTraining)
Definition: GeneralLayer.h:195
XMLNodePointer_t NewChild(XMLNodePointer_t parent, XMLNsPointer_t ns, const char *name, const char *content=0)
create new child element for parent node
Definition: TXMLEngine.cxx:707
void WriteMatrixToXML(void *node, const char *name, const Matrix_t &matrix)
Definition: GeneralLayer.h:479
typename Architecture_t::Scalar_t Scalar_t
Definition: GeneralLayer.h:47
const std::vector< Matrix_t > & GetOutput() const
Definition: GeneralLayer.h:173
size_t fInputHeight
The height of the previous layer or input.
Definition: GeneralLayer.h:53
void CopyWeights(const std::vector< Matrix_t > &otherWeights)
Copies the weights provided as an input.
Definition: GeneralLayer.h:435
size_t fInputWidth
The width of the previous layer or input.
Definition: GeneralLayer.h:54
char name[80]
Definition: TGX11.cxx:109
virtual void AddWeightsXMLTo(void *parent)=0
Writes the information and the weights about the layer in an XML node.
size_t GetWidth() const
Definition: GeneralLayer.h:146
bool fIsTraining
Flag indicatig the mode.
Definition: GeneralLayer.h:60
const char * Data() const
Definition: TString.h:364