Logo ROOT   6.10/09
Reference Guide
Propagation.cxx
Go to the documentation of this file.
1 // @(#)root/tmva/tmva/dnn:$Id$
2 // Author: Simon Pfreundschuh 10/07/16
3 
4 /*************************************************************************
5  * Copyright (C) 2016, Simon Pfreundschuh *
6  * All rights reserved. *
7  * *
8  * For the licensing terms see $ROOTSYS/LICENSE. *
9  * For the list of contributors see $ROOTSYS/README/CREDITS. *
10  *************************************************************************/
11 
12 //////////////////////////////////////////////////////////////////////
13 // Implementation of the functions required for the forward and //
14 // backward propagation of activations through a neural network for //
15 // the reference implementation. //
16 //////////////////////////////////////////////////////////////////////
17 
20 
21 namespace TMVA
22 {
23 namespace DNN
24 {
25 
26 template<typename AFloat>
28  const TCpuMatrix<AFloat> &input,
29  const TCpuMatrix<AFloat> &Weights)
30 {
31  int m = (int) input.GetNrows();
32  int k = (int) input.GetNcols();
33  int n = (int) Weights.GetNrows();
34 
35  char transa = 'N';
36  char transb = 'T';
37 
38  AFloat alpha = 1.0;
39  AFloat beta = 0.0;
40 
41  const AFloat *A = input.GetRawDataPointer();
42  const AFloat *B = Weights.GetRawDataPointer();
43  AFloat *C = output.GetRawDataPointer();
44 
45  ::TMVA::DNN::Blas::Gemm(&transa, &transb, &m, &n, &k, &alpha,
46  A, &m, B, &n, &beta, C, &m);
47 }
48 
49 template<typename AFloat>
52  const TCpuMatrix<AFloat> &biases)
53 {
54  int m = (int) output.GetNrows();
55  int n = (int) output.GetNcols();
56 
57  int inc = 1.0;
58  AFloat alpha = 1.0;
59 
60  AFloat * A = output.GetRawDataPointer();
61  const AFloat * x = TCpuMatrix<AFloat>::GetOnePointer();
62  const AFloat * y = biases.GetRawDataPointer();
63 
64  ::TMVA::DNN::Blas::Ger(&m, &n, &alpha, x, &inc, y, &inc, A, &m);
65 }
66 
67 template<typename AFloat>
69  TCpuMatrix<AFloat> & activationGradientsBackward,
70  TCpuMatrix<AFloat> & weightGradients,
71  TCpuMatrix<AFloat> & biasGradients,
72  TCpuMatrix<AFloat> & df,
73  const TCpuMatrix<AFloat> & activationGradients,
74  const TCpuMatrix<AFloat> & weights,
75  const TCpuMatrix<AFloat> & activationsBackward)
76 {
77  // Compute element-wise product.
78  Hadamard(df, activationGradients);
79 
80  // Activation gradients.
81  if (activationGradientsBackward.GetNElements() > 0)
82  Multiply(activationGradientsBackward, df, weights);
83 
84  // Weight gradients.
85  if (weightGradients.GetNElements() > 0)
86  TransposeMultiply(weightGradients, df, activationsBackward);
87 
88  // Bias gradients.
89  if (biasGradients.GetNElements() > 0)
90  SumColumns(biasGradients, df);
91 }
92 
93 } // namespace DNN
94 } // namespace TMVA
static double B[]
The TCpuMatrix class.
Definition: CpuMatrix.h:46
static void MultiplyTranspose(TCpuMatrix< Scalar_t > &output, const TCpuMatrix< Scalar_t > &input, const TCpuMatrix< Scalar_t > &weights)
Matrix-multiply input with the transpose of and write the results into output.
Definition: Propagation.cxx:27
void Ger(const int *m, const int *n, const Real_t *alpha, const Real_t *x, const int *incx, const Real_t *y, const int *incy, Real_t *A, const int *lda)
Add the outer product of x and y to the matrix A.
size_t GetNcols() const
Definition: CpuMatrix.h:94
static double A[]
double beta(double x, double y)
Calculates the beta function.
static void AddRowWise(TCpuMatrix< Scalar_t > &output, const TCpuMatrix< Scalar_t > &biases)
Add the vectors biases row-wise to the matrix output.
Definition: Propagation.cxx:50
size_t GetNElements() const
Definition: CpuMatrix.h:95
void Gemm(const char *transa, const char *transb, const int *m, const int *n, const int *k, const Real_t *alpha, const Real_t *A, const int *lda, const Real_t *B, const int *ldb, const Real_t *beta, Real_t *C, const int *ldc)
Multiply the matrix A with the matrix B and store the result in C.
Double_t x[n]
Definition: legend1.C:17
static void Backward(TCpuMatrix< Scalar_t > &activationGradientsBackward, TCpuMatrix< Scalar_t > &weightGradients, TCpuMatrix< Scalar_t > &biasGradients, TCpuMatrix< Scalar_t > &df, const TCpuMatrix< Scalar_t > &activationGradients, const TCpuMatrix< Scalar_t > &weights, const TCpuMatrix< Scalar_t > &activationBackward)
Perform the complete backward propagation step.
Definition: Propagation.cxx:68
static double C[]
TMarker * m
Definition: textangle.C:8
AFloat * GetRawDataPointer()
Return raw pointer to the elements stored contiguously in column-major order.
Definition: CpuMatrix.h:103
Double_t y[n]
Definition: legend1.C:17
static const AFloat * GetOnePointer()
Returns pointer to a vector holding only ones with a guaranteed length of the number of columns of ev...
Definition: CpuMatrix.h:62
Abstract ClassifierFactory template that handles arbitrary types.
size_t GetNrows() const
Definition: CpuMatrix.h:93
const Int_t n
Definition: legend1.C:16