29 template <
typename AReal>
30 void TCpuBuffer<AReal>::TDestructor::operator()(AReal **pointer)
37 template <
typename AReal>
38 TCpuBuffer<AReal>::TCpuBuffer(
size_t size) : fSize(size), fOffset(0)
40 AReal **pointer =
new AReal *[1];
41 *pointer =
new AReal[size];
42 fBuffer = std::shared_ptr<AReal *>(pointer, fDestructor);
46 template <
typename AReal>
47 TCpuBuffer<AReal> TCpuBuffer<AReal>::GetSubBuffer(
size_t offset,
size_t size)
const
49 TCpuBuffer buffer = *
this;
50 buffer.fOffset = offset;
56 template <
typename AReal>
57 void TCpuBuffer<AReal>::CopyFrom(
const TCpuBuffer &other)
60 std::copy( *other.fBuffer, *other.fBuffer+fSize, *this->fBuffer);
64 template <
typename AReal>
65 void TCpuBuffer<AReal>::CopyTo(TCpuBuffer &other)
const
67 std::copy( *this->fBuffer, *this->fBuffer+fSize, *other.fBuffer);
73 void TDataLoader<MatrixInput_t, TCpu<Float_t>>::CopyInput(TCpuBuffer<Float_t> &buffer, IndexIterator_t sampleIterator,
76 const TMatrixT<Float_t> &inputMatrix = std::get<0>(fData);
77 size_t n = inputMatrix.GetNcols();
79 for (
size_t i = 0; i < batchSize; i++) {
80 size_t sampleIndex = *sampleIterator;
81 for (
size_t j = 0; j < n; j++) {
82 size_t bufferIndex = j * batchSize + i;
83 buffer[bufferIndex] =
static_cast<Float_t
>(inputMatrix(sampleIndex, j));
91 void TDataLoader<MatrixInput_t, TCpu<Float_t>>::CopyOutput(TCpuBuffer<Float_t> &buffer, IndexIterator_t sampleIterator,
94 const TMatrixT<Float_t> &outputMatrix = std::get<1>(fData);
95 size_t n = outputMatrix.GetNcols();
97 for (
size_t i = 0; i < batchSize; i++) {
98 size_t sampleIndex = *sampleIterator;
99 for (
size_t j = 0; j < n; j++) {
100 size_t bufferIndex = j * batchSize + i;
101 buffer[bufferIndex] =
static_cast<Float_t
>(outputMatrix(sampleIndex, j));
109 void TDataLoader<MatrixInput_t, TCpu<Float_t>>::CopyWeights(TCpuBuffer<Float_t> &buffer, IndexIterator_t sampleIterator,
112 const TMatrixT<Float_t> &outputMatrix = std::get<2>(fData);
114 for (
size_t i = 0; i < batchSize; i++) {
115 size_t sampleIndex = *sampleIterator;
116 buffer[i] =
static_cast<Float_t
>(outputMatrix(sampleIndex, 0));
123 void TDataLoader<MatrixInput_t, TCpu<Double_t>>::CopyInput(TCpuBuffer<Double_t> &buffer, IndexIterator_t sampleIterator,
126 const TMatrixT<Double_t> &inputMatrix = std::get<0>(fData);
127 size_t n = inputMatrix.GetNcols();
129 for (
size_t i = 0; i < batchSize; i++) {
130 size_t sampleIndex = *sampleIterator;
131 for (
size_t j = 0; j < n; j++) {
132 size_t bufferIndex = j * batchSize + i;
133 buffer[bufferIndex] = inputMatrix(sampleIndex, j);
141 void TDataLoader<MatrixInput_t, TCpu<Double_t>>::CopyOutput(TCpuBuffer<Double_t> &buffer,
142 IndexIterator_t sampleIterator,
size_t batchSize)
144 const TMatrixT<Double_t> &outputMatrix = std::get<1>(fData);
145 size_t n = outputMatrix.GetNcols();
147 for (
size_t i = 0; i < batchSize; i++) {
148 size_t sampleIndex = *sampleIterator;
149 for (
size_t j = 0; j < n; j++) {
150 size_t bufferIndex = j * batchSize + i;
151 buffer[bufferIndex] = outputMatrix(sampleIndex, j);
159 void TDataLoader<MatrixInput_t, TCpu<Double_t>>::CopyWeights(TCpuBuffer<Double_t> &buffer,
160 IndexIterator_t sampleIterator,
size_t batchSize)
162 const TMatrixT<Double_t> &outputMatrix = std::get<2>(fData);
164 for (
size_t i = 0; i < batchSize; i++) {
165 size_t sampleIndex = *sampleIterator;
166 buffer[i] =
static_cast<Double_t
>(outputMatrix(sampleIndex, 0));
173 void TDataLoader<TMVAInput_t, TCpu<Double_t>>::CopyInput(TCpuBuffer<Double_t> &buffer, IndexIterator_t sampleIterator,
176 Event *
event = std::get<0>(fData)[0];
177 size_t n =
event->GetNVariables();
178 for (
size_t i = 0; i < batchSize; i++) {
179 size_t sampleIndex = * sampleIterator++;
180 event = std::get<0>(fData)[sampleIndex];
181 for (
size_t j = 0; j < n; j++) {
182 size_t bufferIndex = j * batchSize + i;
183 buffer[bufferIndex] =
event->GetValue(j);
190 void TDataLoader<TMVAInput_t, TCpu<Double_t>>::CopyOutput(TCpuBuffer<Double_t> &buffer, IndexIterator_t sampleIterator,
193 const DataSetInfo &info = std::get<1>(fData);
194 size_t n = buffer.GetSize() / batchSize;
198 for (
size_t i = 0; i < batchSize; i++) {
199 size_t sampleIndex = *sampleIterator++;
200 Event *
event = std::get<0>(fData)[sampleIndex];
201 for (
size_t j = 0; j < n; j++) {
203 size_t bufferIndex = j * batchSize + i;
205 if (event->GetNTargets() == 0) {
208 buffer[bufferIndex] = (info.IsSignal(event)) ? 1.0 : 0.0;
211 buffer[bufferIndex] = 0.0;
212 if (j == event->GetClass()) {
213 buffer[bufferIndex] = 1.0;
217 buffer[bufferIndex] =
static_cast<Float_t
>(
event->GetTarget(j));
225 void TDataLoader<TMVAInput_t, TCpu<Double_t>>::CopyWeights(TCpuBuffer<Double_t> &buffer, IndexIterator_t sampleIterator,
228 for (
size_t i = 0; i < batchSize; i++) {
229 size_t sampleIndex = *sampleIterator++;
230 Event *
event = std::get<0>(fData)[sampleIndex];
231 buffer[i] =
event->GetWeight();
237 void TDataLoader<TMVAInput_t, TCpu<Float_t>>::CopyInput(TCpuBuffer<Float_t> &buffer, IndexIterator_t sampleIterator,
240 Event *
event = std::get<0>(fData)[0];
241 size_t n =
event->GetNVariables();
242 for (
size_t i = 0; i < batchSize; i++) {
243 size_t sampleIndex = * sampleIterator++;
244 event = std::get<0>(fData)[sampleIndex];
245 for (
size_t j = 0; j < n; j++) {
246 size_t bufferIndex = j * batchSize + i;
247 buffer[bufferIndex] =
static_cast<Float_t
>(
event->GetValue(j));
254 void TDataLoader<TMVAInput_t, TCpu<Float_t>>::CopyOutput(TCpuBuffer<Float_t> &buffer, IndexIterator_t sampleIterator,
257 const DataSetInfo &info = std::get<1>(fData);
258 size_t n = buffer.GetSize() / batchSize;
262 for (
size_t i = 0; i < batchSize; i++) {
263 size_t sampleIndex = *sampleIterator++;
264 Event *
event = std::get<0>(fData)[sampleIndex];
265 for (
size_t j = 0; j < n; j++) {
267 size_t bufferIndex = j * batchSize + i;
269 if (event->GetNTargets() == 0) {
272 buffer[bufferIndex] = (info.IsSignal(event)) ? 1.0 : 0.0;
275 buffer[bufferIndex] = 0.0;
276 if (j == event->GetClass()) {
277 buffer[bufferIndex] = 1.0;
281 buffer[bufferIndex] =
static_cast<Float_t
>(
event->GetTarget(j));
289 void TDataLoader<TMVAInput_t, TCpu<Float_t>>::CopyWeights(TCpuBuffer<Float_t> &buffer, IndexIterator_t sampleIterator,
292 for (
size_t i = 0; i < batchSize; i++) {
293 size_t sampleIndex = *sampleIterator++;
294 Event *
event = std::get<0>(fData)[sampleIndex];
295 buffer[i] =
static_cast<Float_t
>(
event->GetWeight());
301 void TTensorDataLoader<TensorInput, TCpu<Float_t>>::CopyTensorInput(TCpuBuffer<Float_t> &buffer,
302 IndexIterator_t sampleIterator)
304 const std::vector<TMatrixT<Double_t>> &inputTensor = std::get<0>(fData);
306 if (fBatchDepth == 1) {
307 for (
size_t i = 0; i < fBatchHeight; i++) {
308 size_t sampleIndex = *sampleIterator;
309 for (
size_t j = 0; j < fBatchWidth; j++) {
310 size_t bufferIndex = j * fBatchHeight + i;
311 buffer[bufferIndex] =
static_cast<Float_t
>(inputTensor[0](sampleIndex, j));
316 for (
size_t i = 0; i < fBatchDepth; i++) {
317 size_t sampleIndex = *sampleIterator;
318 for (
size_t j = 0; j < fBatchHeight; j++) {
319 for (
size_t k = 0; k < fBatchWidth; k++) {
320 size_t bufferIndex = i * fBatchHeight * fBatchWidth + k * fBatchHeight + j;
321 buffer[bufferIndex] =
static_cast<Float_t
>(inputTensor[sampleIndex](j, k));
331 void TTensorDataLoader<TensorInput, TCpu<Float_t>>::CopyTensorOutput(TCpuBuffer<Float_t> &buffer,
332 IndexIterator_t sampleIterator)
334 const TMatrixT<Double_t> &outputMatrix = std::get<1>(fData);
335 size_t n = outputMatrix.GetNcols();
337 for (
size_t i = 0; i < fBatchSize; i++) {
338 size_t sampleIndex = *sampleIterator;
339 for (
size_t j = 0; j < n; j++) {
340 size_t bufferIndex = j * fBatchSize + i;
341 buffer[bufferIndex] =
static_cast<Float_t
>(outputMatrix(sampleIndex, j));
349 void TTensorDataLoader<TensorInput, TCpu<Float_t>>::CopyTensorWeights(TCpuBuffer<Float_t> &buffer,
350 IndexIterator_t sampleIterator)
352 const TMatrixT<Double_t> &outputMatrix = std::get<2>(fData);
354 for (
size_t i = 0; i < fBatchSize; i++) {
355 size_t sampleIndex = *sampleIterator;
356 buffer[i] =
static_cast<Float_t
>(outputMatrix(sampleIndex, 0));
364 TTensorBatch<TCpu<Float_t> > TTensorDataLoader<TensorInput, TCpu<Float_t> >::GetTensorBatch()
368 DeviceBufferTuple DeviceBuffers = CopyTensorBatches();
370 Tensor_t inputTensor( std::get<0>(DeviceBuffers), { fBatchHeight, fBatchWidth, fBatchSize } );
376 Matrix_t outputMatrix(std::get<1>(DeviceBuffers), fBatchSize, fNOutputFeatures);
377 Matrix_t weightMatrix(std::get<2>(DeviceBuffers), fBatchSize, fNOutputFeatures);
380 return TTensorBatch<TCpu<Float_t> >(inputTensor, outputMatrix, weightMatrix);
386 void TTensorDataLoader<TensorInput, TCpu<Double_t>>::CopyTensorInput(TCpuBuffer<Double_t> &buffer,
387 IndexIterator_t sampleIterator)
389 const std::vector<TMatrixT<Double_t>> &inputTensor = std::get<0>(fData);
391 if (fBatchDepth == 1) {
392 for (
size_t i = 0; i < fBatchHeight; i++) {
393 size_t sampleIndex = *sampleIterator;
394 for (
size_t j = 0; j < fBatchWidth; j++) {
395 size_t bufferIndex = j * fBatchHeight + i;
396 buffer[bufferIndex] = inputTensor[0](sampleIndex, j);
401 for (
size_t i = 0; i < fBatchDepth; i++) {
402 size_t sampleIndex = *sampleIterator;
403 for (
size_t j = 0; j < fBatchHeight; j++) {
404 for (
size_t k = 0; k < fBatchWidth; k++) {
405 size_t bufferIndex = i * fBatchHeight * fBatchWidth + k * fBatchHeight + j;
406 buffer[bufferIndex] = inputTensor[sampleIndex](j, k);
416 void TTensorDataLoader<TensorInput, TCpu<Double_t>>::CopyTensorOutput(TCpuBuffer<Double_t> &buffer,
417 IndexIterator_t sampleIterator)
419 const TMatrixT<Double_t> &outputMatrix = std::get<1>(fData);
420 size_t n = outputMatrix.GetNcols();
422 for (
size_t i = 0; i < fBatchSize; i++) {
423 size_t sampleIndex = *sampleIterator;
424 for (
size_t j = 0; j < n; j++) {
425 size_t bufferIndex = j * fBatchSize + i;
426 buffer[bufferIndex] = outputMatrix(sampleIndex, j);
434 void TTensorDataLoader<TensorInput, TCpu<Double_t>>::CopyTensorWeights(TCpuBuffer<Double_t> &buffer,
435 IndexIterator_t sampleIterator)
437 const TMatrixT<Double_t> &outputMatrix = std::get<2>(fData);
439 for (
size_t i = 0; i < fBatchSize; i++) {
440 size_t sampleIndex = *sampleIterator;
441 buffer[i] =
static_cast<Double_t
>(outputMatrix(sampleIndex, 0));
448 TTensorBatch<TCpu<Double_t> > TTensorDataLoader<TensorInput, TCpu<Double_t> >::GetTensorBatch()
452 DeviceBufferTuple DeviceBuffers = CopyTensorBatches();
454 Tensor_t inputTensor( std::get<0>(DeviceBuffers), { fBatchHeight, fBatchWidth, fBatchSize } );
461 Matrix_t outputMatrix(std::get<1>(DeviceBuffers), fBatchSize, fNOutputFeatures);
462 Matrix_t weightMatrix(std::get<2>(DeviceBuffers), fBatchSize, fNOutputFeatures);
465 return TTensorBatch<TCpu<Double_t> >(inputTensor, outputMatrix, weightMatrix);
473 void TTensorDataLoader<TMVAInput_t, TCpu<Double_t>>::CopyTensorInput(TCpuBuffer<Double_t> &buffer,
474 IndexIterator_t sampleIterator)
478 if (fBatchDepth == 1 && fBatchHeight == fBatchSize) {
479 for (
size_t i = 0; i < fBatchHeight; i++) {
480 size_t sampleIndex = *sampleIterator;
481 Event *
event = std::get<0>(fData)[sampleIndex];
482 for (
size_t j = 0; j < fBatchWidth; j++) {
483 size_t bufferIndex = j * fBatchHeight + i;
484 buffer[bufferIndex] =
event->GetValue(j);
488 }
else if (fBatchDepth == fBatchSize) {
490 for (
size_t i = 0; i < fBatchDepth; i++) {
491 size_t sampleIndex = *sampleIterator;
492 Event *
event = std::get<0>(fData)[sampleIndex];
493 for (
size_t j = 0; j < fBatchHeight; j++) {
494 for (
size_t k = 0; k < fBatchWidth; k++) {
496 size_t bufferIndex = i * fBatchHeight * fBatchWidth + k * fBatchHeight + j;
497 buffer[bufferIndex] =
event->GetValue(j * fBatchWidth + k);
504 Error(
"TTensorDataLoader",
"Inconsistency between batch depth and batch size");
511 void TTensorDataLoader<TMVAInput_t, TCpu<Double_t>>::CopyTensorOutput(TCpuBuffer<Double_t> &buffer,
512 IndexIterator_t sampleIterator)
514 const DataSetInfo &info = std::get<1>(fData);
515 size_t n = buffer.GetSize() / fBatchSize;
519 for (
size_t i = 0; i < fBatchSize; i++) {
520 size_t sampleIndex = *sampleIterator++;
521 Event *
event = std::get<0>(fData)[sampleIndex];
522 for (
size_t j = 0; j < n; j++) {
524 size_t bufferIndex = j * fBatchSize + i;
526 if (event->GetNTargets() == 0) {
529 buffer[bufferIndex] = (info.IsSignal(event)) ? 1.0 : 0.0;
532 buffer[bufferIndex] = 0.0;
533 if (j == event->GetClass()) {
534 buffer[bufferIndex] = 1.0;
538 buffer[bufferIndex] =
static_cast<Float_t
>(
event->GetTarget(j));
546 void TTensorDataLoader<TMVAInput_t, TCpu<Double_t>>::CopyTensorWeights(TCpuBuffer<Double_t> &buffer,
547 IndexIterator_t sampleIterator)
549 for (
size_t i = 0; i < fBatchSize; i++) {
550 size_t sampleIndex = *sampleIterator++;
551 Event *
event = std::get<0>(fData)[sampleIndex];
552 buffer[i] =
event->GetWeight();
559 TTensorBatch<TCpu<Double_t> > TTensorDataLoader<TMVAInput_t, TCpu<Double_t> >::GetTensorBatch()
563 DeviceBufferTuple DeviceBuffers = CopyTensorBatches();
566 Tensor_t inputTensor( std::get<0>(DeviceBuffers), { fBatchHeight, fBatchWidth, fBatchSize } );
572 Matrix_t outputMatrix(std::get<1>(DeviceBuffers), fBatchSize, fNOutputFeatures);
573 Matrix_t weightMatrix(std::get<2>(DeviceBuffers), fBatchSize, fNOutputFeatures);
576 return TTensorBatch<TCpu<Double_t> >(inputTensor, outputMatrix, weightMatrix);
583 void TTensorDataLoader<TMVAInput_t, TCpu<Float_t>>::CopyTensorInput(TCpuBuffer<Float_t> &buffer,
584 IndexIterator_t sampleIterator)
588 if (fBatchDepth == 1 && fBatchHeight == fBatchSize) {
589 for (
size_t i = 0; i < fBatchHeight; i++) {
590 size_t sampleIndex = *sampleIterator;
591 Event *
event = std::get<0>(fData)[sampleIndex];
592 for (
size_t j = 0; j < fBatchWidth; j++) {
593 size_t bufferIndex = j * fBatchHeight + i;
594 buffer[bufferIndex] =
event->GetValue(j);
598 }
else if (fBatchDepth == fBatchSize) {
600 for (
size_t i = 0; i < fBatchDepth; i++) {
601 size_t sampleIndex = *sampleIterator;
602 Event *
event = std::get<0>(fData)[sampleIndex];
603 for (
size_t j = 0; j < fBatchHeight; j++) {
604 for (
size_t k = 0; k < fBatchWidth; k++) {
606 size_t bufferIndex = i * fBatchHeight * fBatchWidth + k * fBatchHeight + j;
607 buffer[bufferIndex] =
event->GetValue(j * fBatchWidth + k);
614 Error(
"TTensorDataLoader",
"Inconsistency between batch depth and batch size");
621 void TTensorDataLoader<TMVAInput_t, TCpu<Float_t>>::CopyTensorOutput(TCpuBuffer<Float_t> &buffer,
622 IndexIterator_t sampleIterator)
624 const DataSetInfo &info = std::get<1>(fData);
625 size_t n = buffer.GetSize() / fBatchSize;
629 for (
size_t i = 0; i < fBatchSize; i++) {
630 size_t sampleIndex = *sampleIterator++;
631 Event *
event = std::get<0>(fData)[sampleIndex];
632 for (
size_t j = 0; j < n; j++) {
634 size_t bufferIndex = j * fBatchSize + i;
636 if (event->GetNTargets() == 0) {
639 buffer[bufferIndex] = (info.IsSignal(event)) ? 1.0 : 0.0;
642 buffer[bufferIndex] = 0.0;
643 if (j == event->GetClass()) {
644 buffer[bufferIndex] = 1.0;
648 buffer[bufferIndex] =
static_cast<Float_t
>(
event->GetTarget(j));
656 void TTensorDataLoader<TMVAInput_t, TCpu<Float_t>>::CopyTensorWeights(TCpuBuffer<Float_t> &buffer,
657 IndexIterator_t sampleIterator)
659 for (
size_t i = 0; i < fBatchSize; i++) {
660 size_t sampleIndex = *sampleIterator++;
661 Event *
event = std::get<0>(fData)[sampleIndex];
662 buffer[i] =
event->GetWeight();
669 TTensorBatch<TCpu<Float_t> > TTensorDataLoader<TMVAInput_t, TCpu<Float_t> >::GetTensorBatch()
673 DeviceBufferTuple DeviceBuffers = CopyTensorBatches();
675 Tensor_t inputTensor( std::get<0>(DeviceBuffers), { fBatchHeight, fBatchWidth, fBatchSize } );
682 Matrix_t outputMatrix(std::get<1>(DeviceBuffers), fBatchSize, fNOutputFeatures);
683 Matrix_t weightMatrix(std::get<2>(DeviceBuffers), fBatchSize, fNOutputFeatures);
686 return TTensorBatch<TCpu<Float_t> >(inputTensor, outputMatrix, weightMatrix);
692 template class TCpuBuffer<Double_t>;
693 template class TCpuBuffer<Float_t>;
695 template class TTensorDataLoader<TensorInput, TCpu<Float_t>>;
696 template class TTensorDataLoader<TMVAInput_t, TCpu<Float_t>>;
697 template class TTensorDataLoader<TensorInput, TCpu<Double_t>>;
698 template class TTensorDataLoader<TMVAInput_t, TCpu<Double_t>>;