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