LBANN  0.103.0
LivermoreBigArtificialNeuralNetworkToolkit
data_reader_numpy.hpp
Go to the documentation of this file.
1 // Copyright (c) 2014-2023, Lawrence Livermore National Security, LLC.
3 // Produced at the Lawrence Livermore National Laboratory.
4 // Written by the LBANN Research Team (B. Van Essen, et al.) listed in
5 // the CONTRIBUTORS file. <lbann-dev@llnl.gov>
6 //
7 // LLNL-CODE-697807.
8 // All rights reserved.
9 //
10 // This file is part of LBANN: Livermore Big Artificial Neural Network
11 // Toolkit. For details, see http://software.llnl.gov/LBANN or
12 // https://github.com/LLNL/LBANN.
13 //
14 // Licensed under the Apache License, Version 2.0 (the "Licensee"); you
15 // may not use this file except in compliance with the License. You may
16 // obtain a copy of the License at:
17 //
18 // http://www.apache.org/licenses/LICENSE-2.0
19 //
20 // Unless required by applicable law or agreed to in writing, software
21 // distributed under the License is distributed on an "AS IS" BASIS,
22 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
23 // implied. See the License for the specific language governing
24 // permissions and limitations under the license.
25 //
26 // lbann_data_reader_numpy .hpp .cpp - generic_data_reader class for numpy
27 // dataset
29 
30 #ifndef LBANN_DATA_READER_NUMPY_HPP
31 #define LBANN_DATA_READER_NUMPY_HPP
32 
33 #include "data_reader.hpp"
34 #include <cnpy.h>
35 
36 namespace lbann {
37 
46 {
47 public:
48  numpy_reader(bool shuffle = true);
49  // These need to be explicit because of some issue with the cnpy copy
50  // constructor/assignment operator not linking correctly otherwise.
51  numpy_reader(const numpy_reader&);
53  ~numpy_reader() override {}
54 
55  numpy_reader* copy() const override { return new numpy_reader(*this); }
56 
57  std::string get_type() const override { return "numpy_reader"; }
58 
59  void load() override;
60 
61  int get_num_labels() const override { return m_num_labels; }
62  int get_linearized_data_size() const override { return m_num_features; }
63  int get_linearized_label_size() const override { return m_num_labels; }
64  const std::vector<El::Int> get_data_dims() const override
65  {
66  std::vector<El::Int> dims(m_data.shape.begin() + 1, m_data.shape.end());
69  dims.back() -= 1;
70  }
71  return dims;
72  }
73 
74 protected:
75  bool fetch_datum(CPUMat& X, int data_id, int mb_idx) override;
76  bool fetch_label(CPUMat& Y, int data_id, int mb_idx) override;
77  bool fetch_response(CPUMat& Y, int data_id, int mb_idx) override;
78 
80  int m_num_samples = 0;
82  int m_num_features = 0;
84  int m_num_labels = 0;
90  cnpy::NpyArray m_data;
91 };
92 
93 } // namespace lbann
94 
95 #endif // LBANN_DATA_READER_NUMPY_HPP
int get_linearized_data_size() const override
Get the linearized size (i.e. number of elements) in a sample.
const std::vector< El::Int > get_data_dims() const override
Get the dimensions of the data.
numpy_reader * copy() const override
int m_num_labels
Number of label classes.
std::map< data_field_type, bool > m_supported_input_types
Holds a true value for each input data type that is supported. Use an ordered map so that checkpoints...
#define INPUT_DATA_TYPE_LABELS
bool fetch_label(CPUMat &Y, int data_id, int mb_idx) override
int m_num_samples
Number of samples.
int get_linearized_label_size() const override
Get the linearized size (i.e. number of elements) in a label.
void load() override
bool fetch_datum(CPUMat &X, int data_id, int mb_idx) override
El::Matrix< DataType, El::Device::CPU > CPUMat
Definition: base.hpp:116
bool fetch_response(CPUMat &Y, int data_id, int mb_idx) override
#define INPUT_DATA_TYPE_RESPONSES
std::string get_type() const override
int get_num_labels() const override
Return the number of labels (classes) in this dataset.
int m_num_features
Number of features in each sample.
numpy_reader & operator=(const numpy_reader &)
numpy_reader(bool shuffle=true)