Logo ROOT   6.07/09
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 = [](AFloat sum1, AFloat sum2)
38  {
39  return sum1 + sum2;
40  };
41 
42  Weights.GetThreadExecutor().Map(f, ROOT::TSeqI(Weights.GetNElements()));
43  return Weights.GetThreadExecutor().Reduce(temp, reduction);
44 }
45 
46 //______________________________________________________________________________
47 template<typename AFloat>
50  const TCpuMatrix<AFloat> & A,
51  AFloat weightDecay)
52 {
53  AFloat *dataB = B.GetRawDataPointer();
54  const AFloat *dataA = A.GetRawDataPointer();
55 
56  auto f = [&dataA, &dataB, weightDecay](UInt_t workerID)
57  {
58  AFloat sign = (dataA[workerID] < 0.0) ? -1.0 : 1.0;
59  dataB[workerID] += weightDecay * sign;
60  return 0;
61  };
62 
64 }
65 
66 //______________________________________________________________________________
67 template<typename AFloat>
69 {
70  const AFloat *data = Weights.GetRawDataPointer();
71  std::vector<AFloat> temp(Weights.GetNElements());
72 
73  auto f = [&data, &temp](UInt_t workerID)
74  {
75  temp[workerID] = data[workerID] * data[workerID];
76  return 0;
77  };
78 
79  auto reduction = [](AFloat sum1, AFloat sum2)
80  {
81  return sum1 + sum2;
82  };
83 
84  Weights.GetThreadExecutor().Map(f, ROOT::TSeqI(Weights.GetNElements()));
85  return Weights.GetThreadExecutor().Reduce(temp, reduction);
86 }
87 
88 //______________________________________________________________________________
89 template<typename AFloat>
92  const TCpuMatrix<AFloat> & A,
93  AFloat weightDecay)
94 {
95  AFloat *dataB = B.GetRawDataPointer();
96  const AFloat *dataA = A.GetRawDataPointer();
97 
98  auto f = [&dataA, &dataB, weightDecay](UInt_t workerID)
99  {
100  dataB[workerID] += 2.0 * weightDecay * dataA[workerID];
101  return 0;
102  };
103 
105 }
106 
107 } // namespace DNN
108 } // namespace TMVA
static double B[]
The TCpuMatrix class.
Definition: CpuMatrix.h:46
ROOT::TThreadExecutor & GetThreadExecutor() const
Definition: CpuMatrix.h:106
static double A[]
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:491
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)
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:103
static void AddL2RegularizationGradients(TCpuMatrix< Scalar_t > &A, const TCpuMatrix< Scalar_t > &W, Scalar_t weightDecay)
size_t GetNElements() const
Definition: CpuMatrix.h:95
double f(double x)
A pseudo container class which is a generator of indices.
Definition: TSeq.hxx:66
static Scalar_t L2Regularization(const TCpuMatrix< Scalar_t > &W)
Abstract ClassifierFactory template that handles arbitrary types.