Logo ROOT   6.12/07
Reference Guide
Regularization.cxx
Go to the documentation of this file.
1 // @(#)root/tmva/tmva/dnn:$Id$
2 // Author: Simon Pfreundschuh 21/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 regularization functionals and gradients //
14 // for the multi-threaded CPU implementation using Roots TThreadExecutor. //
15 ///////////////////////////////////////////////////////////////////////
16 
18 
19 namespace TMVA
20 {
21 namespace DNN
22 {
23 
24 //______________________________________________________________________________
25 template<typename AFloat>
27 {
28  const AFloat *data = Weights.GetRawDataPointer();
29  std::vector<AFloat> temp(Weights.GetNElements());
30 
31  auto f = [&data, &temp](UInt_t workerID)
32  {
33  temp[workerID] = fabs(data[workerID]);
34  return 0;
35  };
36 
37  auto reduction = [](const std::vector<AFloat> & v )
38  {
39  return std::accumulate(v.begin(),v.end(),AFloat{});
40  };
41  // auto reduction = [](AFloat sum1, AFloat sum2)
42  // {
43  // return sum1 + sum2;
44  // };
45 
46  Weights.GetThreadExecutor().Map(f, ROOT::TSeqI(Weights.GetNElements()));
47  return Weights.GetThreadExecutor().Reduce(temp, reduction);
48 }
49 
50 //______________________________________________________________________________
51 template<typename AFloat>
54  const TCpuMatrix<AFloat> & A,
55  AFloat weightDecay)
56 {
57  AFloat *dataB = B.GetRawDataPointer();
58  const AFloat *dataA = A.GetRawDataPointer();
59 
60  auto f = [&dataA, &dataB, weightDecay](UInt_t workerID)
61  {
62  AFloat sign = (dataA[workerID] < 0.0) ? -1.0 : 1.0;
63  dataB[workerID] += weightDecay * sign;
64  return 0;
65  };
66 
68 }
69 
70 //______________________________________________________________________________
71 template<typename AFloat>
73 {
74  const AFloat *data = Weights.GetRawDataPointer();
75  std::vector<AFloat> temp(Weights.GetNElements());
76 
77  auto f = [&data, &temp](UInt_t workerID)
78  {
79  temp[workerID] = data[workerID] * data[workerID];
80  return 0;
81  };
82 
83  auto reduction = [](const std::vector<AFloat> & v )
84  {
85  return std::accumulate(v.begin(),v.end(),AFloat{});
86  };
87  // auto reduction = [](AFloat sum1, AFloat sum2)
88  // {
89  // return sum1 + sum2;
90  // };
91 
92  Weights.GetThreadExecutor().Map(f, ROOT::TSeqI(Weights.GetNElements()));
93  return Weights.GetThreadExecutor().Reduce(temp, reduction);
94 }
95 
96 //______________________________________________________________________________
97 template<typename AFloat>
100  const TCpuMatrix<AFloat> & A,
101  AFloat weightDecay)
102 {
103  AFloat *dataB = B.GetRawDataPointer();
104  const AFloat *dataA = A.GetRawDataPointer();
105 
106  auto f = [&dataA, &dataB, weightDecay](UInt_t workerID)
107  {
108  dataB[workerID] += 2.0 * weightDecay * dataA[workerID];
109  return 0;
110  };
111 
113 }
114 
115 } // namespace DNN
116 } // namespace TMVA
static double B[]
The TCpuMatrix class.
Definition: CpuMatrix.h:46
static double A[]
size_t GetNElements() const
Definition: CpuMatrix.h:94
static Scalar_t L1Regularization(const TCpuMatrix< Scalar_t > &W)
double weightDecay(double error, ItWeight itWeight, ItWeight itWeightEnd, double factorWeightDecay, EnumRegularization eRegularization)
compute the weight decay for regularization (L1 or L2)
Definition: NeuralNet.icc:497
auto Reduce(const std::vector< T > &objs, BINARYOP redfunc) -> decltype(redfunc(objs.front(), objs.front()))
"Reduce" an std::vector into a single object in parallel by passing a binary operator as the second a...
static void AddL1RegularizationGradients(TCpuMatrix< Scalar_t > &A, const TCpuMatrix< Scalar_t > &W, Scalar_t weightDecay)
VecExpr< UnaryOp< Fabs< T >, VecExpr< A, T, D >, T >, T, D > fabs(const VecExpr< A, T, D > &rhs)
SVector< double, 2 > v
Definition: Dict.h:5
unsigned int UInt_t
Definition: RtypesCore.h:42
AFloat * GetRawDataPointer()
Return raw pointer to the elements stored contiguously in column-major order.
Definition: CpuMatrix.h:102
static void AddL2RegularizationGradients(TCpuMatrix< Scalar_t > &A, const TCpuMatrix< Scalar_t > &W, Scalar_t weightDecay)
A pseudo container class which is a generator of indices.
Definition: TSeq.hxx:66
static Scalar_t L2Regularization(const TCpuMatrix< Scalar_t > &W)
ROOT::TThreadExecutor & GetThreadExecutor() const
Definition: CpuMatrix.h:105
Abstract ClassifierFactory template that handles arbitrary types.
auto Map(F func, unsigned nTimes) -> std::vector< typename std::result_of< F()>::type >
Execute func (with no arguments) nTimes in parallel.