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