Skip to content

Commit

Permalink
[TMVA] Add operations needed for performing optimizer updates:
Browse files Browse the repository at this point in the history
* Implement ConstAdd, ConstMult, ReciprocalElementWise, SquareElementWise, SqrtElementWise in CPU and Reference
architectures.

* Add Unit Tests for them in CPU architecture.

* Add ROOT Style docs and clang format the code.
  • Loading branch information
ravikiran0606 committed Jul 15, 2018
1 parent 15ed1fb commit dc6f823
Show file tree
Hide file tree
Showing 8 changed files with 472 additions and 29 deletions.
25 changes: 25 additions & 0 deletions tmva/tmva/inc/TMVA/DNN/Architectures/Cpu.h
Expand Up @@ -479,6 +479,31 @@ class TCpu

/** Check two matrices for equality, taking floating point arithmetic errors into account. */
static bool AlmostEquals(const TCpuMatrix<Scalar_t> &A, const TCpuMatrix<Scalar_t> &B, double epsilon = 0.1);

/** Add the constant \p beta to all the elements of matrix \p A and write the
* result into \p A.
*/
static void ConstAdd(TCpuMatrix<Scalar_t> &A, Scalar_t beta);

/** Multiply the constant \p beta to all the elements of matrix \p A and write the
* result into \p A.
*/
static void ConstMult(TCpuMatrix<Scalar_t> &A, Scalar_t beta);

/** Reciprocal each element of the matrix \p A and write the result into
* \p A
*/
static void ReciprocalElementWise(TCpuMatrix<Scalar_t> &A);

/** Square each element of the matrix \p A and write the result into
* \p A
*/
static void SquareElementWise(TCpuMatrix<Scalar_t> &A);

/** Square root each element of the matrix \p A and write the result into
* \p A
*/
static void SqrtElementWise(TCpuMatrix<Scalar_t> &A);
};

//____________________________________________________________________________
Expand Down
30 changes: 30 additions & 0 deletions tmva/tmva/inc/TMVA/DNN/Architectures/Reference.h
Expand Up @@ -472,6 +472,36 @@ class TReference
*/
static void SumColumns(TMatrixT<AReal> &B, const TMatrixT<AReal> &A);

/** In-place Hadamard (element-wise) product of matrices \p A and \p B
* with the result being written into \p A.
*/
static void Hadamard(TMatrixT<AReal> &A, const TMatrixT<AReal> &B);

/** Add the constant \p beta to all the elements of matrix \p A and write the
* result into \p A.
*/
static void ConstAdd(TMatrixT<AReal> &A, AReal beta);

/** Multiply the constant \p beta to all the elements of matrix \p A and write the
* result into \p A.
*/
static void ConstMult(TMatrixT<AReal> &A, AReal beta);

/** Reciprocal each element of the matrix \p A and write the result into
* \p A
*/
static void ReciprocalElementWise(TMatrixT<AReal> &A);

/** Square each element of the matrix \p A and write the result into
* \p A
*/
static void SquareElementWise(TMatrixT<AReal> &A);

/** Square root each element of the matrix \p A and write the result into
* \p A
*/
static void SqrtElementWise(TMatrixT<AReal> &A);

//____________________________________________________________________________
//
// AutoEncoder Propagation
Expand Down
39 changes: 39 additions & 0 deletions tmva/tmva/src/DNN/Architectures/Cpu/Arithmetic.cxx
Expand Up @@ -199,6 +199,45 @@ void TCpu<Real_t>::Copy(std::vector<TCpuMatrix<Real_t>> &B,
}
}

//____________________________________________________________________________
template <typename Real_t>
void TCpu<Real_t>::ConstAdd(TCpuMatrix<Real_t> &A, Real_t beta)
{
auto f = [beta](Real_t x) { return x + beta; };
A.Map(f);
}

//____________________________________________________________________________
template <typename Real_t>
void TCpu<Real_t>::ConstMult(TCpuMatrix<Real_t> &A, Real_t beta)
{
auto f = [beta](Real_t x) { return x * beta; };
A.Map(f);
}

//____________________________________________________________________________
template <typename Real_t>
void TCpu<Real_t>::ReciprocalElementWise(TCpuMatrix<Real_t> &A)
{
auto f = [](Real_t x) { return 1.0 / x; };
A.Map(f);
}

//____________________________________________________________________________
template <typename Real_t>
void TCpu<Real_t>::SquareElementWise(TCpuMatrix<Real_t> &A)
{
auto f = [](Real_t x) { return x * x; };
A.Map(f);
}

//____________________________________________________________________________
template <typename Real_t>
void TCpu<Real_t>::SqrtElementWise(TCpuMatrix<Real_t> &A)
{
auto f = [](Real_t x) { return sqrt(x); };
A.Map(f);
}

} // DNN
} // TMVA
1 change: 1 addition & 0 deletions tmva/tmva/src/DNN/Architectures/Reference.cxx
Expand Up @@ -19,6 +19,7 @@

#include "Reference/Propagation.cxx"
#include "Reference/ActivationFunctions.cxx"
#include "Reference/Arithmetic.cxx"
#include "Reference/OutputFunctions.cxx"
#include "Reference/LossFunctions.cxx"
#include "Reference/Regularization.cxx"
Expand Down
102 changes: 102 additions & 0 deletions tmva/tmva/src/DNN/Architectures/Reference/Arithmetic.cxx
@@ -0,0 +1,102 @@
// @(#)root/tmva/tmva/dnn:$Id$
// Author: Ravi Kiran S

/*************************************************************************
* Copyright (C) 2018, Ravi Kiran S *
* All rights reserved. *
* *
* For the licensing terms see $ROOTSYS/LICENSE. *
* For the list of contributors see $ROOTSYS/README/CREDITS. *
*************************************************************************/

//////////////////////////////////////////////////////////////////
// Implementation of the Helper arithmetic functions for the //
// reference implementation. //
//////////////////////////////////////////////////////////////////

#include "TMVA/DNN/Architectures/Reference.h"
#include <math.h>

namespace TMVA {
namespace DNN {

//______________________________________________________________________________
template <typename AReal>
void TReference<AReal>::SumColumns(TMatrixT<AReal> &B, const TMatrixT<AReal> &A)
{
B = 0.0;
for (Int_t i = 0; i < A.GetNrows(); i++) {
for (Int_t j = 0; j < A.GetNcols(); j++) {
B(0, j) += A(i, j);
}
}
}

//______________________________________________________________________________
template <typename AReal>
void TReference<AReal>::Hadamard(TMatrixT<AReal> &A, const TMatrixT<AReal> &B)
{
for (Int_t i = 0; i < A.GetNrows(); i++) {
for (Int_t j = 0; j < A.GetNcols(); j++) {
A(i, j) *= B(i, j);
}
}
}

//______________________________________________________________________________
template <typename AReal>
void TReference<AReal>::ConstAdd(TMatrixT<AReal> &A, AReal beta)
{
for (Int_t i = 0; i < A.GetNrows(); i++) {
for (Int_t j = 0; j < A.GetNcols(); j++) {
A(i, j) += beta;
}
}
}

//______________________________________________________________________________
template <typename AReal>
void TReference<AReal>::ConstMult(TMatrixT<AReal> &A, AReal beta)
{
for (Int_t i = 0; i < A.GetNrows(); i++) {
for (Int_t j = 0; j < A.GetNcols(); j++) {
A(i, j) *= beta;
}
}
}

//______________________________________________________________________________
template <typename AReal>
void TReference<AReal>::ReciprocalElementWise(TMatrixT<AReal> &A)
{
for (Int_t i = 0; i < A.GetNrows(); i++) {
for (Int_t j = 0; j < A.GetNcols(); j++) {
A(i, j) = 1.0 / A(i, j);
}
}
}

//______________________________________________________________________________
template <typename AReal>
void TReference<AReal>::SquareElementWise(TMatrixT<AReal> &A)
{
for (Int_t i = 0; i < A.GetNrows(); i++) {
for (Int_t j = 0; j < A.GetNcols(); j++) {
A(i, j) *= A(i, j);
}
}
}

//______________________________________________________________________________
template <typename AReal>
void TReference<AReal>::SqrtElementWise(TMatrixT<AReal> &A)
{
for (Int_t i = 0; i < A.GetNrows(); i++) {
for (Int_t j = 0; j < A.GetNcols(); j++) {
A(i, j) = sqrt(A(i, j));
}
}
}

} // namespace DNN
} // namespace TMVA
11 changes: 0 additions & 11 deletions tmva/tmva/src/DNN/Architectures/Reference/Propagation.cxx
Expand Up @@ -104,17 +104,6 @@ void TReference<AReal>::Copy(std::vector<TMatrixT<AReal>> &A, const std::vector<
}
}

template <typename AReal>
void TReference<AReal>::SumColumns(TMatrixT<AReal> &B, const TMatrixT<AReal> &A)
{
B = 0.0;
for (Int_t i = 0; i < A.GetNrows(); i++) {
for (Int_t j = 0; j < A.GetNcols(); j++) {
B(0, j) += A(i, j);
}
}
}

//______________________________________________________________________________
template <typename AReal>
void TReference<AReal>::Im2col(TMatrixT<AReal> &A, TMatrixT<AReal> &B, size_t imgHeight, size_t imgWidth,
Expand Down

0 comments on commit dc6f823

Please sign in to comment.