Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:10:52

0001 // @(#)root/tmva/tmva/dnn:$Id$
0002 // Author: Vladimir Ilievski
0003 
0004 /**********************************************************************************
0005  * Project: TMVA - a Root-integrated toolkit for multivariate data analysis       *
0006  * Package: TMVA                                                                  *
0007  * Class  : TTensorDataLoader                                                     *
0008  *                                             *
0009  *                                                                                *
0010  * Description:                                                                   *
0011  *      Specialization of the Tensor Data Loader Class                            *
0012  *                                                                                *
0013  * Authors (alphabetical):                                                        *
0014  *      Vladimir Ilievski      <ilievski.vladimir@live.com>  - CERN, Switzerland  *
0015  *                                                                                *
0016  * Copyright (c) 2005-2015:                                                       *
0017  *      CERN, Switzerland                                                         *
0018  *      U. of Victoria, Canada                                                    *
0019  *      MPI-K Heidelberg, Germany                                                 *
0020  *      U. of Bonn, Germany                                                       *
0021  *                                                                                *
0022  * Redistribution and use in source and binary forms, with or without             *
0023  * modification, are permitted according to the terms listed in LICENSE           *
0024  * (see tmva/doc/LICENSE)                                          *
0025  **********************************************************************************/
0026 
0027 //////////////////////////////////////////////////////////////////////////
0028 // Partial specialization of the TTensorDataLoader class to adapt       //
0029 // it to the TMatrix class. Also the data transfer is kept simple,      //
0030 // since this implementation (being intended as reference and fallback) //
0031 // is not optimized for performance.                                    //
0032 //////////////////////////////////////////////////////////////////////////
0033 
0034 #ifndef TMVA_DNN_ARCHITECTURES_REFERENCE_TENSORDATALOADER
0035 #define TMVA_DNN_ARCHITECTURES_REFERENCE_TENSORDATALOADER
0036 
0037 #include "TMVA/DNN/TensorDataLoader.h"
0038 #include <iostream>
0039 
0040 namespace TMVA {
0041 namespace DNN {
0042 
0043 template <typename AReal>
0044 class TReference;
0045 
0046 template <typename AData, typename AReal>
0047 class TTensorDataLoader<AData, TReference<AReal>> {
0048 private:
0049    using BatchIterator_t = TTensorBatchIterator<AData, TReference<AReal>>;
0050 
0051    const AData &fData;      ///< The data that should be loaded in the batches.
0052 
0053    size_t fNSamples;        ///< The total number of samples in the dataset.
0054    //size_t fBatchSize;     ///< The size of a batch.
0055    size_t fBatchDepth;      ///< The number of matrices in the tensor.
0056    size_t fBatchHeight;     ///< The number od rows in each matrix.
0057    size_t fBatchWidth;      ///< The number of columns in each matrix.
0058    size_t fNOutputFeatures; ///< The number of outputs from the classifier/regressor.
0059    size_t fBatchIndex;      ///< The index of the batch when there are multiple batches in parallel.
0060 
0061    std::vector<size_t> fInputShape;     ///< Defines the batch depth, no. of channels and spatial dimensions of an input tensor
0062 
0063    std::vector<TMatrixT<AReal>> inputTensor; ///< The 3D tensor used to keep the input data.
0064    TMatrixT<AReal> outputMatrix;             ///< The matrix used to keep the output.
0065    TMatrixT<AReal> weightMatrix;             ///< The matrix used to keep the batch weights.
0066 
0067    std::vector<size_t> fSampleIndices; ///< Ordering of the samples in the epoch.
0068 
0069 public:
0070    /*! Constructor. */
0071    TTensorDataLoader(const AData &data, size_t nSamples, size_t batchDepth,
0072                      size_t batchHeight, size_t batchWidth, size_t nOutputFeatures,
0073                      std::vector<size_t> inputShape, size_t nStreams = 1);
0074 
0075    TTensorDataLoader(const TTensorDataLoader &) = default;
0076    TTensorDataLoader(TTensorDataLoader &&) = default;
0077    TTensorDataLoader &operator=(const TTensorDataLoader &) = default;
0078    TTensorDataLoader &operator=(TTensorDataLoader &&) = default;
0079 
0080    /** Copy input tensor into the given host buffer. Function to be specialized by
0081     *  the architecture-specific backend. */
0082    void CopyTensorInput(std::vector<TMatrixT<AReal>> &tensor, IndexIterator_t sampleIterator);
0083    /** Copy output matrix into the given host buffer. Function to be specialized
0084     * by the architecture-specific backend. */
0085    void CopyTensorOutput(TMatrixT<AReal> &matrix, IndexIterator_t sampleIterator);
0086    /** Copy weight matrix into the given host buffer. Function to be specialized
0087     * by the architecture-specific backend. */
0088    void CopyTensorWeights(TMatrixT<AReal> &matrix, IndexIterator_t sampleIterator);
0089 
0090    BatchIterator_t begin() { return BatchIterator_t(*this); }
0091    BatchIterator_t end() { return BatchIterator_t(*this, fNSamples / fInputShape[0]); }
0092 
0093    /** Shuffle the order of the samples in the batch. The shuffling is indirect,
0094     *  i.e. only the indices are shuffled. No input data is moved by this
0095     * routine. */
0096    template<typename RNG>
0097    void Shuffle(RNG & rng);
0098 
0099    /** Return the next batch from the training set. The TTensorDataLoader object
0100     *  keeps an internal counter that cycles over the batches in the training
0101     *  set. */
0102    TTensorBatch<TReference<AReal>> GetTensorBatch();
0103 };
0104 
0105 //
0106 // TTensorDataLoader Class.
0107 //______________________________________________________________________________
0108 template <typename AData, typename AReal>
0109 TTensorDataLoader<AData, TReference<AReal>>::TTensorDataLoader(const AData &data, size_t nSamples, size_t batchDepth,
0110                                                                size_t batchHeight, size_t batchWidth, size_t nOutputFeatures,
0111                                                                std::vector<size_t> inputShape, size_t /* nStreams */)
0112    : fData(data), fNSamples(nSamples), fBatchDepth(batchDepth), fBatchHeight(batchHeight),
0113      fBatchWidth(batchWidth), fNOutputFeatures(nOutputFeatures), fBatchIndex(0), fInputShape(std::move(inputShape)), inputTensor(),
0114      outputMatrix(inputShape[0], nOutputFeatures), weightMatrix(inputShape[0], 1), fSampleIndices()
0115 {
0116 
0117    inputTensor.reserve(fBatchDepth);
0118    for (size_t i = 0; i < fBatchDepth; i++) {
0119       inputTensor.emplace_back(batchHeight, batchWidth);
0120    }
0121 
0122    fSampleIndices.reserve(fNSamples);
0123    for (size_t i = 0; i < fNSamples; i++) {
0124       fSampleIndices.push_back(i);
0125    }
0126 }
0127 
0128 template <typename AData, typename AReal>
0129 template <typename RNG>
0130 void TTensorDataLoader<AData, TReference<AReal>>::Shuffle(RNG & rng)
0131 {
0132    std::shuffle(fSampleIndices.begin(), fSampleIndices.end(), rng);
0133 }
0134 
0135 template <typename AData, typename AReal>
0136 auto TTensorDataLoader<AData, TReference<AReal>>::GetTensorBatch() -> TTensorBatch<TReference<AReal>>
0137 {
0138    fBatchIndex %= (fNSamples / fInputShape[0]); // Cycle through samples.
0139 
0140    size_t sampleIndex = fBatchIndex * fInputShape[0];
0141    IndexIterator_t sampleIndexIterator = fSampleIndices.begin() + sampleIndex;
0142 
0143    CopyTensorInput(inputTensor, sampleIndexIterator);
0144    CopyTensorOutput(outputMatrix, sampleIndexIterator);
0145    CopyTensorWeights(weightMatrix, sampleIndexIterator);
0146 
0147    fBatchIndex++;
0148    return TTensorBatch<TReference<AReal>>(inputTensor, outputMatrix, weightMatrix);
0149 }
0150 
0151 } // namespace DNN
0152 } // namespace TMVA
0153 
0154 #endif