ATLAS Offline Software
FastInputPreprocessor.cxx
Go to the documentation of this file.
1 // this is -*- C++ -*-
2 /*
3  Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
4 */
5 
6 // Any modifications to this file may be copied to lwtnn[1] without
7 // attribution.
8 //
9 // [1]: https::www.github.com/lwtnn/lwtnn
10 
12 #include "lwtnn/Exceptions.hh"
13 
14 namespace {
15  // utility functions
16  //
17  // Build a mapping from the inputs in the saved network to the
18  // inputs that the user is going to hand us.
19  std::vector<size_t> get_value_indices(
20  const std::vector<std::string>& order,
21  const std::vector<lwt::Input>& inputs)
22  {
23  std::map<std::string, size_t> order_indices;
24  for (size_t i = 0; i < order.size(); i++) {
25  order_indices[order.at(i)] = i;
26  }
27  std::vector<size_t> value_indices;
28  for (const lwt::Input& input: inputs) {
29  if (!order_indices.count(input.name)) {
30  throw lwt::NNConfigurationException("Missing input " + input.name);
31  }
32  value_indices.push_back(order_indices.at(input.name));
33  }
34  return value_indices;
35  }
36 
37 }
38 
39 namespace lwt::atlas {
40  // ______________________________________________________________________
41  // FastInput preprocessors
42 
43  // simple feed-forwared version
45  const std::vector<Input>& inputs,
46  const std::vector<std::string>& order):
47  m_offsets(inputs.size()),
48  m_scales(inputs.size())
49  {
50  size_t in_num = 0;
51  for (const auto& input: inputs) {
52  m_offsets(in_num) = input.offset;
53  m_scales(in_num) = input.scale;
54  in_num++;
55  }
56  m_indices = get_value_indices(order, inputs);
57  }
58  VectorXd FastInputPreprocessor::operator()(const VectorXd& in) const {
59  VectorXd invec(m_indices.size());
60  size_t input_number = 0;
61  for (size_t index: m_indices) {
62  if (static_cast<int>(index) >= in.rows()) {
63  throw NNEvaluationException(
64  "index " + std::to_string(index) + " is out of range, scalar "
65  "input only has " + std::to_string(in.rows()) + " entries");
66  }
67  invec(input_number) = in(index);
68  input_number++;
69  }
70  return (invec + m_offsets).cwiseProduct(m_scales);
71  }
72 
73 
74  // Input vector preprocessor
76  const std::vector<Input>& inputs,
77  const std::vector<std::string>& order):
78  m_offsets(inputs.size()),
79  m_scales(inputs.size())
80  {
81  size_t in_num = 0;
82  for (const auto& input: inputs) {
83  m_offsets(in_num) = input.offset;
84  m_scales(in_num) = input.scale;
85  in_num++;
86  }
87  // require at least one input at configuration, since we require
88  // at least one for evaluation
89  if (in_num == 0) {
90  throw NNConfigurationException("need at least one input");
91  }
92  m_indices = get_value_indices(order, inputs);
93  }
94  MatrixXd FastInputVectorPreprocessor::operator()(const MatrixXd& in) const {
95  using namespace Eigen;
96  size_t n_cols = in.cols();
97  MatrixXd inmat(m_indices.size(), n_cols);
98  size_t in_num = 0;
99  for (size_t index: m_indices) {
100  if (static_cast<int>(index) >= in.rows()) {
101  throw NNEvaluationException(
102  "index " + std::to_string(index) + " is out of range, sequence "
103  "input only has " + std::to_string(in.rows()) + " entries");
104  }
105  inmat.row(in_num) = in.row(index);
106  in_num++;
107  }
108  if (n_cols == 0) {
109  return MatrixXd(m_indices.size(), 0);
110  }
111  return m_scales.asDiagonal() * (inmat.colwise() + m_offsets);
112  }
113 
114 }
lwt::atlas::FastInputPreprocessor::m_indices
std::vector< size_t > m_indices
Definition: FastInputPreprocessor.h:42
index
Definition: index.py:1
lwt::atlas::FastInputPreprocessor::m_scales
VectorXd m_scales
Definition: FastInputPreprocessor.h:41
postInclude.inputs
inputs
Definition: postInclude.SortInput.py:15
lwt::atlas::FastInputVectorPreprocessor::m_scales
VectorXd m_scales
Definition: FastInputPreprocessor.h:54
python.setupRTTAlg.size
int size
Definition: setupRTTAlg.py:39
lwt::atlas::FastInputPreprocessor::FastInputPreprocessor
FastInputPreprocessor(const std::vector< Input > &inputs, const std::vector< std::string > &order)
Definition: FastInputPreprocessor.cxx:44
lwt::atlas::FastInputVectorPreprocessor::FastInputVectorPreprocessor
FastInputVectorPreprocessor(const std::vector< Input > &inputs, const std::vector< std::string > &order)
Definition: FastInputPreprocessor.cxx:75
lumiFormat.i
int i
Definition: lumiFormat.py:85
mc.order
order
Configure Herwig7.
Definition: mc.Herwig7_Dijet.py:12
PlotPulseshapeFromCool.input
input
Definition: PlotPulseshapeFromCool.py:106
lwt::atlas::FastInputVectorPreprocessor::m_offsets
VectorXd m_offsets
Definition: FastInputPreprocessor.h:53
lwt::atlas
Ensure that the extensions for the Vector3D are properly loaded.
Definition: LWTNNCondAlg.h:24
lwt::atlas::FastInputPreprocessor::m_offsets
VectorXd m_offsets
Definition: FastInputPreprocessor.h:40
lwt::atlas::FastInputVectorPreprocessor::m_indices
std::vector< size_t > m_indices
Definition: FastInputPreprocessor.h:55
lwt::atlas::FastInputPreprocessor::operator()
VectorXd operator()(const VectorXd &) const
Definition: FastInputPreprocessor.cxx:58
ActsTrk::to_string
std::string to_string(const DetectorType &type)
Definition: GeometryDefs.h:34
lwt::atlas::FastInputVectorPreprocessor::operator()
MatrixXd operator()(const MatrixXd &) const
Definition: FastInputPreprocessor.cxx:94
Input
NswErrorCalibData::Input Input
Definition: NswErrorCalibData.cxx:6
FastInputPreprocessor.h