Logo ROOT   6.07/09
Reference Guide
LossFunctions.cxx
Go to the documentation of this file.
1 // @(#)root/tmva/tmva/dnn:$Id$
2 // Author: Simon Pfreundschuh 20/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 loss functions for the multi-threaded CPU //
14  // implementation using Roots TThreadExecutor and BLAS. //
15  /////////////////////////////////////////////////////////////////////
16 
18 
19 namespace TMVA
20 {
21 namespace DNN
22 {
23 
24 //______________________________________________________________________________
25 template<typename AFloat>
28 {
29  const AFloat *dataY = Y.GetRawDataPointer();
30  const AFloat *dataOutput = output.GetRawDataPointer();
31  std::vector<AFloat> temp(Y.GetNElements());
32  AFloat norm = 1.0 / ((AFloat) Y.GetNrows() * Y.GetNcols());
33 
34  auto f = [&dataY, &dataOutput, &temp](UInt_t workerID)
35  {
36  AFloat dy = dataY[workerID] - dataOutput[workerID];
37  temp[workerID] = dy * dy;
38  return 0;
39  };
40 
41  auto reduction = [](AFloat sum1, AFloat sum2)
42  {
43  return sum1 + sum2;
44  };
45 
47  return norm * Y.GetThreadExecutor().Reduce(temp, reduction);
48 }
49 
50 //______________________________________________________________________________
51 template<typename AFloat>
53  TCpuMatrix<AFloat> & dY,
54  const TCpuMatrix<AFloat> & Y,
55  const TCpuMatrix<AFloat> & output)
56 {
57 
58  AFloat *dataDY = dY.GetRawDataPointer();
59  const AFloat *dataY = Y.GetRawDataPointer();
60  const AFloat *dataOutput = output.GetRawDataPointer();
61  AFloat norm = 1.0 / ((AFloat) Y.GetNrows() * Y.GetNcols());
62 
63  auto f = [&dataDY, &dataY, &dataOutput, norm](UInt_t workerID)
64  {
65  dataDY[workerID] = - 2.0 * norm * (dataY[workerID] - dataOutput[workerID]);
66  return 0;
67  };
68 
70 }
71 
72 //______________________________________________________________________________
73 template<typename AFloat>
76 {
77  const AFloat *dataY = Y.GetRawDataPointer();
78  const AFloat *dataOutput = output.GetRawDataPointer();
79  std::vector<AFloat> temp(Y.GetNElements());
80  AFloat norm = 1.0 / ((AFloat) Y.GetNrows() * Y.GetNcols());
81 
82  auto f = [&dataY, &dataOutput, &temp](UInt_t workerID)
83  {
84  AFloat y = dataY[workerID];
85  AFloat sig = 1.0 / (1.0 + exp(- dataOutput[workerID]));
86  temp[workerID] = - (y * log(sig) + (1.0 - y) * log(1.0 - sig));
87  return 0;
88  };
89 
90  auto reduction = [](AFloat sum1, AFloat sum2)
91  {
92  return sum1 + sum2;
93  };
94 
96  return norm * Y.GetThreadExecutor().Reduce(temp, reduction);
97 }
98 
99 //______________________________________________________________________________
100 template<typename AFloat>
102  TCpuMatrix<AFloat> & dY,
103  const TCpuMatrix<AFloat> & Y,
104  const TCpuMatrix<AFloat> & output)
105 {
106  AFloat *dataDY = dY.GetRawDataPointer();
107  const AFloat *dataY = Y.GetRawDataPointer();
108  const AFloat *dataOutput = output.GetRawDataPointer();
109  AFloat norm = 1.0 / ((AFloat) Y.GetNrows() * Y.GetNcols());
110 
111  auto f = [&dataDY, &dataY, &dataOutput, norm](UInt_t workerID)
112  {
113  AFloat y = dataY[workerID];
114  AFloat sig = 1.0 / (1.0 + exp(- dataOutput[workerID]));
115  dataDY[workerID] = norm * (sig - y);
116  return 0;
117  };
118 
120 }
121 
122 } // namespace DNN
123 } // namespace TMVA
The TCpuMatrix class.
Definition: CpuMatrix.h:46
static Scalar_t MeanSquaredError(const TCpuMatrix< Scalar_t > &Y, const TCpuMatrix< Scalar_t > &output)
ROOT::TThreadExecutor & GetThreadExecutor() const
Definition: CpuMatrix.h:106
size_t GetNrows() const
Definition: CpuMatrix.h:93
static void CrossEntropyGradients(TCpuMatrix< Scalar_t > &dY, const TCpuMatrix< Scalar_t > &Y, const TCpuMatrix< Scalar_t > &output)
unsigned int UInt_t
Definition: RtypesCore.h:42
static void MeanSquaredErrorGradients(TCpuMatrix< Scalar_t > &dY, const TCpuMatrix< Scalar_t > &Y, const TCpuMatrix< Scalar_t > &output)
AFloat * GetRawDataPointer()
Return raw pointer to the elements stored contiguously in column-major order.
Definition: CpuMatrix.h:103
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
Double_t y[n]
Definition: legend1.C:17
Abstract ClassifierFactory template that handles arbitrary types.
size_t GetNcols() const
Definition: CpuMatrix.h:94
double exp(double)
static void output(int code)
Definition: gifencode.c:226
double norm(double *x, double *p)
Definition: unuranDistr.cxx:40
static Scalar_t CrossEntropy(const TCpuMatrix< Scalar_t > &Y, const TCpuMatrix< Scalar_t > &output)
Sigmoid transformation is implicitly applied, thus output should hold the linear activations of the l...
double log(double)