Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
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 
31  TRandom & dlRand = TCpu<AFloat>::GetRandomGenerator();
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
51  TCpuMatrix<AFloat>::GetThreadExecutor().Foreach(f, ROOT::TSeqI(0,nElements,nSteps));
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