17 #include "TMVA/DNN/Architectures/Cpu/DataLoader.h" 28 template<
typename Data_t,
typename Real_t>
29 TCpuBatchIterator<Data_t, Real_t>::TCpuBatchIterator(
30 TCpuDataLoader<Data_t, Real_t> & dataLoader,
32 : fDataLoader(dataLoader), fBatchIndex(batchIndex)
38 template<
typename Data_t,
typename Real_t>
41 return fDataLoader.GetBatch(fBatchIndex);
45 template<
typename Data_t,
typename Real_t>
46 TCpuBatchIterator<Data_t, Real_t> & TCpuBatchIterator<Data_t, Real_t>::operator++()
53 template<
typename Data_t,
typename Real_t>
56 return fBatchIndex != other.GetBatchIndex();
60 template<
typename Data_t,
typename Real_t>
63 return fBatchIndex == other.GetBatchIndex();
68 template<
typename Data_t,
typename Real_t>
69 TCpuDataLoader<Data_t, Real_t>::TCpuDataLoader(
const Data_t &input,
72 size_t ninputFeatures,
73 size_t noutputFeatures,
75 : fInput(input), fNSamples(nsamples), fBatchSize(batchSize),
76 fBufferSize(bufferSize), fNInputFeatures(ninputFeatures),
77 fNOutputFeatures(noutputFeatures), fNBatches(nsamples / batchSize),
78 fInputMatrices(), fOutputMatrices(), fSampleIndices()
80 fInputMatrices.reserve(fBufferSize);
81 fOutputMatrices.reserve(fBufferSize);
82 for (
size_t i = 0; i < fBufferSize; i++) {
83 fInputMatrices.emplace_back(fBatchSize, fNInputFeatures);
84 fOutputMatrices.emplace_back(fBatchSize, fNOutputFeatures);
87 fSampleIndices.reserve(fNBatches);
88 for (
size_t i = 0; i < fNSamples; i++) {
89 fSampleIndices.emplace_back(i);
94 template<
typename Data_t,
typename Real_t>
95 inline void TCpuDataLoader<Data_t, Real_t>::CopyData(
size_t batchIndex)
97 auto copy = [
this](
UInt_t workerID)
99 CopyBatch(this->fInputMatrices[workerID % this->fBufferSize],
100 this->fOutputMatrices[workerID % this->fBufferSize],
102 this->fSampleIndices.begin() + sampleIndex,
103 this->fSampleIndices.begin() + sampleIndex + this->fBatchSize);
104 sampleIndex += this->fBatchSize;
108 size_t end = std::min(batchIndex + fBufferSize, fNBatches);
109 size_t start = batchIndex;
115 template<
typename Data_t,
typename Real_t>
116 TCpuBatch<Real_t> TCpuDataLoader<Data_t, Real_t>::GetBatch(
size_t batchIndex)
118 size_t fBufferIndex = batchIndex % fBufferSize;
119 if (fBufferIndex == 0) {
120 CopyData(batchIndex);
122 return TCpuBatch<Real_t>(fInputMatrices[fBufferIndex],
123 fOutputMatrices[fBufferIndex]);
127 template<
typename Data_t,
typename Real_t>
128 auto TCpuDataLoader<Data_t, Real_t>::begin()
131 random_shuffle(fSampleIndices.begin(), fSampleIndices.end());
132 return BatchIterator_t(*
this, 0);
136 template<
typename Data_t,
typename Real_t>
137 auto TCpuDataLoader<Data_t, Real_t>::end()
140 return BatchIterator_t(*
this, fNBatches);
145 void TCpuDataLoader<MatrixInput_t, Double_t>::CopyBatch(
146 Matrix_t &inputMatrix,
147 Matrix_t &outputMatrix,
152 auto &in = std::get<0>(input);
153 auto &out = std::get<1>(input);
155 size_t batchIndex = 0;
158 for (
size_t j = 0; j < (size_t) in.GetNcols(); j++) {
159 inputMatrix(batchIndex, j) = in(index, j);
161 for (
size_t j = 0; j < (size_t) out.GetNcols(); j++) {
162 outputMatrix(batchIndex, j) = out(index, j);
170 void TCpuDataLoader<TMVAInput_t, Double_t>::CopyBatch(
171 Matrix_t &inputMatrix,
172 Matrix_t &outputMatrix,
177 size_t batchIndex = 0;
180 Event *
event = input.at(index);
181 for (
size_t j = 0; j <
event->GetNVariables(); j++) {
182 inputMatrix(batchIndex, j) =
event->GetValue(j);
184 if (event->GetNTargets() > 0) {
185 for (
size_t j = 0; j <
event->GetNTargets(); j++) {
186 outputMatrix(batchIndex, j) =
event->GetTarget(j);
189 outputMatrix(batchIndex, 0) = (
event->GetClass() == 0) ? 1.0 : 0.0;
197 template class TCpuDataLoader<MatrixInput_t, Double_t>;
198 template class TCpuDataLoader<TMVAInput_t, Double_t>;
199 template class TCpuBatchIterator<MatrixInput_t, Double_t>;
200 template class TCpuBatchIterator<TMVAInput_t, Double_t>;
201 template class TCpuBatch<Double_t>;
typename std::vector< size_t >::iterator IndexIterator_t
Bool_t operator!=(const TDatime &d1, const TDatime &d2)
This class provides a simple interface to execute the same task multiple times in parallel...
std::vector< Event * > TMVAInput_t
TTime operator*(const TTime &t1, const TTime &t2)
A pseudo container class which is a generator of indices.
Bool_t operator==(const TDatime &d1, const TDatime &d2)
Abstract ClassifierFactory template that handles arbitrary types.
auto Map(F func, unsigned nTimes) -> std::vector< typename std::result_of< F()>::type >
Execute func (with no arguments) nTimes in parallel.
std::pair< const TMatrixT< Double_t > &, const TMatrixT< Double_t > & > MatrixInput_t