Logo ROOT   6.08/07
Reference Guide
LossFunctions.cxx
Go to the documentation of this file.
1 // @(#)root/tmva/tmva/dnn:$Id$
2 // Author: Simon Pfreundschuh 10/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 reference //
14  // implementation. //
15  ////////////////////////////////////////////////////////////
16 
18 
19 namespace TMVA
20 {
21 namespace DNN
22 {
23 //______________________________________________________________________________
24 template<typename AReal>
26  const TMatrixT<AReal> &output)
27 {
28  size_t m,n;
29  m = Y.GetNrows();
30  n = Y.GetNcols();
31  AReal result = 0.0;
32 
33  for (size_t i = 0; i < m; i++) {
34  for (size_t j = 0; j < n; j++) {
35  AReal dY = (Y(i,j) - output(i,j));
36  result += dY * dY;
37  }
38  }
39  result /= (AReal) (m * n);
40  return result;
41 }
42 
43 //______________________________________________________________________________
44 template<typename AReal>
46  const TMatrixT<AReal> & Y,
47  const TMatrixT<AReal> & output)
48 {
49  size_t m,n;
50  m = Y.GetNrows();
51  n = Y.GetNcols();
52 
53  dY.Minus(Y, output);
54  dY *= - 2.0 / ((AReal) (m*n));
55 }
56 
57 //______________________________________________________________________________
58 template<typename AReal>
60  const TMatrixT<AReal> &output)
61 {
62  size_t m,n;
63  m = Y.GetNrows();
64  n = Y.GetNcols();
65  AReal result = 0.0;
66 
67  for (size_t i = 0; i < m; i++) {
68  for (size_t j = 0; j < n; j++) {
69  AReal sig = 1.0 / (1.0 + std::exp(-output(i,j)));
70  result += Y(i,j) * std::log(sig)
71  + (1.0 - Y(i,j)) * std::log(1.0 - sig);
72  }
73  }
74  result /= - (AReal) (m * n);
75  return result;
76 }
77 
78 //______________________________________________________________________________
79 template<typename AReal>
81  const TMatrixT<AReal> & Y,
82  const TMatrixT<AReal> & output)
83 {
84  size_t m,n;
85  m = Y.GetNrows();
86  n = Y.GetNcols();
87 
88  AReal norm = 1.0 / ((AReal) (m * n));
89  for (size_t i = 0; i < m; i++)
90  {
91  for (size_t j = 0; j < n; j++)
92  {
93  AReal y = Y(i,j);
94  AReal sig = 1.0 / (1.0 + std::exp(-output(i,j)));
95  dY(i,j) = norm * (sig - y);
96  }
97  }
98 }
99 
100 //______________________________________________________________________________
101 template<typename AReal>
103  const TMatrixT<AReal> &output)
104 {
105  size_t m,n;
106  m = Y.GetNrows();
107  n = Y.GetNcols();
108  AReal result = 0.0;
109 
110  for (size_t i = 0; i < m; i++) {
111  AReal sum = 0.0;
112  for (size_t j = 0; j < n; j++) {
113  sum += exp(output(i,j));
114  }
115  for (size_t j = 0; j < n; j++) {
116  result += Y(i,j) * log(exp(output(i,j)) / sum);
117  }
118  }
119  result /= - m;
120  return result;
121 }
122 
123 //______________________________________________________________________________
124 template<typename AReal>
126  const TMatrixT<AReal> & Y,
127  const TMatrixT<AReal> & output)
128 {
129  size_t m,n;
130  m = Y.GetNrows();
131  n = Y.GetNcols();
132  AReal norm = 1.0 / m ;
133 
134  for (size_t i = 0; i < m; i++)
135  {
136  AReal sum = 0.0;
137  AReal sumY = 0.0;
138  for (size_t j = 0; j < n; j++) {
139  sum += exp(output(i,j));
140  sumY += Y(i,j);
141  }
142  for (size_t j = 0; j < n; j++) {
143  dY(i,j) = norm * (exp(output(i,j)) / sum * sumY - Y(i,j));
144  }
145  }
146 }
147 
148 } // namespace DNN
149 } // namespace TMVA
static void MeanSquaredErrorGradients(TMatrixT< AReal > &dY, const TMatrixT< AReal > &Y, const TMatrixT< AReal > &output)
static long int sum(long int i)
Definition: Factory.cxx:1786
static void SoftmaxCrossEntropyGradients(TMatrixT< AReal > &dY, const TMatrixT< AReal > &Y, const TMatrixT< AReal > &output)
static void CrossEntropyGradients(TMatrixT< AReal > &dY, const TMatrixT< AReal > &Y, const TMatrixT< AReal > &output)
Int_t GetNcols() const
Definition: TMatrixTBase.h:137
TMatrixT.
Definition: TMatrixDfwd.h:24
void Minus(const TMatrixT< Element > &a, const TMatrixT< Element > &b)
General matrix summation. Create a matrix C such that C = A - B.
Definition: TMatrixT.cxx:580
static AReal SoftmaxCrossEntropy(const TMatrixT< AReal > &Y, const TMatrixT< AReal > &output)
Softmax transformation is implicitly applied, thus output should hold the linear activations of the l...
TMarker * m
Definition: textangle.C:8
Int_t GetNrows() const
Definition: TMatrixTBase.h:134
static AReal CrossEntropy(const TMatrixT< AReal > &Y, const TMatrixT< AReal > &output)
Sigmoid transformation is implicitly applied, thus output should hold the linear activations of the l...
Double_t y[n]
Definition: legend1.C:17
Abstract ClassifierFactory template that handles arbitrary types.
static AReal MeanSquaredError(const TMatrixT< AReal > &Y, const TMatrixT< AReal > &output)
double result[121]
double exp(double)
double norm(double *x, double *p)
Definition: unuranDistr.cxx:40
const Int_t n
Definition: legend1.C:16
double log(double)