Logo ROOT   6.12/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 AReal>
26  const TMatrixT<AReal> &weights)
27 {
28  output.MultT(input, weights);
29 }
30 
31 template <typename AReal>
33 {
34  for (size_t i = 0; i < (size_t) output.GetNrows(); i++) {
35  for (size_t j = 0; j < (size_t) output.GetNcols(); j++) {
36  output(i,j) += biases(j,0);
37  }
38  }
39 }
40 
41 template <typename AReal>
42 void TReference<AReal>::Backward(TMatrixT<AReal> &activation_gradients_backward, TMatrixT<AReal> &weight_gradients,
43  TMatrixT<AReal> &bias_gradients, TMatrixT<AReal> &df,
44  const TMatrixT<AReal> &activation_gradients, const TMatrixT<AReal> &weights,
45  const TMatrixT<AReal> &activations_backward)
46 {
47 
48  // Compute element-wise product.
49  for (size_t i = 0; i < (size_t) df.GetNrows(); i++) {
50  for (size_t j = 0; j < (size_t) df.GetNcols(); j++) {
51  df(i,j) *= activation_gradients(i,j);
52  }
53  }
54 
55  // Activation gradients.
56  if (activation_gradients_backward.GetNoElements() > 0) {
57  activation_gradients_backward.Mult(df, weights);
58  }
59 
60  // Weights gradients.
61  if (weight_gradients.GetNoElements() > 0) {
62  weight_gradients.TMult(df, activations_backward);
63  }
64 
65  // Bias gradients.
66  if (bias_gradients.GetNoElements() > 0) {
67  for (size_t j = 0; j < (size_t) df.GetNcols(); j++) {
68  AReal sum = 0.0;
69  for (size_t i = 0; i < (size_t) df.GetNrows(); i++) {
70  sum += df(i,j);
71  }
72  bias_gradients(j,0) = sum;
73  }
74  }
75 }
76 
77 template <typename AReal>
79 {
80  for (size_t i = 0; i < (size_t) A.GetNrows(); i++) {
81  for (size_t j = 0; j < (size_t) A.GetNcols(); j++) {
82  A(i,j) += beta * B(i,j);
83  }
84  }
85 }
86 
87 template <typename AReal>
89 {
90  A = B;
91 }
92 
93 template <typename AReal>
95 {
96  B = 0.0;
97  for (Int_t i = 0; i < A.GetNrows(); i++) {
98  for (Int_t j = 0; j < A.GetNcols(); j++) {
99  B(0, j) += A(i, j);
100  }
101  }
102 }
103 
104 } // namespace DNN
105 } // namespace TMVA
static double B[]
static long int sum(long int i)
Definition: Factory.cxx:2173
Int_t GetNcols() const
Definition: TMatrixTBase.h:125
int Int_t
Definition: RtypesCore.h:41
static double A[]
double beta(double x, double y)
Calculates the beta function.
Int_t GetNoElements() const
Definition: TMatrixTBase.h:126
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
static void Copy(TMatrixT< Scalar_t > &A, const TMatrixT< Scalar_t > &B)
Definition: Propagation.cxx:88
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 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:42
Int_t GetNrows() const
Definition: TMatrixTBase.h:122
static void SumColumns(TMatrixT< AReal > &B, const TMatrixT< AReal > &A)
Sum columns of (m x n) matrixx A and write the results into the first m elements in A...
Definition: Propagation.cxx:94
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:78
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
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
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:32