Logo ROOT   6.10/09
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/DNN/DataLoader.h"
20 #include "Rtypes.h"
21 #include <iostream>
22 
23 namespace TMVA
24 {
25 namespace DNN
26 {
27 
28 //______________________________________________________________________________
29 template<typename AReal>
31 {
32  delete[] * pointer;
33  delete[] pointer;
34 }
35 
36 //______________________________________________________________________________
37 template<typename AReal>
39  : fSize(size), fOffset(0)
40 {
41  AReal ** pointer = new AReal * [1];
42  * pointer = new AReal[size];
43  fBuffer = std::shared_ptr<AReal *>(pointer, fDestructor);
44 }
45 
46 //______________________________________________________________________________
47 template<typename AReal>
49 {
50  TCpuBuffer buffer = *this;
51  buffer.fOffset = offset;
52  buffer.fSize = size;
53  return buffer;
54 }
55 
56 //______________________________________________________________________________
57 template<typename AReal>
59 {
60  std::swap(*this->fBuffer, *other.fBuffer);
61 }
62 
63 //______________________________________________________________________________
64 template<typename AReal>
66 {
67  std::swap(*this->fBuffer, *other.fBuffer);
68 }
69 
70 //______________________________________________________________________________
71 template<>
73  TCpuBuffer<Real_t> & buffer,
74  IndexIterator_t sampleIterator,
75  size_t batchSize)
76 {
77  const TMatrixT<Real_t> &inputMatrix = std::get<0>(fData);
78  size_t n = inputMatrix.GetNcols();
79 
80  for (size_t i = 0; i < batchSize; i++) {
81  size_t sampleIndex = *sampleIterator;
82  for (size_t j = 0; j < n; j++) {
83  size_t bufferIndex = j * batchSize + i;
84  buffer[bufferIndex] = static_cast<Real_t>(inputMatrix(sampleIndex, j));
85  }
86  sampleIterator++;
87  }
88 }
89 
90 //______________________________________________________________________________
91 template<>
93  TCpuBuffer<Real_t> & buffer,
94  IndexIterator_t sampleIterator,
95  size_t batchSize)
96 {
97  const TMatrixT<Real_t> &outputMatrix = std::get<1>(fData);
98  size_t n = outputMatrix.GetNcols();
99 
100  for (size_t i = 0; i < batchSize; i++) {
101  size_t sampleIndex = *sampleIterator;
102  for (size_t j = 0; j < n; j++) {
103  size_t bufferIndex = j * batchSize + i;
104  buffer[bufferIndex] = static_cast<Real_t>(outputMatrix(sampleIndex, j));
105  }
106  sampleIterator++;
107  }
108 }
109 
110 //______________________________________________________________________________
111 template<>
113  TCpuBuffer<Double_t> & buffer,
114  IndexIterator_t sampleIterator,
115  size_t batchSize)
116 {
117  const TMatrixT<Double_t> &inputMatrix = std::get<0>(fData);
118  size_t n = inputMatrix.GetNcols();
119 
120  for (size_t i = 0; i < batchSize; i++) {
121  size_t sampleIndex = *sampleIterator;
122  for (size_t j = 0; j < n; j++) {
123  size_t bufferIndex = j * batchSize + i;
124  buffer[bufferIndex] = inputMatrix(sampleIndex, j);
125  }
126  sampleIterator++;
127  }
128 }
129 
130 //______________________________________________________________________________
131 template<>
133  TCpuBuffer<Double_t> & buffer,
134  IndexIterator_t sampleIterator,
135  size_t batchSize)
136 {
137  const TMatrixT<Double_t> &outputMatrix = std::get<1>(fData);
138  size_t n = outputMatrix.GetNcols();
139 
140  for (size_t i = 0; i < batchSize; i++) {
141  size_t sampleIndex = *sampleIterator;
142  for (size_t j = 0; j < n; j++) {
143  size_t bufferIndex = j * batchSize + i;
144  buffer[bufferIndex] = outputMatrix(sampleIndex, j);
145  }
146  sampleIterator++;
147  }
148 }
149 
150 //______________________________________________________________________________
151 template<>
153  TCpuBuffer<Double_t> & buffer,
154  IndexIterator_t sampleIterator,
155  size_t batchSize)
156 {
157  Event * event = fData.front();
158  size_t n = event->GetNVariables();
159 
160  // Copy input variables.
161 
162  for (size_t i = 0; i < batchSize; i++) {
163  size_t sampleIndex = * sampleIterator++;
164  event = fData[sampleIndex];
165  for (size_t j = 0; j < n; j++) {
166  size_t bufferIndex = j * batchSize + i;
167  buffer[bufferIndex] = event->GetValue(j);
168  }
169  }
170 }
171 
172 //______________________________________________________________________________
173 template<>
175  TCpuBuffer<Double_t> & buffer,
176  IndexIterator_t sampleIterator,
177  size_t batchSize)
178 {
179  Event * event = fData.front();
180  size_t n = buffer.GetSize() / batchSize;
181 
182  // Copy target(s).
183 
184  for (size_t i = 0; i < batchSize; i++) {
185  size_t sampleIndex = * sampleIterator++;
186  event = fData[sampleIndex];
187  for (size_t j = 0; j < n; j++) {
188  // Copy output matrices.
189  size_t bufferIndex = j * batchSize + i;
190  // Classification
191  if (event->GetNTargets() == 0) {
192  if (n == 1) {
193  // Binary.
194  buffer[bufferIndex] = (event->GetClass() == 0) ? 1.0 : 0.0;
195  } else {
196  // Multiclass.
197  buffer[bufferIndex] = 0.0;
198  if (j == event->GetClass()) {
199  buffer[bufferIndex] = 1.0;
200  }
201  }
202  } else {
203  buffer[bufferIndex] = static_cast<Real_t>(event->GetTarget(j));
204  }
205  }
206  }
207 }
208 
209 //______________________________________________________________________________
210 template<>
212  TCpuBuffer<Real_t> & buffer,
213  IndexIterator_t sampleIterator,
214  size_t batchSize)
215 {
216  Event * event = fData.front();
217  size_t n = event->GetNVariables();
218 
219  // Copy input variables.
220 
221  for (size_t i = 0; i < batchSize; i++) {
222  size_t sampleIndex = * sampleIterator++;
223  event = fData[sampleIndex];
224  for (size_t j = 0; j < n; j++) {
225  size_t bufferIndex = j * batchSize + i;
226  buffer[bufferIndex] = static_cast<Real_t>(event->GetValue(j));
227  }
228  }
229 }
230 
231 //______________________________________________________________________________
232 template<>
234  TCpuBuffer<Real_t> & buffer,
235  IndexIterator_t sampleIterator,
236  size_t batchSize)
237 {
238  Event * event = fData.front();
239  size_t n = buffer.GetSize() / batchSize;
240 
241  // Copy target(s).
242 
243  for (size_t i = 0; i < batchSize; i++) {
244  size_t sampleIndex = * sampleIterator++;
245  event = fData[sampleIndex];
246  for (size_t j = 0; j < n; j++) {
247  // Copy output matrices.
248  size_t bufferIndex = j * batchSize + i;
249  // Classification
250  if (event->GetNTargets() == 0) {
251  if (n == 1) {
252  // Binary.
253  buffer[bufferIndex] = (event->GetClass() == 0) ? 1.0 : 0.0;
254  } else {
255  // Multiclass.
256  buffer[bufferIndex] = 0.0;
257  if (j == event->GetClass()) {
258  buffer[bufferIndex] = 1.0;
259  }
260  }
261  } else {
262  buffer[bufferIndex] = static_cast<Real_t>(event->GetTarget(j));
263  }
264  }
265  }
266 }
267 
268 // Explicit instantiations.
269 template class TCpuBuffer<Double_t>;
270 template class TCpuBuffer<Real_t>;
271 
272 } // namespace DNN
273 } // namespace TMVA
274 
275 
typename std::vector< size_t >::iterator IndexIterator_t
Definition: DataLoader.h:38
void swap(TDirectoryEntry &e1, TDirectoryEntry &e2) noexcept
void CopyTo(TCpuBuffer &)
Copy data to another buffer.
Definition: CpuBuffer.cxx:65
std::shared_ptr< AFloat * > fBuffer
Definition: CpuBuffer.h:49
Int_t GetNcols() const
Definition: TMatrixTBase.h:125
void operator()(AFloat **pointer)
Definition: CpuBuffer.cxx:30
TMatrixT.
Definition: TMatrixDfwd.h:22
TCpuBuffer(size_t size)
Construct buffer to hold size numbers of type AFloat.
Definition: CpuBuffer.cxx:38
TCpuBuffer.
Definition: CpuBuffer.h:43
TDataLoader.
Definition: DataLoader.h:73
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:58
TCpuBuffer GetSubBuffer(size_t offset, size_t start)
Return subbuffer of siez start starting at element offset.
Definition: CpuBuffer.cxx:48
size_t GetSize() const
Definition: CpuBuffer.h:81
struct TMVA::DNN::TCpuBuffer::TDestructor fDestructor
const Int_t n
Definition: legend1.C:16