Logo ROOT   6.12/07
Reference Guide
CpuBuffer.cxx
Go to the documentation of this file.
1 // @(#)root/tmva/tmva/dnn:$Id$
2 // Author: Simon Pfreundschuh 12/08/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 // CPU Buffer interface class for the generic data loader. //
14 /////////////////////////////////////////////////////////////
15 
16 #include <vector>
17 #include <memory>
18 #include "TMVA/DataSetInfo.h"
19 #include "TMVA/DNN/DataLoader.h"
21 #include "Rtypes.h"
22 #include <iostream>
23 
24 namespace TMVA
25 {
26 namespace DNN
27 {
28 
29 //______________________________________________________________________________
30 template<typename AReal>
32 {
33  delete[] * pointer;
34  delete[] pointer;
35 }
36 
37 //______________________________________________________________________________
38 template<typename AReal>
40  : fSize(size), fOffset(0)
41 {
42  AReal ** pointer = new AReal * [1];
43  * pointer = new AReal[size];
44  fBuffer = std::shared_ptr<AReal *>(pointer, fDestructor);
45 }
46 
47 //______________________________________________________________________________
48 template<typename AReal>
50 {
51  TCpuBuffer buffer = *this;
52  buffer.fOffset = offset;
53  buffer.fSize = size;
54  return buffer;
55 }
56 
57 //______________________________________________________________________________
58 template<typename AReal>
60 {
61  std::swap(*this->fBuffer, *other.fBuffer);
62 }
63 
64 //______________________________________________________________________________
65 template<typename AReal>
67 {
68  std::swap(*this->fBuffer, *other.fBuffer);
69 }
70 
71 //______________________________________________________________________________
72 template<>
74  TCpuBuffer<Real_t> & buffer,
75  IndexIterator_t sampleIterator,
76  size_t batchSize)
77 {
78  const TMatrixT<Real_t> &inputMatrix = std::get<0>(fData);
79  size_t n = inputMatrix.GetNcols();
80 
81  for (size_t i = 0; i < batchSize; i++) {
82  size_t sampleIndex = *sampleIterator;
83  for (size_t j = 0; j < n; j++) {
84  size_t bufferIndex = j * batchSize + i;
85  buffer[bufferIndex] = static_cast<Real_t>(inputMatrix(sampleIndex, j));
86  }
87  sampleIterator++;
88  }
89 }
90 
91 //______________________________________________________________________________
92 template<>
94  TCpuBuffer<Real_t> & buffer,
95  IndexIterator_t sampleIterator,
96  size_t batchSize)
97 {
98  const TMatrixT<Real_t> &outputMatrix = std::get<1>(fData);
99  size_t n = outputMatrix.GetNcols();
100 
101  for (size_t i = 0; i < batchSize; i++) {
102  size_t sampleIndex = *sampleIterator;
103  for (size_t j = 0; j < n; j++) {
104  size_t bufferIndex = j * batchSize + i;
105  buffer[bufferIndex] = static_cast<Real_t>(outputMatrix(sampleIndex, j));
106  }
107  sampleIterator++;
108  }
109 }
110 
111 //______________________________________________________________________________
112 template <>
114  size_t batchSize)
115 {
116  const TMatrixT<Real_t> &outputMatrix = std::get<2>(fData);
117 
118  for (size_t i = 0; i < batchSize; i++) {
119  size_t sampleIndex = *sampleIterator;
120  buffer[i] = static_cast<Real_t>(outputMatrix(sampleIndex, 0));
121  sampleIterator++;
122  }
123 }
124 
125 //______________________________________________________________________________
126 template <>
128  size_t batchSize)
129 {
130  const TMatrixT<Double_t> &inputMatrix = std::get<0>(fData);
131  size_t n = inputMatrix.GetNcols();
132 
133  for (size_t i = 0; i < batchSize; i++) {
134  size_t sampleIndex = *sampleIterator;
135  for (size_t j = 0; j < n; j++) {
136  size_t bufferIndex = j * batchSize + i;
137  buffer[bufferIndex] = inputMatrix(sampleIndex, j);
138  }
139  sampleIterator++;
140  }
141 }
142 
143 //______________________________________________________________________________
144 template<>
146  TCpuBuffer<Double_t> & buffer,
147  IndexIterator_t sampleIterator,
148  size_t batchSize)
149 {
150  const TMatrixT<Double_t> &outputMatrix = std::get<1>(fData);
151  size_t n = outputMatrix.GetNcols();
152 
153  for (size_t i = 0; i < batchSize; i++) {
154  size_t sampleIndex = *sampleIterator;
155  for (size_t j = 0; j < n; j++) {
156  size_t bufferIndex = j * batchSize + i;
157  buffer[bufferIndex] = outputMatrix(sampleIndex, j);
158  }
159  sampleIterator++;
160  }
161 }
162 
163 //______________________________________________________________________________
164 template <>
166  IndexIterator_t sampleIterator, size_t batchSize)
167 {
168  const TMatrixT<Double_t> &outputMatrix = std::get<2>(fData);
169 
170  for (size_t i = 0; i < batchSize; i++) {
171  size_t sampleIndex = *sampleIterator;
172  buffer[i] = static_cast<Double_t>(outputMatrix(sampleIndex, 0));
173  sampleIterator++;
174  }
175 }
176 
177 //______________________________________________________________________________
178 template <>
180  size_t batchSize)
181 {
182  Event *event = std::get<0>(fData)[0];
183  size_t n = event->GetNVariables();
184  for (size_t i = 0; i < batchSize; i++) {
185  size_t sampleIndex = * sampleIterator++;
186  event = std::get<0>(fData)[sampleIndex];
187  for (size_t j = 0; j < n; j++) {
188  size_t bufferIndex = j * batchSize + i;
189  buffer[bufferIndex] = event->GetValue(j);
190  }
191  }
192 }
193 
194 //______________________________________________________________________________
195 template<>
197  TCpuBuffer<Double_t> & buffer,
198  IndexIterator_t sampleIterator,
199  size_t batchSize)
200 {
201  const DataSetInfo &info = std::get<1>(fData);
202  size_t n = buffer.GetSize() / batchSize;
203 
204  // Copy target(s).
205 
206  for (size_t i = 0; i < batchSize; i++) {
207  size_t sampleIndex = *sampleIterator++;
208  Event *event = std::get<0>(fData)[sampleIndex];
209  for (size_t j = 0; j < n; j++) {
210  // Copy output matrices.
211  size_t bufferIndex = j * batchSize + i;
212  // Classification
213  if (event->GetNTargets() == 0) {
214  if (n == 1) {
215  // Binary.
216  buffer[bufferIndex] = (info.IsSignal(event)) ? 1.0 : 0.0;
217  } else {
218  // Multiclass.
219  buffer[bufferIndex] = 0.0;
220  if (j == event->GetClass()) {
221  buffer[bufferIndex] = 1.0;
222  }
223  }
224  } else {
225  buffer[bufferIndex] = static_cast<Real_t>(event->GetTarget(j));
226  }
227  }
228  }
229 }
230 
231 //______________________________________________________________________________
232 template <>
234  size_t batchSize)
235 {
236  for (size_t i = 0; i < batchSize; i++) {
237  size_t sampleIndex = *sampleIterator++;
238  Event *event = std::get<0>(fData)[sampleIndex];
239  buffer[i] = event->GetWeight();
240  }
241 }
242 
243 //______________________________________________________________________________
244 template <>
246  size_t batchSize)
247 {
248  Event *event = std::get<0>(fData)[0];
249  size_t n = event->GetNVariables();
250  for (size_t i = 0; i < batchSize; i++) {
251  size_t sampleIndex = * sampleIterator++;
252  event = std::get<0>(fData)[sampleIndex];
253  for (size_t j = 0; j < n; j++) {
254  size_t bufferIndex = j * batchSize + i;
255  buffer[bufferIndex] = static_cast<Real_t>(event->GetValue(j));
256  }
257  }
258 }
259 
260 //______________________________________________________________________________
261 template<>
263  TCpuBuffer<Real_t> & buffer,
264  IndexIterator_t sampleIterator,
265  size_t batchSize)
266 {
267  const DataSetInfo &info = std::get<1>(fData);
268  size_t n = buffer.GetSize() / batchSize;
269 
270  // Copy target(s).
271 
272  for (size_t i = 0; i < batchSize; i++) {
273  size_t sampleIndex = *sampleIterator++;
274  Event *event = std::get<0>(fData)[sampleIndex];
275  for (size_t j = 0; j < n; j++) {
276  // Copy output matrices.
277  size_t bufferIndex = j * batchSize + i;
278  // Classification
279  if (event->GetNTargets() == 0) {
280  if (n == 1) {
281  // Binary.
282  buffer[bufferIndex] = (info.IsSignal(event)) ? 1.0 : 0.0;
283  } else {
284  // Multiclass.
285  buffer[bufferIndex] = 0.0;
286  if (j == event->GetClass()) {
287  buffer[bufferIndex] = 1.0;
288  }
289  }
290  } else {
291  buffer[bufferIndex] = static_cast<Real_t>(event->GetTarget(j));
292  }
293  }
294  }
295 }
296 
297 //______________________________________________________________________________
298 template <>
300  size_t batchSize)
301 {
302  for (size_t i = 0; i < batchSize; i++) {
303  size_t sampleIndex = *sampleIterator++;
304  Event *event = std::get<0>(fData)[sampleIndex];
305  buffer[i] = static_cast<Real_t>(event->GetWeight());
306  }
307 }
308 
309 // Explicit instantiations.
310 template class TCpuBuffer<Double_t>;
311 template class TCpuBuffer<Real_t>;
312 
313 } // namespace DNN
314 } // namespace TMVA
315 
316 
typename std::vector< size_t >::iterator IndexIterator_t
Definition: DataLoader.h:42
void swap(TDirectoryEntry &e1, TDirectoryEntry &e2) noexcept
void CopyTo(TCpuBuffer &)
Copy data to another buffer.
Definition: CpuBuffer.cxx:66
std::shared_ptr< AFloat * > fBuffer
Definition: CpuBuffer.h:49
Int_t GetNcols() const
Definition: TMatrixTBase.h:125
void operator()(AFloat **pointer)
Definition: CpuBuffer.cxx:31
TMatrixT.
Definition: TMatrixDfwd.h:22
Class that contains all the data information.
Definition: DataSetInfo.h:60
TCpuBuffer(size_t size)
Construct buffer to hold size numbers of type AFloat.
Definition: CpuBuffer.cxx:39
TCpuBuffer.
Definition: CpuBuffer.h:43
TDataLoader.
Definition: DataLoader.h:79
double Double_t
Definition: RtypesCore.h:55
float Real_t
Definition: RtypesCore.h:64
Abstract ClassifierFactory template that handles arbitrary types.
void CopyFrom(TCpuBuffer &)
Copy data from another buffer.
Definition: CpuBuffer.cxx:59
Bool_t IsSignal(const Event *ev) const
TCpuBuffer GetSubBuffer(size_t offset, size_t start)
Return subbuffer of siez start starting at element offset.
Definition: CpuBuffer.cxx:49
size_t GetSize() const
Definition: CpuBuffer.h:81
struct TMVA::DNN::TCpuBuffer::TDestructor fDestructor
const Int_t n
Definition: legend1.C:16