ATLAS Offline Software
Loading...
Searching...
No Matches
InputPreprocessor.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2019 CERN for the benefit of the ATLAS collaboration
3*/
4
6
7namespace lwtDev {
8 // ______________________________________________________________________
9 // Input preprocessors
10
11 // simple feed-forwared version
12 InputPreprocessor::InputPreprocessor(const std::vector<Input>& inputs):
13 m_offsets(inputs.size()),
14 m_scales(inputs.size())
15 {
16 size_t in_num = 0;
17 for (const auto& input: inputs) {
18 m_offsets(in_num) = input.offset;
19 m_scales(in_num) = input.scale;
20 m_names.push_back(input.name);
21 in_num++;
22 }
23 }
24 VectorXd InputPreprocessor::operator()(const ValueMap& in) const {
25 VectorXd invec(m_names.size());
26 size_t input_number = 0;
27 for (const auto& in_name: m_names) {
28 if (!in.count(in_name)) {
29 throw NNEvaluationException("can't find input: " + in_name);
30 }
31 invec(input_number) = in.at(in_name);
32 input_number++;
33 }
34 return (invec + m_offsets).cwiseProduct(m_scales);
35 }
36
37
38 // Input vector preprocessor
40 const std::vector<Input>& inputs):
41 m_offsets(inputs.size()),
42 m_scales(inputs.size())
43 {
44 size_t in_num = 0;
45 for (const auto& input: inputs) {
46 m_offsets(in_num) = input.offset;
47 m_scales(in_num) = input.scale;
48 m_names.push_back(input.name);
49 in_num++;
50 }
51 // require at least one input at configuration, since we require
52 // at least one for evaluation
53 if (in_num == 0) {
54 throw NNConfigurationException("need at least one input");
55 }
56 }
58 using namespace Eigen;
59 if (in.size() == 0) {
60 throw NNEvaluationException("Empty input map");
61 }
62 size_t n_cols = in.begin()->second.size();
63 MatrixXd inmat(m_names.size(), n_cols);
64 size_t in_num = 0;
65 for (const auto& in_name: m_names) {
66 if (!in.count(in_name)) {
67 throw NNEvaluationException("can't find input: " + in_name);
68 }
69 const auto& invec = in.at(in_name);
70 if (invec.size() != n_cols) {
71 throw NNEvaluationException("Input vector size mismatch");
72 }
73 inmat.row(in_num) = Map<const VectorXd>(invec.data(), invec.size());
74 in_num++;
75 }
76 if (n_cols == 0) {
77 return MatrixXd(m_names.size(), 0);
78 }
79 return m_scales.asDiagonal() * (inmat.colwise() + m_offsets);
80 }
81
82}
std::vector< std::string > m_names
VectorXd operator()(const ValueMap &) const
InputPreprocessor(const std::vector< Input > &inputs)
MatrixXd operator()(const VectorMap &) const
std::vector< std::string > m_names
InputVectorPreprocessor(const std::vector< Input > &inputs)
std::map< std::string, std::vector< double > > VectorMap
std::map< std::string, double > ValueMap