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 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 Real_t>
26  const TMatrixT<Real_t> &output)
27 {
28  size_t m,n;
29  m = Y.GetNrows();
30  n = Y.GetNcols();
31  Real_t result = 0.0;
32 
33  for (size_t i = 0; i < m; i++) {
34  for (size_t j = 0; j < n; j++) {
35  Real_t dY = (Y(i,j) - output(i,j));
36  result += dY * dY;
37  }
38  }
39  result /= (Real_t) (m * n);
40  return result;
41 }
42 
43 //______________________________________________________________________________
44 template<typename Real_t>
46  const TMatrixT<Real_t> & Y,
47  const TMatrixT<Real_t> & 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 / ((Real_t) (m*n));
55 }
56 
57 //______________________________________________________________________________
58 template<typename Real_t>
60  const TMatrixT<Real_t> &output)
61 {
62  size_t m,n;
63  m = Y.GetNrows();
64  n = Y.GetNcols();
65  Real_t result = 0.0;
66 
67  for (size_t i = 0; i < m; i++) {
68  for (size_t j = 0; j < n; j++) {
69  Real_t 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 /= - (Real_t) (m * n);
75  return result;
76 }
77 
78 //______________________________________________________________________________
79 template<typename Real_t>
81  const TMatrixT<Real_t> & Y,
82  const TMatrixT<Real_t> & output)
83 {
84  size_t m,n;
85  m = Y.GetNrows();
86  n = Y.GetNcols();
87 
88  Real_t norm = 1.0 / ((Real_t) (m * n));
89  for (size_t i = 0; i < m; i++)
90  {
91  for (size_t j = 0; j < n; j++)
92  {
93  Real_t y = Y(i,j);
94  Real_t sig = 1.0 / (1.0 + std::exp(-output(i,j)));
95  dY(i,j) = norm * (sig - y);
96  }
97  }
98 }
99 
100 } // namespace DNN
101 } // namespace TMVA
static Real_t CrossEntropy(const TMatrixT< Real_t > &Y, const TMatrixT< Real_t > &output)
Sigmoid transformation is implicitly applied, thus output should hold the linear activations of the l...
static Real_t MeanSquaredError(const TMatrixT< Real_t > &Y, const TMatrixT< Real_t > &output)
static void MeanSquaredErrorGradients(TMatrixT< Real_t > &dY, const TMatrixT< Real_t > &Y, const TMatrixT< Real_t > &output)
TMatrixT.
Definition: TMatrixDfwd.h:24
static void CrossEntropyGradients(TMatrixT< Real_t > &dY, const TMatrixT< Real_t > &Y, const TMatrixT< Real_t > &output)
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
Int_t GetNrows() const
Definition: TMatrixTBase.h:134
TMarker * m
Definition: textangle.C:8
Int_t GetNcols() const
Definition: TMatrixTBase.h:137
Double_t y[n]
Definition: legend1.C:17
float Real_t
Definition: RtypesCore.h:64
Abstract ClassifierFactory template that handles arbitrary types.
double result[121]
double exp(double)
static void output(int code)
Definition: gifencode.c:226
double norm(double *x, double *p)
Definition: unuranDistr.cxx:40
const Int_t n
Definition: legend1.C:16
double log(double)