Logo ROOT  
Reference Guide
Dropout.hxx
Go to the documentation of this file.
1 // @(#)root/tmva/tmva/dnn:$Id$
2 // Author: Simon Pfreundschuh 21/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 
13 #include "TRandom.h"
14 
15 /////////////////////////////////////////////////////////////////////
16 // Implementation of Dropout for multi-threaded CPU architectures. //
17 /////////////////////////////////////////////////////////////////////
18 
19 namespace TMVA {
20 namespace DNN {
21 //#if 0
22 //____________________________________________________________________________
23 template<typename AFloat>
24 void TCpu<AFloat>::DropoutForward(TCpuTensor<AFloat> & A,
25  TDescriptors * /*descriptors*/,
26  TWorkspace * /*workspace*/,
27  AFloat dropoutProbability)
28 {
29  AFloat *data = A.GetData();
30 
32  size_t seed = dlRand.Integer(4294967295); // use 2^32-1
33 
34  size_t nElements = A.GetSize();
35  const size_t nSteps = TCpuMatrix<AFloat>::GetNWorkItems(nElements);
36 
37  // apply droput. The probability is actually the probability to keep the node
38  // (i.e. 1 - dropout_prob)
39  auto f = [&data, dropoutProbability, &nSteps, &nElements, &seed](UInt_t workerID)
40  {
41  TRandom rand(seed+workerID);
42  size_t iMax = std::min(workerID+nSteps,nElements);
43  for (size_t i = workerID; i < iMax; ++i) {
44  AFloat r = rand.Uniform();
45  data[i] = (r > dropoutProbability) ? 0.0 : data[i] / dropoutProbability;
46  }
47  return 0;
48  };
49 
50 #ifdef DL_USE_MTE
52 #else
53  for (size_t i = 0; i < nElements; i+=nSteps)
54  f(i);
55 #endif
56 }
57  // old impl (to be removed)
58 #if 0
59 //____________________________________________________________________________
60 template<typename AFloat>
61 void TCpu<AFloat>::Dropout(TCpuMatrix<AFloat> &A,
62  AFloat dropoutProbability)
63 {
64  AFloat *data = A.GetRawDataPointer();
65 
66  auto f = [&data, dropoutProbability](UInt_t workerID)
67  {
68  TRandom rand(time(nullptr) + workerID);
69  AFloat r = rand.Uniform();
70  data[workerID] = (r > dropoutProbability) ? 0.0 : data[workerID] / dropoutProbability;
71  return 0;
72  };
73 
74  A.GetThreadExecutor().Map(f, ROOT::TSeqI(A.GetNoElements()));
75 }
76 #endif
77 
78 
79 
80 } // namespace DNN
81 } // namespace TMVA
Cpu.h
f
#define f(i)
Definition: RSha256.hxx:104
r
ROOT::R::TRInterface & r
Definition: Object.C:4
TMVA::DNN::TCpuMatrix::GetThreadExecutor
static Executor & GetThreadExecutor()
Definition: CpuMatrix.h:169
TRandom.h
ROOT::Math::Cephes::A
static double A[]
Definition: SpecFuncCephes.cxx:170
TMVA::DNN::TCpuMatrix::GetNWorkItems
static size_t GetNWorkItems(size_t nelements)
Definition: CpuMatrix.h:191
TRandom
This is the base class for the ROOT Random number generators.
Definition: TRandom.h:27
TMVA::DNN::TCpu::GetRandomGenerator
static TRandom & GetRandomGenerator()
Definition: Initialization.hxx:35
TMVA::DNN::TCpu::DropoutForward
static void DropoutForward(Tensor_t &A, TDescriptors *descriptors, TWorkspace *workspace, Scalar_t p)
Apply dropout with activation probability p to the given tensor A and scale the result by reciprocal ...
TMVA::Executor::Foreach
void Foreach(Function func, unsigned int nTimes, unsigned nChunks=0)
wrap TExecutor::Foreach
Definition: Executor.h:111
UInt_t
unsigned int UInt_t
Definition: RtypesCore.h:46
TRandom::Integer
virtual UInt_t Integer(UInt_t imax)
Returns a random integer uniformly distributed on the interval [ 0, imax-1 ].
Definition: TRandom.cxx:360
ROOT::TSeq
A pseudo container class which is a generator of indices.
Definition: TSeq.hxx:66
TMVA
create variable transformations
Definition: GeneticMinimizer.h:22