Logo ROOT   6.08/07
Reference Guide
Propagation.cxx
Go to the documentation of this file.
1 // @(#)root/tmva/tmva/dnn:$Id$ // Author: Simon Pfreundschuh 10/07/16
2 
3 /*************************************************************************
4  * Copyright (C) 2016, Simon Pfreundschuh *
5  * All rights reserved. *
6  * *
7  * For the licensing terms see $ROOTSYS/LICENSE. *
8  * For the list of contributors see $ROOTSYS/README/CREDITS. *
9  *************************************************************************/
10 
11 /////////////////////////////////////////////////////////////////////
12 // Implementation of the functions required for the forward and //
13 // backward propagation of activations through a neural network in //
14 // the reference implementation. //
15 /////////////////////////////////////////////////////////////////////
16 
18 
19 namespace TMVA
20 {
21 namespace DNN
22 {
23 
24 template<typename Scalar_t>
26  const TMatrixT<Scalar_t> &input,
27  const TMatrixT<Scalar_t> &weights)
28 {
29  output.MultT(input, weights);
30 }
31 
32 template<typename Scalar_t>
34  const TMatrixT<Scalar_t> &biases)
35 {
36  for (size_t i = 0; i < (size_t) output.GetNrows(); i++) {
37  for (size_t j = 0; j < (size_t) output.GetNcols(); j++) {
38  output(i,j) += biases(j,0);
39  }
40  }
41 }
42 
43 template<typename Scalar_t>
44 void TReference<Scalar_t>::Backward(TMatrixT<Scalar_t> & activation_gradients_backward,
45  TMatrixT<Scalar_t> & weight_gradients,
46  TMatrixT<Scalar_t> & bias_gradients,
47  TMatrixT<Scalar_t> & df,
48  const TMatrixT<Scalar_t> & activation_gradients,
49  const TMatrixT<Scalar_t> & weights,
50  const TMatrixT<Scalar_t> & activations_backward)
51 {
52 
53  // Compute element-wise product.
54  for (size_t i = 0; i < (size_t) df.GetNrows(); i++) {
55  for (size_t j = 0; j < (size_t) df.GetNcols(); j++) {
56  df(i,j) *= activation_gradients(i,j);
57  }
58  }
59 
60  // Activation gradients.
61  if (activation_gradients_backward.GetNoElements() > 0) {
62  activation_gradients_backward.Mult(df, weights);
63  }
64 
65  // Weights gradients.
66  if (weight_gradients.GetNoElements() > 0) {
67  weight_gradients.TMult(df, activations_backward);
68  }
69 
70  // Bias gradients.
71  if (bias_gradients.GetNoElements() > 0) {
72  for (size_t j = 0; j < (size_t) df.GetNcols(); j++) {
73  Scalar_t sum = 0.0;
74  for (size_t i = 0; i < (size_t) df.GetNrows(); i++) {
75  sum += df(i,j);
76  }
77  bias_gradients(j,0) = sum;
78  }
79  }
80 }
81 
82 template<typename Scalar_t>
84  const TMatrixT<Scalar_t> & B,
85  Scalar_t beta)
86 {
87  for (size_t i = 0; i < (size_t) A.GetNrows(); i++) {
88  for (size_t j = 0; j < (size_t) A.GetNcols(); j++) {
89  A(i,j) += beta * B(i,j);
90  }
91  }
92 }
93 
94 template<typename Scalar_t>
96  const TMatrixT<Scalar_t> & B)
97 {
98  A = B;
99 }
100 
101 } // namespace DNN
102 } // namespace TMVA
static double B[]
static long int sum(long int i)
Definition: Factory.cxx:1786
static void MultiplyTranspose(TMatrixT< Scalar_t > &output, const TMatrixT< Scalar_t > &input, const TMatrixT< Scalar_t > &weights)
Matrix-multiply input with the transpose of and write the results into output.
Definition: Propagation.cxx:25
Int_t GetNcols() const
Definition: TMatrixTBase.h:137
static void Backward(TMatrixT< Scalar_t > &activationGradientsBackward, TMatrixT< Scalar_t > &weightGradients, TMatrixT< Scalar_t > &biasGradients, TMatrixT< Scalar_t > &df, const TMatrixT< Scalar_t > &activationGradients, const TMatrixT< Scalar_t > &weights, const TMatrixT< Scalar_t > &activationBackward)
Perform the complete backward propagation step.
Definition: Propagation.cxx:44
static void AddRowWise(TMatrixT< Scalar_t > &output, const TMatrixT< Scalar_t > &biases)
Add the vectors biases row-wise to the matrix output.
Definition: Propagation.cxx:33
static double A[]
double beta(double x, double y)
Calculates the beta function.
TMatrixT.
Definition: TMatrixDfwd.h:24
Int_t GetNoElements() const
Definition: TMatrixTBase.h:138
void MultT(const TMatrixT< Element > &a, const TMatrixT< Element > &b)
General matrix multiplication. Create a matrix C such that C = A * B^T.
Definition: TMatrixT.cxx:951
void TMult(const TMatrixT< Element > &a, const TMatrixT< Element > &b)
Create a matrix C such that C = A&#39; * B.
Definition: TMatrixT.cxx:852
static void ScaleAdd(TMatrixT< Scalar_t > &A, const TMatrixT< Scalar_t > &B, Scalar_t beta=1.0)
Adds a the elements in matrix B scaled by c to the elements in the matrix A.
Definition: Propagation.cxx:83
Int_t GetNrows() const
Definition: TMatrixTBase.h:134
static void Copy(TMatrixT< Scalar_t > &A, const TMatrixT< Scalar_t > &B)
Definition: Propagation.cxx:95
Abstract ClassifierFactory template that handles arbitrary types.
void Mult(const TMatrixT< Element > &a, const TMatrixT< Element > &b)
General matrix multiplication. Create a matrix C such that C = A * B.
Definition: TMatrixT.cxx:648