Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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
19namespace TMVA {
20namespace DNN {
21//#if 0
22//____________________________________________________________________________
23template<typename AFloat>
24void 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//____________________________________________________________________________
60template<typename AFloat>
61void 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
ROOT::R::TRInterface & r
Definition Object.C:4
#define f(i)
Definition RSha256.hxx:104
unsigned int UInt_t
Definition RtypesCore.h:46
A pseudo container class which is a generator of indices.
Definition TSeq.hxx:66
static size_t GetNWorkItems(size_t nelements)
Definition CpuMatrix.h:191
static Executor & GetThreadExecutor()
Definition CpuMatrix.h:169
static TRandom & GetRandomGenerator()
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 ...
void Foreach(Function func, unsigned int nTimes, unsigned nChunks=0)
wrap TExecutor::Foreach
Definition Executor.h:111
This is the base class for the ROOT Random number generators.
Definition TRandom.h:27
virtual UInt_t Integer(UInt_t imax)
Returns a random integer uniformly distributed on the interval [ 0, imax-1 ].
Definition TRandom.cxx:360
create variable transformations