Logo ROOT   6.16/01
Reference Guide
DenoisePropagation.cxx
Go to the documentation of this file.
1// @(#)root/tmva/tmva/dnn:$Id$
2// Author: Akshay Vashistha(ajatgd)
3
4/*************************************************************************
5 * Copyright (C) 2017 ajatgd *
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 Denoise Autoencoder functions for the //
14// reference implementation. //
15//////////////////////////////////////////////////////////////////
16
18
19#include <cmath>
20#include <cstdlib>
21#include <iostream>
22namespace TMVA
23{
24namespace DNN
25{
26
27//______________________________________________________________________________
28
29template<typename Real_t>
31 const TMatrixT<Real_t> &biases)
32{
33 size_t m,n;
34 m = A.GetNrows();
35 n= A.GetNcols();
36 for(size_t i = 0; i<m; i++)
37 {
38 for(size_t j=0; j<n; j++)
39 {
40 A(i, j) += biases(i, 0);
41 }
42 }
43}
44
45//______________________________________________________________________________
46
47template <typename Real_t>
50 TMatrixT<Real_t> &z, TMatrixT<Real_t> &fVBiases, TMatrixT<Real_t> &fHBiases,
51 TMatrixT<Real_t> &fWeights, TMatrixT<Real_t> &VBiasError,
52 TMatrixT<Real_t> &HBiasError, Real_t learningRate, size_t fBatchSize) {
53
54 //updating fVBiases
55 for (size_t i = 0; i < (size_t)fVBiases.GetNrows(); i++)
56 {
57 for (size_t j = 0; j < (size_t)fVBiases.GetNcols(); j++) {
58 VBiasError(i, j) = x(i, j) - z(i, j);
59 fVBiases(i, j) += learningRate * VBiasError(i, j) / fBatchSize;
60 }
61 }
62
63 //updating fHBiases
64 for (Int_t i = 0; i < fHBiases.GetNrows(); i++) {
65 HBiasError(i,0) = 0;
66 for (Int_t j = 0; j < fVBiases.GetNrows(); j++) {
67 HBiasError(i, 0) += fWeights(i, j) * VBiasError(j, 0);
68 }
69 HBiasError(i, 0) *= y(i, 0) * (1 - y(i, 0));
70 fHBiases(i, 0) += learningRate * HBiasError(i, 0) / fBatchSize;
71 }
72
73 //updating weights
74 for (Int_t i = 0; i < fHBiases.GetNrows(); i++) {
75 for (Int_t j = 0; j < fVBiases.GetNrows(); j++) {
76 fWeights(i, j) += learningRate * (HBiasError(i, 0) * tildeX(j, 0) +
77 VBiasError(j, 0) * y(i, 0)) / fBatchSize;
78 }
79 }
80}
81
82//______________________________________________________________________________
83
84template<typename Real_t>
86{
87 size_t m,n;
88 m = A.GetNrows();
89 n = A.GetNcols();
90
91 Real_t sum = 0.0;
92 for (size_t i = 0; i < m; i++) {
93 for (size_t j = 0; j < n; j++) {
94 sum += exp(A(i, j));
95 }
96 }
97
98 for (size_t i = 0; i < m; i++) {
99 for (size_t j = 0; j < n; j++) {
100 A(i, j) = exp(A(i, j)) / sum;
101 }
102 }
103}
104
105//______________________________________________________________________________
106
107template <typename Real_t>
109 TMatrixT<Real_t> &corruptedInput,
110 Real_t corruptionLevel) {
111 for(size_t i=0; i< (size_t)input.GetNrows(); i++)
112 {
113 for(size_t j=0; j<(size_t)input.GetNcols(); j++ )
114 {
115
116 if ((size_t)((rand() / (RAND_MAX + 1.0)) * 100) %
117 ((size_t)(corruptionLevel * 10)) ==
118 0)
119 {
120 corruptedInput(i, j) = 0;
121 }
122 else
123 {
124 corruptedInput(i, j) = input(i, j);
125 }
126 }
127 }
128}
129
130
131//______________________________________________________________________________
132
133template <typename Real_t>
135 TMatrixT<Real_t> &compressedInput,
136 TMatrixT<Real_t> &Weights) {
137
138 size_t m, a;
139 m = compressedInput.GetNrows();
140 a = input.GetNrows();
141
142 for (size_t i = 0; i < m; i++) {
143 compressedInput(i, 0) = 0;
144 for (size_t j = 0; j < a; j++) {
145 compressedInput(i, 0) =
146 compressedInput(i, 0) + (Weights(i, j) * input(j, 0));
147 }
148 }
149}
150//______________________________________________________________________________
151template <typename Real_t>
153 TMatrixT<Real_t> &reconstructedInput,
154 TMatrixT<Real_t> &fWeights) {
155 for (size_t i=0; i<(size_t)reconstructedInput.GetNrows(); i++)
156 {
157 reconstructedInput(i, 0) = 0;
158 for(size_t j=0; j<(size_t)compressedInput.GetNrows();j++)
159 {
160 reconstructedInput(i, 0) += fWeights(j, i) * compressedInput(j, 0);
161 }
162 }
163}
164
165//______________________________________________________________________________
166// Logistic Regression Layer Methods
167//
168//______________________________________________________________________________
169
170template<typename Real_t>
173 TMatrixT<Real_t> &fWeights)
174{
175 size_t m,n;
176 m = p.GetNrows();
177 n = input.GetNrows();
178 for(size_t i= 0; i < m; i++)
179 {
180 p(i, 0) = 0;
181 for(size_t j=0; j < n; j++)
182 {
183 p(i, 0) += fWeights(i, j) * input(j, 0);
184 }
185 }
186}
187
188//______________________________________________________________________________
189
190template<typename Real_t>
193 TMatrixT<Real_t> &difference,
195 TMatrixT<Real_t> &fWeights,
196 TMatrixT<Real_t> &fBiases,
197 Real_t learningRate,
198 size_t fBatchSize)
199{
200 size_t m,n;
201 m = p.GetNrows();
202 n = input.GetNrows();
203
204 for(size_t i= 0; i<m; i++)
205 {
206 difference(i, 0) = output(i, 0) - p(i, 0);
207 for(size_t j=0; j<n; j++)
208 {
209 fWeights(i, j) +=
210 learningRate * difference(i, 0) * input(j, 0) / fBatchSize;
211 }
212
213 fBiases(i, 0) += learningRate * difference(i, 0) / fBatchSize;
214 }
215}
216//______________________________________________________________________________
217
218}
219}
int Int_t
Definition: RtypesCore.h:41
float Real_t
Definition: RtypesCore.h:64
double exp(double)
static void SoftmaxAE(TMatrixT< AReal > &A)
static void EncodeInput(TMatrixT< AReal > &input, TMatrixT< AReal > &compressedInput, TMatrixT< AReal > &Weights)
static void ReconstructInput(TMatrixT< AReal > &compressedInput, TMatrixT< AReal > &reconstructedInput, TMatrixT< AReal > &fWeights)
static void UpdateParams(TMatrixT< AReal > &x, TMatrixT< AReal > &tildeX, TMatrixT< AReal > &y, TMatrixT< AReal > &z, TMatrixT< AReal > &fVBiases, TMatrixT< AReal > &fHBiases, TMatrixT< AReal > &fWeights, TMatrixT< AReal > &VBiasError, TMatrixT< AReal > &HBiasError, AReal learningRate, size_t fBatchSize)
static void UpdateParamsLogReg(TMatrixT< AReal > &input, TMatrixT< AReal > &output, TMatrixT< AReal > &difference, TMatrixT< AReal > &p, TMatrixT< AReal > &fWeights, TMatrixT< AReal > &fBiases, AReal learningRate, size_t fBatchSize)
static void CorruptInput(TMatrixT< AReal > &input, TMatrixT< AReal > &corruptedInput, AReal corruptionLevel)
static void ForwardLogReg(TMatrixT< AReal > &input, TMatrixT< AReal > &p, TMatrixT< AReal > &fWeights)
static void AddBiases(TMatrixT< AReal > &A, const TMatrixT< AReal > &biases)
Int_t GetNrows() const
Definition: TMatrixTBase.h:124
Int_t GetNcols() const
Definition: TMatrixTBase.h:127
TMatrixT.
Definition: TMatrixT.h:39
Double_t y[n]
Definition: legend1.C:17
Double_t x[n]
Definition: legend1.C:17
const Int_t n
Definition: legend1.C:16
static double A[]
Abstract ClassifierFactory template that handles arbitrary types.
auto * m
Definition: textangle.C:8
auto * a
Definition: textangle.C:12
static long int sum(long int i)
Definition: Factory.cxx:2258