Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
LossFunctions.hxx
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
19
20namespace TMVA
21{
22namespace DNN
23{
24
25//______________________________________________________________________________
26template <typename AFloat>
28 const TCpuMatrix<AFloat> &weights)
29{
30 const AFloat *dataY = Y.GetRawDataPointer();
31 const AFloat *dataOutput = output.GetRawDataPointer();
32 const AFloat *dataWeights = weights.GetRawDataPointer();
33 std::vector<AFloat> temp(Y.GetNoElements());
34 size_t m = Y.GetNrows();
35 AFloat norm = 1.0 / ((AFloat) Y.GetNrows() * Y.GetNcols());
36
37 auto f = [&dataY, &dataOutput, &dataWeights, &temp, m](UInt_t workerID) {
38 AFloat dy = dataY[workerID] - dataOutput[workerID];
39 temp[workerID] = dataWeights[workerID % m] * dy * dy;
40 return 0;
41 };
42
43 auto reduction = [](const std::vector<AFloat> & v )
44 {
45 return std::accumulate(v.begin(),v.end(),AFloat{});
46 };
47
49 return norm * Y.GetThreadExecutor().Reduce(temp, reduction);
50}
51
52//______________________________________________________________________________
53template <typename AFloat>
55 const TCpuMatrix<AFloat> &output, const TCpuMatrix<AFloat> &weights)
56{
57
58 AFloat *dataDY = dY.GetRawDataPointer();
59 const AFloat *dataY = Y.GetRawDataPointer();
60 const AFloat *dataOutput = output.GetRawDataPointer();
61 const AFloat *dataWeights = weights.GetRawDataPointer();
62
63 size_t m = Y.GetNrows();
64 AFloat norm = 1.0 / ((AFloat) Y.GetNrows() * Y.GetNcols());
65
66 auto f = [&dataDY, &dataY, &dataOutput, &dataWeights, m, norm](UInt_t workerID) {
67 dataDY[workerID] = -2.0 * norm * (dataY[workerID] - dataOutput[workerID]);
68 dataDY[workerID] *= dataWeights[workerID % m];
69 return 0;
70 };
71
73}
74
75//______________________________________________________________________________
76template <typename AFloat>
78 const TCpuMatrix<AFloat> &weights)
79{
80 const AFloat *dataY = Y.GetRawDataPointer();
81 const AFloat *dataOutput = output.GetRawDataPointer();
82 const AFloat *dataWeights = weights.GetRawDataPointer();
83 std::vector<AFloat> temp(Y.GetNoElements());
84
85 size_t m = Y.GetNrows();
86 AFloat norm = 1.0 / ((AFloat) Y.GetNrows() * Y.GetNcols());
87
88 auto f = [&dataY, &dataOutput, &dataWeights, &temp, m](UInt_t workerID) {
89 AFloat y = dataY[workerID];
90 // AFloat sig = 1.0 / (1.0 + exp(- dataOutput[workerID]));
91 // Use more robust formula to compute log(sig) and log(1-sig) where sig= 1./(1+exp(-x))
92 // when sig is close to zero or to 1
93 AFloat x = dataOutput[workerID];
94 AFloat lr = 0;
95 if (x < -75.)
96 lr = -x ;
97 else if (x > 75)
98 lr = exp(-x);
99 else
100 lr = std::log(1. + exp(-x));
101
102 //temp[workerID] = - (y * log(sig) + (1.0 - y) * log(1.0 - sig));
103 temp[workerID] = y * lr + (1.0 - y) * (x +lr);
104
105 temp[workerID] *= dataWeights[workerID % m];
106 return 0;
107 };
108
109 auto reduction = [](const std::vector<AFloat> & v )
110 {
111 return std::accumulate(v.begin(),v.end(),AFloat{});
112 };
113
115 return norm * Y.GetThreadExecutor().Reduce(temp, reduction);
116}
117
118//______________________________________________________________________________
119template <typename AFloat>
121 const TCpuMatrix<AFloat> &output, const TCpuMatrix<AFloat> &weights)
122{
123 AFloat *dataDY = dY.GetRawDataPointer();
124 const AFloat *dataY = Y.GetRawDataPointer();
125 const AFloat *dataOutput = output.GetRawDataPointer();
126 const AFloat *dataWeights = weights.GetRawDataPointer();
127
128 size_t m = Y.GetNrows();
129 AFloat norm = 1.0 / ((AFloat) Y.GetNrows() * Y.GetNcols());
130
131 auto f = [&dataDY, &dataY, &dataOutput, &dataWeights, m, norm](UInt_t workerID) {
132 AFloat y = dataY[workerID];
133 AFloat sig = 1.0 / (1.0 + exp(- dataOutput[workerID]));
134 dataDY[workerID] = norm * (sig - y);
135 dataDY[workerID] *= dataWeights[workerID % m];
136 return 0;
137 };
138
140}
141
142//______________________________________________________________________________
143template <typename AFloat>
145 const TCpuMatrix<AFloat> &weights)
146{
147 const AFloat *dataY = Y.GetRawDataPointer();
148 const AFloat *dataOutput = output.GetRawDataPointer();
149 const AFloat *dataWeights = weights.GetRawDataPointer();
150
151 std::vector<AFloat> temp(Y.GetNrows());
152 size_t m = Y.GetNrows();
153 size_t n = Y.GetNcols();
154 AFloat norm = 1.0 / ((AFloat) m);
155
156 auto f = [&dataY, &dataOutput, &dataWeights, &temp, n, m](UInt_t workerID) {
157 AFloat sum = 0.0;
158 for (size_t j = 0; j < n; j++) {
159 sum += exp(dataOutput[workerID + j * m]);
160 }
161 for (size_t j = 0; j < n; j++) {
162 temp[workerID] -=
163 dataY[workerID + j * m] * log(exp(dataOutput[workerID + j * m]) / sum);
164 }
165 temp[workerID] *= dataWeights[workerID];
166 return 0;
167 };
168
169 auto reduction = [](const std::vector<AFloat> & v )
170 {
171 return std::accumulate(v.begin(),v.end(),AFloat{});
172 };
173
175 return norm * Y.GetThreadExecutor().Reduce(temp, reduction);
176}
177
178//______________________________________________________________________________
179template <typename AFloat>
181 const TCpuMatrix<AFloat> &output, const TCpuMatrix<AFloat> &weights)
182{
183 AFloat *dataDY = dY.GetRawDataPointer();
184 const AFloat *dataY = Y.GetRawDataPointer();
185 const AFloat *dataOutput = output.GetRawDataPointer();
186 const AFloat *dataWeights = weights.GetRawDataPointer();
187
188 size_t m = Y.GetNrows();
189 size_t n = Y.GetNcols();
190 AFloat norm = 1.0 / ((AFloat) m);
191
192 auto f = [&dataDY, &dataY, &dataOutput, &dataWeights, norm, n, m](UInt_t workerID) {
193 AFloat sum = 0.0;
194 AFloat sumY = 0.0;
195 AFloat weight = dataWeights[workerID];
196 for (size_t j = 0; j < n; j++) {
197 sum += exp(dataOutput[workerID + j * m]);
198 sumY += dataY[workerID + j * m];
199 }
200 for (size_t j = 0; j < n; j++) {
201 dataDY[workerID + j * m] =
202 norm * (exp(dataOutput[workerID + j * m]) / sum * sumY - dataY[workerID + j * m]);
203 dataDY[workerID + j * m] *= weight;
204 }
205 return 0;
206 };
207
209}
210
211} // namespace DNN
212} // namespace TMVA
#define f(i)
Definition RSha256.hxx:104
A pseudo container class which is a generator of indices.
Definition TSeq.hxx:67
The TCpuMatrix class.
Definition CpuMatrix.h:86
size_t GetNcols() const
Definition CpuMatrix.h:156
AFloat * GetRawDataPointer()
Return raw pointer to the elements stored contiguously in column-major order.
Definition CpuMatrix.h:166
size_t GetNrows() const
Definition CpuMatrix.h:155
static Executor & GetThreadExecutor()
Definition CpuMatrix.h:169
size_t GetNoElements() const
Definition CpuMatrix.h:157
static void SoftmaxCrossEntropyGradients(Matrix_t &dY, const Matrix_t &Y, const Matrix_t &output, const Matrix_t &weights)
static void CrossEntropyGradients(Matrix_t &dY, const Matrix_t &Y, const Matrix_t &output, const Matrix_t &weights)
static void MeanSquaredErrorGradients(Matrix_t &dY, const Matrix_t &Y, const Matrix_t &output, const Matrix_t &weights)
static Scalar_t MeanSquaredError(const Matrix_t &Y, const Matrix_t &output, const Matrix_t &weights)
static Scalar_t CrossEntropy(const Matrix_t &Y, const Matrix_t &output, const Matrix_t &weights)
Sigmoid transformation is implicitly applied, thus output should hold the linear activations of the l...
static Scalar_t SoftmaxCrossEntropy(const Matrix_t &Y, const Matrix_t &output, const Matrix_t &weights)
Softmax transformation is implicitly applied, thus output should hold the linear activations of the l...
auto Map(F func, unsigned nTimes) -> std::vector< InvokeResult_t< F > >
Wrap TExecutor::Map functions.
Definition Executor.h:140
auto Reduce(const std::vector< T > &objs, R redfunc) -> decltype(redfunc(objs))
Wrap Reduce function.
Definition Executor.h:168
Double_t y[n]
Definition legend1.C:17
Double_t x[n]
Definition legend1.C:17
const Int_t n
Definition legend1.C:16
create variable transformations
TMarker m
Definition textangle.C:8
static uint64_t sum(uint64_t i)
Definition Factory.cxx:2345
static void output()