ATLAS Offline Software
Tracking/TrkUtilityPackages/TrkNeuralNetworkUtils/TrkNeuralNetworkUtils/TTrainedNetwork.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 //-*-c++-*-
6 #ifndef TRKNEURALNETWORKUTILS_TTRAINEDNETWORK_H
7 #define TRKNEURALNETWORKUTILS_TTRAINEDNETWORK_H
8 
9 #include "TObject.h"
10 #include "TMatrixD.h"
11 #include "TVectorD.h"
12 #include <math.h>
13 #include <vector>
14 #include <string>
15 #include <map>
16 
17 /******************************************************
18  @class TTrainedNetwork
19  Created : 18-02-2008
20  @author Giacinto Piacquadio (giacinto.piacquadio AT physik.uni-freiburg.de)
21 ********************************************************/
22 
23 namespace nnopt {
24 }
25 
26 class TTrainedNetwork : public TObject
27 {
28 public:
29 
30  static const unsigned linearOutput = 1u << 0;
31  static const unsigned normalizeOutput = 1u << 1;
32 
34  SIGMOID = 1
35  };
36 
37  struct Input {
38  std::string name; //<- requires unique strings or none at all
39  double offset; //<- this value is added to the input before giving to nn
40  double scale; //<- after offset is added, input is scaled by this value
41  };
42 
43  typedef std::vector<Double_t> DVec;
44  typedef std::map<std::string, double> DMap;
45  typedef DMap::const_iterator DMapI;
46 
48 
49  //NOTE: class takes ownership of all pointers.
50 
51  // old-school constructor (for compatability)
52  TTrainedNetwork(Int_t nInput,
53  Int_t nHidden,
54  Int_t nOutput,
55  std::vector<Int_t> & nHiddenLayerSize,
56  std::vector<TVectorD*> & thresholdVectors,
57  std::vector<TMatrixD*> & weightMatrices,
58  Int_t activationFunction,
59  bool linearOutput = false,
60  bool normalizeOutput = false);
61 
62  // You can also add offsets and scales.
63  // This is intended as a workaround for older code that didn't include
64  // normalization.
65  void setOffsets(const std::vector<double>& offsets);
66  void setScales(const std::vector<double>& scales);
67 
68  // new constructor for normalized nn.
69  // This avoids some chances for logical inconsistency by constructing
70  // the hidden layer size from the thresholdVectors and weightMatrices.
71  // Also runs a consistency check on thresholdVectors and weightMatrices.
72  TTrainedNetwork(const std::vector<TTrainedNetwork::Input>& inputs,
73  unsigned nOutput,
74  std::vector<TVectorD*> & thresholdVectors,
75  std::vector<TMatrixD*> & weightMatrices,
76  ActivationFunction activationFunction = SIGMOID,
77  unsigned options = 0);
78 
80 
81  // returns an empty vector if normalization isn't set
82  std::vector<Input> getInputs() const;
83 
84  void setNewWeights(std::vector<TVectorD*> & thresholdVectors,
85  std::vector<TMatrixD*> & weightMatrices);
86 
87  Int_t getnInput() const {return m_nInput;};
88 
89  Int_t getnHidden() const {return m_nHidden;};
90 
91  Int_t getnOutput() const {return m_nOutput;};
92 
93  const std::vector<Int_t> & getnHiddenLayerSize() const {
94  return m_nHiddenLayerSize;
95  };
96 
98 
99  const std::vector<TVectorD*> & getThresholdVectors() const {
100  return m_ThresholdVectors;
101  };
102 
103  const std::vector<TMatrixD*> & weightMatrices() const {
104  return m_WeightMatrices;
105  };
106 
107  // these methods should be optimized.
108  DVec calculateOutputValues(const DVec & input) const;
109  DVec calculateNormalized(const DVec& input) const;
110 
111  // these are intentionally slow: the NN must
112  // rearrange the inputs each time these functions are called.
113  // They are designed for robustness and ease of use, not for highly
114  // optimized code.
115  DVec calculateNormalized(const DMap & input) const;
116 
117  bool getIfLinearOutput() const { return m_LinearOutput; };
118 
119  bool getIfNormalizeOutput() const { return m_NormalizeOutput; };
120 
121 private:
122 
123 
124  unsigned m_nInput;
125  unsigned m_nHidden;
126  unsigned m_nOutput;
127 
128  // in an ideal world these would be one object in a vector, but
129  // storing classes within classes in root is ugly
130  std::vector<Double_t> m_input_node_offset;
131  std::vector<Double_t> m_input_node_scale;
132 
133  std::map<std::string,int> m_inputStringToNode;
134 
135  std::vector<Int_t> m_nHiddenLayerSize;
136 
137  std::vector<TVectorD*> m_ThresholdVectors;
138  std::vector<TMatrixD*> m_WeightMatrices;
139 
140  unsigned int m_bufferSizeMax;
141 
142  Int_t m_ActivationFunction;
143 
144  bool m_LinearOutput;
145 
146  bool m_NormalizeOutput;
147 
149 
150  Double_t sigmoid(Double_t x) const;
151 
152  // assertions to check for nonsense initialization
153  bool is_consistent() const;
154  bool check_norm_size(unsigned size) const;
155 
156 
157  ClassDef( TTrainedNetwork, 3 )
158 
159 };
160 
161 #endif
TTrainedNetwork::Input
Definition: Tracking/TrkUtilityPackages/TrkNeuralNetworkUtils/TrkNeuralNetworkUtils/TTrainedNetwork.h:37
TTrainedNetwork::getnInput
Int_t getnInput() const
Definition: Tracking/TrkUtilityPackages/TrkNeuralNetworkUtils/TrkNeuralNetworkUtils/TTrainedNetwork.h:87
TTrainedNetwork::calculateOutputValues
std::vector< Double_t > calculateOutputValues(std::vector< Double_t > &input) const
Definition: InnerDetector/InDetCalibAlgs/PixelCalibAlgs/NNClusteringCalibration_RunI/TTrainedNetwork.cxx:99
TTrainedNetwork::ActivationFunction
ActivationFunction
Definition: Tracking/TrkUtilityPackages/TrkNeuralNetworkUtils/TrkNeuralNetworkUtils/TTrainedNetwork.h:33
TTrainedNetwork::m_nHiddenLayerSize
std::vector< Int_t > m_nHiddenLayerSize
Definition: InnerDetector/InDetCalibAlgs/PixelCalibAlgs/NNClusteringCalibration_RunI/TTrainedNetwork.h:72
TTrainedNetwork::DMapI
DMap::const_iterator DMapI
Definition: Tracking/TrkUtilityPackages/TrkNeuralNetworkUtils/TrkNeuralNetworkUtils/TTrainedNetwork.h:45
TTrainedNetwork::m_LinearOutput
bool m_LinearOutput
Definition: InnerDetector/InDetCalibAlgs/PixelCalibAlgs/NNClusteringCalibration_RunI/TTrainedNetwork.h:81
TTrainedNetwork::m_nOutput
unsigned m_nOutput
Definition: Tracking/TrkUtilityPackages/TrkNeuralNetworkUtils/TrkNeuralNetworkUtils/TTrainedNetwork.h:126
TTrainedNetwork::getActivationFunction
Int_t getActivationFunction() const
Definition: Tracking/TrkUtilityPackages/TrkNeuralNetworkUtils/TrkNeuralNetworkUtils/TTrainedNetwork.h:97
TTrainedNetwork::m_nInput
Int_t m_nInput
Definition: InnerDetector/InDetCalibAlgs/PixelCalibAlgs/NNClusteringCalibration_RunI/TTrainedNetwork.h:64
TTrainedNetwork::Input::offset
double offset
Definition: Tracking/TrkUtilityPackages/TrkNeuralNetworkUtils/TrkNeuralNetworkUtils/TTrainedNetwork.h:39
TTrainedNetwork::getnHiddenLayerSize
const std::vector< Int_t > & getnHiddenLayerSize() const
Definition: Tracking/TrkUtilityPackages/TrkNeuralNetworkUtils/TrkNeuralNetworkUtils/TTrainedNetwork.h:93
TTrainedNetwork::m_NormalizeOutput
bool m_NormalizeOutput
Definition: InnerDetector/InDetCalibAlgs/PixelCalibAlgs/NNClusteringCalibration_RunI/TTrainedNetwork.h:83
TTrainedNetwork::getIfNormalizeOutput
bool getIfNormalizeOutput() const
Definition: Tracking/TrkUtilityPackages/TrkNeuralNetworkUtils/TrkNeuralNetworkUtils/TTrainedNetwork.h:119
x
#define x
TTrainedNetwork::getnHidden
Int_t getnHidden() const
Definition: Tracking/TrkUtilityPackages/TrkNeuralNetworkUtils/TrkNeuralNetworkUtils/TTrainedNetwork.h:89
postInclude.inputs
inputs
Definition: postInclude.SortInput.py:15
TTrainedNetwork::weightMatrices
const std::vector< TMatrixD * > & weightMatrices() const
Definition: InnerDetector/InDetCalibAlgs/PixelCalibAlgs/NNClusteringCalibration_RunI/TTrainedNetwork.h:58
Trk::u
@ u
Enums for curvilinear frames.
Definition: ParamDefs.h:83
TTrainedNetwork::m_inputStringToNode
std::map< std::string, int > m_inputStringToNode
Definition: Tracking/TrkUtilityPackages/TrkNeuralNetworkUtils/TrkNeuralNetworkUtils/TTrainedNetwork.h:133
TTrainedNetwork::m_nHidden
Int_t m_nHidden
Definition: InnerDetector/InDetCalibAlgs/PixelCalibAlgs/NNClusteringCalibration_RunI/TTrainedNetwork.h:69
TTrainedNetwork::TTrainedNetwork
TTrainedNetwork()
TTrainedNetwork::getInputs
std::vector< Input > getInputs() const
Definition: Tracking/TrkUtilityPackages/TrkNeuralNetworkUtils/src/TTrainedNetwork.cxx:163
python.setupRTTAlg.size
int size
Definition: setupRTTAlg.py:39
TTrainedNetwork::DVec
std::vector< Double_t > DVec
Definition: Tracking/TrkUtilityPackages/TrkNeuralNetworkUtils/TrkNeuralNetworkUtils/TTrainedNetwork.h:43
TTrainedNetwork::m_maxExpValue
double m_maxExpValue
Definition: Tracking/TrkUtilityPackages/TrkNeuralNetworkUtils/TrkNeuralNetworkUtils/TTrainedNetwork.h:148
nnopt
Definition: Tracking/TrkUtilityPackages/TrkNeuralNetworkUtils/TrkNeuralNetworkUtils/TTrainedNetwork.h:23
TTrainedNetwork::is_consistent
bool is_consistent() const
Definition: Tracking/TrkUtilityPackages/TrkNeuralNetworkUtils/src/TTrainedNetwork.cxx:372
TTrainedNetwork::check_norm_size
bool check_norm_size(unsigned size) const
Definition: Tracking/TrkUtilityPackages/TrkNeuralNetworkUtils/src/TTrainedNetwork.cxx:412
PlotPulseshapeFromCool.input
input
Definition: PlotPulseshapeFromCool.py:106
TTrainedNetwork::m_nOutput
Int_t m_nOutput
Definition: InnerDetector/InDetCalibAlgs/PixelCalibAlgs/NNClusteringCalibration_RunI/TTrainedNetwork.h:70
TTrainedNetwork::calculateNormalized
DVec calculateNormalized(const DVec &input) const
Definition: Tracking/TrkUtilityPackages/TrkNeuralNetworkUtils/src/TTrainedNetwork.cxx:267
TTrainedNetwork::~TTrainedNetwork
~TTrainedNetwork()
TTrainedNetwork::Input::scale
double scale
Definition: Tracking/TrkUtilityPackages/TrkNeuralNetworkUtils/TrkNeuralNetworkUtils/TTrainedNetwork.h:40
python.AtlRunQueryLib.options
options
Definition: AtlRunQueryLib.py:379
TTrainedNetwork::setScales
void setScales(const std::vector< double > &scales)
Definition: Tracking/TrkUtilityPackages/TrkNeuralNetworkUtils/src/TTrainedNetwork.cxx:70
TTrainedNetwork::m_WeightMatrices
std::vector< TMatrixD * > m_WeightMatrices
Definition: InnerDetector/InDetCalibAlgs/PixelCalibAlgs/NNClusteringCalibration_RunI/TTrainedNetwork.h:76
TTrainedNetwork::getThresholdVectors
const std::vector< TVectorD * > & getThresholdVectors() const
Definition: Tracking/TrkUtilityPackages/TrkNeuralNetworkUtils/TrkNeuralNetworkUtils/TTrainedNetwork.h:99
TTrainedNetwork
Definition: InnerDetector/InDetCalibAlgs/PixelCalibAlgs/NNClusteringCalibration_RunI/TTrainedNetwork.h:21
TTrainedNetwork::m_ThresholdVectors
std::vector< TVectorD * > m_ThresholdVectors
Definition: InnerDetector/InDetCalibAlgs/PixelCalibAlgs/NNClusteringCalibration_RunI/TTrainedNetwork.h:75
TTrainedNetwork::Input::name
std::string name
Definition: Tracking/TrkUtilityPackages/TrkNeuralNetworkUtils/TrkNeuralNetworkUtils/TTrainedNetwork.h:38
TTrainedNetwork::setNewWeights
void setNewWeights(std::vector< TVectorD * > &thresholdVectors, std::vector< TMatrixD * > &weightMatrices)
TTrainedNetwork::sigmoid
Double_t sigmoid(Double_t x) const
TTrainedNetwork::DMap
std::map< std::string, double > DMap
Definition: Tracking/TrkUtilityPackages/TrkNeuralNetworkUtils/TrkNeuralNetworkUtils/TTrainedNetwork.h:44
TTrainedNetwork::SIGMOID
@ SIGMOID
Definition: Tracking/TrkUtilityPackages/TrkNeuralNetworkUtils/TrkNeuralNetworkUtils/TTrainedNetwork.h:34
TTrainedNetwork::normalizeOutput
static const unsigned normalizeOutput
Definition: Tracking/TrkUtilityPackages/TrkNeuralNetworkUtils/TrkNeuralNetworkUtils/TTrainedNetwork.h:31
TTrainedNetwork::setOffsets
void setOffsets(const std::vector< double > &offsets)
Definition: Tracking/TrkUtilityPackages/TrkNeuralNetworkUtils/src/TTrainedNetwork.cxx:65
TTrainedNetwork::m_ActivationFunction
Int_t m_ActivationFunction
cache of the maximum needed size, not persisitified
Definition: InnerDetector/InDetCalibAlgs/PixelCalibAlgs/NNClusteringCalibration_RunI/TTrainedNetwork.h:79
TTrainedNetwork::m_nHidden
unsigned m_nHidden
Definition: Tracking/TrkUtilityPackages/TrkNeuralNetworkUtils/TrkNeuralNetworkUtils/TTrainedNetwork.h:125
TTrainedNetwork::TTrainedNetwork
TTrainedNetwork(Int_t nInput, Int_t nHidden, Int_t nOutput, std::vector< Int_t > &nHiddenLayerSize, std::vector< TVectorD * > &thresholdVectors, std::vector< TMatrixD * > &weightMatrices, Int_t activationFunction, bool linearOutput=false, bool normalizeOutput=false)
TTrainedNetwork::getIfLinearOutput
bool getIfLinearOutput() const
Definition: Tracking/TrkUtilityPackages/TrkNeuralNetworkUtils/TrkNeuralNetworkUtils/TTrainedNetwork.h:117
TTrainedNetwork::getnOutput
Int_t getnOutput() const
Definition: Tracking/TrkUtilityPackages/TrkNeuralNetworkUtils/TrkNeuralNetworkUtils/TTrainedNetwork.h:91
TTrainedNetwork::m_input_node_offset
std::vector< Double_t > m_input_node_offset
Definition: Tracking/TrkUtilityPackages/TrkNeuralNetworkUtils/TrkNeuralNetworkUtils/TTrainedNetwork.h:130
TTrainedNetwork::m_nInput
unsigned m_nInput
Definition: Tracking/TrkUtilityPackages/TrkNeuralNetworkUtils/TrkNeuralNetworkUtils/TTrainedNetwork.h:119
TTrainedNetwork::linearOutput
static const unsigned linearOutput
Definition: Tracking/TrkUtilityPackages/TrkNeuralNetworkUtils/TrkNeuralNetworkUtils/TTrainedNetwork.h:30
TTrainedNetwork::m_bufferSizeMax
unsigned int m_bufferSizeMax
Definition: Tracking/TrkUtilityPackages/TrkNeuralNetworkUtils/TrkNeuralNetworkUtils/TTrainedNetwork.h:140
TTrainedNetwork::m_input_node_scale
std::vector< Double_t > m_input_node_scale
Definition: Tracking/TrkUtilityPackages/TrkNeuralNetworkUtils/TrkNeuralNetworkUtils/TTrainedNetwork.h:131