Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
Initialization.cu
Go to the documentation of this file.
1// @(#)root/tmva/tmva/dnn:$Id$
2// Author: Simon Pfreundschuh 14/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 initialization functions for CUDA //
14 // Architectures //
15 /////////////////////////////////////////////////////////////
16
17#include "TRandom3.h"
18
21
22
23
24namespace TMVA
25{
26namespace DNN
27{
28
29template <typename AFloat>
30TRandom * TCudnn<AFloat>::fgRandomGen = nullptr;
31//______________________________________________________________________________
32template<typename AFloat>
33void TCudnn<AFloat>::SetRandomSeed(size_t seed)
34{
35 if (!fgRandomGen) fgRandomGen = new TRandom3();
36 fgRandomGen->SetSeed(seed);
37}
38template<typename AFloat>
39TRandom & TCudnn<AFloat>::GetRandomGenerator()
40{
41 if (!fgRandomGen) fgRandomGen = new TRandom3(0);
42 return *fgRandomGen;
43}
44//______________________________________________________________________________
45template<typename AFloat>
46void TCudnn<AFloat>::InitializeGauss(TCudaTensor<AFloat> & A)
47{
48 // n is the size of the feature map
49 size_t n = (A.GetNDim() == 2 && A.GetLayout() == Tensor_t::MemoryLayout::ColumnMajor) ?
50 A.GetShape()[1] : A.GetFirstStride();
51
52 TRandom & rand = GetRandomGenerator();
53
54 Double_t sigma = sqrt(2.0 / ((Double_t) n));
55
56 size_t nelements = A.GetSize();
57 TCudaHostBuffer<AFloat> xhost(nelements);
58 for (size_t i = 0; i < nelements; i++) {
59 xhost[i] = rand.Gaus(0,sigma);
60 }
61 A.GetDeviceBuffer().CopyFrom(xhost);
62}
63
64//______________________________________________________________________________
65template<typename AFloat>
66void TCudnn<AFloat>::InitializeUniform(TCudaTensor<AFloat> & A)
67{
68 // n is the size of the feature map
69 size_t n =
70 (A.GetNDim() == 2 && A.GetLayout() == Tensor_t::MemoryLayout::ColumnMajor) ?
71 A.GetShape()[1] : A.GetFirstStride();
72
73 TRandom & rand = GetRandomGenerator();
74
75 Double_t range = sqrt(2.0 / ((Double_t) n));
76
77 size_t nelements = A.GetSize();
78 TCudaHostBuffer<AFloat> xhost(nelements);
79 for (size_t i = 0; i < nelements; i++) {
80 xhost[i] = rand.Uniform(-range, range);
81 }
82 A.GetDeviceBuffer().CopyFrom(xhost);
83
84}
85
86//______________________________________________________________________________
87/// Truncated normal initialization (Glorot, called also Xavier normal)
88/// The values are sample with a normal distribution with stddev = sqrt(2/N_input + N_output) and
89/// values larger than 2 * stddev are discarded
90/// See Glorot & Bengio, AISTATS 2010 - http://jmlr.org/proceedings/papers/v9/glorot10a/glorot10a.pdf
91template<typename AFloat>
92void TCudnn<AFloat>::InitializeGlorotNormal(TCudaTensor<AFloat> & A)
93{
94 // n,m are the output/input units of the tensor
95 // default is caseof tensor of 2D (dense layer)
96 size_t n = A.GetShape()[0]; // output size
97 size_t m = A.GetShape()[1]; // input size
98 // for convolutions
99 if (A.GetShape().size() > 2) {
100 // n is number of inputs
101 for (size_t j = 2; j < A.GetShape().size(); ++j) {
102 m *= A.GetShape()[j];
103 n *= A.GetShape()[j];
104 }
105 }
106
107 TRandom & rand = GetRandomGenerator();
108 Double_t sigma = sqrt(2.0 /((Double_t) n + (Double_t) m) );
109
110 size_t nsize = A.GetSize();
111 TCudaHostBuffer<AFloat> xhost(nsize);
112 for (size_t i = 0; i < nsize; i++) {
113 AFloat value = 0;
114 do {
115 value = rand.Gaus(0.0, sigma);
116 } while ( std::abs(value) > 2*sigma);
117 xhost[i] = value;
118 }
119 A.GetDeviceBuffer().CopyFrom(xhost);
120}
121
122//______________________________________________________________________________
123/// Sample from a uniform distribution in range [ -lim,+lim] where
124/// lim = sqrt(6/N_in+N_out).
125/// This initialization is also called Xavier uniform
126/// see Glorot & Bengio, AISTATS 2010 - http://jmlr.org/proceedings/papers/v9/glorot10a/glorot10a.pdf
127template<typename AFloat>
128void TCudnn<AFloat>::InitializeGlorotUniform(TCudaTensor<AFloat> & A)
129{
130 size_t n = A.GetShape()[0]; // output size
131 size_t m = A.GetShape()[1]; // input size
132 // for convolutions
133 if (A.GetShape().size() > 2) {
134 // n is number of inputs
135 for (size_t j = 2; j < A.GetShape().size(); ++j) {
136 m *= A.GetShape()[j];
137 n *= A.GetShape()[j];
138 }
139 }
140
141 TRandom & rand = GetRandomGenerator();
142 Double_t range = sqrt(6.0 /( (Double_t) n + (Double_t) m) );
143
144 size_t nsize = A.GetSize();
145 TCudaHostBuffer<AFloat> xhost(nsize);
146 for (size_t i = 0; i < nsize; i++) {
147 xhost[i] = rand.Uniform(-range, range);
148 }
149 A.GetDeviceBuffer().CopyFrom(xhost);
150
151}
152
153//______________________________________________________________________________
154template<typename AFloat>
155void TCudnn<AFloat>::InitializeIdentity(TCudaTensor<AFloat> & A)
156{
157 size_t m,n;
158 m = A.GetFirstSize();
159 n = A.GetFirstStride();
160 // assume weight trnsor is like a matrix M x N
161 TMatrixT<AFloat> B(m, n);
162
163 for (size_t i = 0; i < m; i++) {
164 for (size_t j = 0; j < n ; j++) {
165 B(i,j) = 0.0;
166 }
167 if (i < n) {
168 B(i,i) = 1.0;
169 }
170 }
171 TCudaMatrix<AFloat> mB = B;
172 A.GetDeviceBuffer() = mB.GetDeviceBuffer();
173}
174
175//______________________________________________________________________________
176template<typename AFloat>
177void TCudnn<AFloat>::InitializeZero(TCudaTensor<AFloat> & A)
178{
179 // use fast zero initialization on the device
180 cudaMemset(A.GetDataPointer(), 0, sizeof(AFloat) * A.GetSize());
181}
182
183} // namespace DNN
184} // namespace TMVA
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
double Double_t
Definition RtypesCore.h:59
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void value
TMatrixT.
Definition TMatrixT.h:40
Random number generator class based on M.
Definition TRandom3.h:27
This is the base class for the ROOT Random number generators.
Definition TRandom.h:27
virtual Double_t Gaus(Double_t mean=0, Double_t sigma=1)
Samples a random number from the standard Normal (Gaussian) Distribution with the given mean and sigm...
Definition TRandom.cxx:275
virtual Double_t Uniform(Double_t x1=1)
Returns a uniform deviate on the interval (0, x1).
Definition TRandom.cxx:682
const Double_t sigma
const Int_t n
Definition legend1.C:16
VecExpr< UnaryOp< Sqrt< T >, VecExpr< A, T, D >, T >, T, D > sqrt(const VecExpr< A, T, D > &rhs)
create variable transformations
TMarker m
Definition textangle.C:8