ATLAS Offline Software
Public Member Functions | Private Member Functions | Private Attributes | List of all members
lwtDev::Stack Class Reference

#include <Stack.h>

Collaboration diagram for lwtDev::Stack:

Public Member Functions

 Stack ()
 
 Stack (size_t n_inputs, const std::vector< LayerConfig > &layers, size_t skip_layers=0)
 
 ~Stack ()
 
 Stack (Stack &)=delete
 
Stackoperator= (Stack &)=delete
 
VectorXd compute (VectorXd) const
 
size_t n_outputs () const
 

Private Member Functions

size_t add_layers (size_t n_inputs, const LayerConfig &)
 
size_t add_dense_layers (size_t n_inputs, const LayerConfig &)
 
size_t add_normalization_layers (size_t n_inputs, const LayerConfig &)
 
size_t add_highway_layers (size_t n_inputs, const LayerConfig &)
 
size_t add_maxout_layers (size_t n_inputs, const LayerConfig &)
 

Private Attributes

std::vector< ILayer * > m_layers
 
size_t m_n_outputs
 

Detailed Description

Definition at line 47 of file Stack.h.

Constructor & Destructor Documentation

◆ Stack() [1/3]

lwtDev::Stack::Stack ( )

Definition at line 20 of file Stack.cxx.

20  {
21  m_layers.push_back(new DummyLayer);
22  m_layers.push_back(new UnaryActivationLayer({ Activation::SIGMOID, 0.0 }));
23 
24  m_layers.push_back(new BiasLayer(std::vector<double>{1, 1, 1, 1}));
25  MatrixXd mat(4, 4);
26  mat <<
27  0, 0, 0, 1,
28  0, 0, 1, 0,
29  0, 1, 0, 0,
30  1, 0, 0, 0;
31  m_layers.push_back(new MatrixLayer(mat));
32  m_n_outputs = 4;
33  }

◆ Stack() [2/3]

lwtDev::Stack::Stack ( size_t  n_inputs,
const std::vector< LayerConfig > &  layers,
size_t  skip_layers = 0 
)

Definition at line 36 of file Stack.cxx.

37  {
38  for (size_t nnn = skip; nnn < layers.size(); nnn++) {
39  n_inputs = add_layers(n_inputs, layers.at(nnn));
40  }
41  // the final assigned n_inputs is the number of output nodes
42  m_n_outputs = n_inputs;
43  }

◆ ~Stack()

lwtDev::Stack::~Stack ( )

Definition at line 45 of file Stack.cxx.

45  {
46  for (auto& layer: m_layers) {
47  delete layer;
48  layer = 0;
49  }
50  }

◆ Stack() [3/3]

lwtDev::Stack::Stack ( Stack )
delete

Member Function Documentation

◆ add_dense_layers()

size_t lwtDev::Stack::add_dense_layers ( size_t  n_inputs,
const LayerConfig layer 
)
private

Definition at line 79 of file Stack.cxx.

79  {
80  assert(layer.architecture == Architecture::DENSE);
82 
83  size_t n_outputs = n_inputs;
84 
85  // add matrix layer
86  if (layer.weights.size() > 0) {
87  MatrixXd matrix = build_matrix(layer.weights, n_inputs);
88  n_outputs = matrix.rows();
89  m_layers.push_back(new MatrixLayer(matrix));
90  };
91 
92  // add bias layer
93  if (layer.bias.size() > 0) {
94  if (n_outputs != layer.bias.size() ) {
95  std::string problem = "tried to add a bias layer with " +
96  std::to_string(layer.bias.size()) + " entries, previous layer"
97  " had " + std::to_string(n_outputs) + " outputs";
98  throw NNConfigurationException(problem);
99  }
100  m_layers.push_back(new BiasLayer(layer.bias));
101  }
102 
103  // add activation layer
104  if (layer.activation.function != Activation::LINEAR) {
105  m_layers.push_back(get_raw_activation_layer(layer.activation));
106  }
107 
108  return n_outputs;
109  }

◆ add_highway_layers()

size_t lwtDev::Stack::add_highway_layers ( size_t  n_inputs,
const LayerConfig layer 
)
private

Definition at line 133 of file Stack.cxx.

133  {
134  auto& comps = layer.components;
135  const auto& t = get_component(comps.at(Component::T), n_inputs);
136  const auto& c = get_component(comps.at(Component::CARRY), n_inputs);
137 
138  m_layers.push_back(
139  new HighwayLayer(t.W, t.b, c.W, c.b, layer.activation));
140  return n_inputs;
141  }

◆ add_layers()

size_t lwtDev::Stack::add_layers ( size_t  n_inputs,
const LayerConfig layer 
)
private

Definition at line 66 of file Stack.cxx.

66  {
67  if (layer.architecture == Architecture::DENSE) {
68  return add_dense_layers(n_inputs, layer);
69  } else if (layer.architecture == Architecture::NORMALIZATION){
70  return add_normalization_layers(n_inputs, layer);
71  } else if (layer.architecture == Architecture::HIGHWAY){
72  return add_highway_layers(n_inputs, layer);
73  } else if (layer.architecture == Architecture::MAXOUT) {
74  return add_maxout_layers(n_inputs, layer);
75  }
76  throw NNConfigurationException("unknown architecture");
77  }

◆ add_maxout_layers()

size_t lwtDev::Stack::add_maxout_layers ( size_t  n_inputs,
const LayerConfig layer 
)
private

Definition at line 144 of file Stack.cxx.

144  {
145  assert(layer.architecture == Architecture::MAXOUT);
147  std::vector<MaxoutLayer::InitUnit> matrices;
148  std::set<size_t> n_outputs;
149  for (const auto& sublayer: layer.sublayers) {
150  MatrixXd matrix = build_matrix(sublayer.weights, n_inputs);
151  VectorXd bias = build_vector(sublayer.bias);
152  n_outputs.insert(matrix.rows());
153  matrices.push_back(std::make_pair(matrix, bias));
154  }
155  if (n_outputs.size() == 0) {
156  throw NNConfigurationException("tried to build maxout withoutweights!");
157  }
158  else if (n_outputs.size() != 1) {
159  throw NNConfigurationException("uneven matrices for maxout");
160  }
161  m_layers.push_back(new MaxoutLayer(matrices));
162  return *n_outputs.begin();
163  }

◆ add_normalization_layers()

size_t lwtDev::Stack::add_normalization_layers ( size_t  n_inputs,
const LayerConfig layer 
)
private

Definition at line 111 of file Stack.cxx.

111  {
112  assert(layer.architecture == Architecture::NORMALIZATION);
114 
115  // Do some checks
116  if ( layer.weights.size() < 1 || layer.bias.size() < 1 ) {
117  std::string problem = "Either weights or bias layer size is < 1";
118  throw NNConfigurationException(problem);
119  };
120  if ( layer.weights.size() != layer.bias.size() ) {
121  std::string problem = "weights and bias layer are not equal in size!";
122  throw NNConfigurationException(problem);
123  };
124  VectorXd v_weights = build_vector(layer.weights);
125  VectorXd v_bias = build_vector(layer.bias);
126 
127  m_layers.push_back(
128  new NormalizationLayer(v_weights, v_bias));
129  return n_inputs;
130  }

◆ compute()

VectorXd lwtDev::Stack::compute ( VectorXd  in) const

Definition at line 51 of file Stack.cxx.

51  {
52  for (const auto& layer: m_layers) {
53  in = layer->compute(in);
54  }
55  return in;
56  }

◆ n_outputs()

size_t lwtDev::Stack::n_outputs ( ) const

Definition at line 57 of file Stack.cxx.

57  {
58  return m_n_outputs;
59  }

◆ operator=()

Stack& lwtDev::Stack::operator= ( Stack )
delete

Member Data Documentation

◆ m_layers

std::vector<ILayer*> lwtDev::Stack::m_layers
private

Definition at line 71 of file Stack.h.

◆ m_n_outputs

size_t lwtDev::Stack::m_n_outputs
private

Definition at line 72 of file Stack.h.


The documentation for this class was generated from the following files:
lwtDev::Architecture::DENSE
@ DENSE
skel.nnn
nnn
Definition: skel.GENtoEVGEN.py:692
lwtDev::Activation::LINEAR
@ LINEAR
lwtDev::build_matrix
MatrixXd build_matrix(const std::vector< double > &weights, size_t n_inputs)
Definition: Stack.cxx:741
lwtDev::MaxoutLayer
Definition: Stack.h:126
mat
GeoMaterial * mat
Definition: LArDetectorConstructionTBEC.cxx:55
lwtDev::Stack::add_highway_layers
size_t add_highway_layers(size_t n_inputs, const LayerConfig &)
Definition: Stack.cxx:133
lwtDev::Stack::add_dense_layers
size_t add_dense_layers(size_t n_inputs, const LayerConfig &)
Definition: Stack.cxx:79
module_driven_slicing.layers
layers
Definition: module_driven_slicing.py:114
lwtDev::Stack::n_outputs
size_t n_outputs() const
Definition: Stack.cxx:57
lwtDev::Stack::m_n_outputs
size_t m_n_outputs
Definition: Stack.h:72
lwtDev::BiasLayer
Definition: Stack.h:107
read_hist_ntuple.t
t
Definition: read_hist_ntuple.py:5
lwtDev::NNConfigurationException
Definition: Reconstruction/tauRecTools/tauRecTools/lwtnn/Exceptions.h:21
lwtDev::DummyLayer
Definition: Stack.h:86
lwtDev::MatrixLayer
Definition: Stack.h:117
TRT::Hit::layer
@ layer
Definition: HitInfo.h:79
lwtDev::Stack::m_layers
std::vector< ILayer * > m_layers
Definition: Stack.h:71
lwtDev::Stack::add_layers
size_t add_layers(size_t n_inputs, const LayerConfig &)
Definition: Stack.cxx:66
lwtDev::build_vector
VectorXd build_vector(const std::vector< double > &bias)
Definition: Stack.cxx:760
lwtDev::Architecture::MAXOUT
@ MAXOUT
lwtDev::Architecture::HIGHWAY
@ HIGHWAY
lwtDev::throw_if_not_dense
void throw_if_not_dense(const LayerConfig &layer)
Definition: Stack.cxx:779
lwtDev::Component::T
@ T
ActsTrk::to_string
std::string to_string(const DetectorType &type)
Definition: GeometryDefs.h:34
lwtDev::throw_if_not_normalization
void throw_if_not_normalization(const LayerConfig &layer)
Definition: Stack.cxx:785
python.testIfMatch.matrix
matrix
Definition: testIfMatch.py:66
lwtDev::get_component
DenseComponents get_component(const lwtDev::LayerConfig &layer, size_t n_in)
Definition: Stack.cxx:792
lwtDev::NormalizationLayer
Normalization layer /// https://arxiv.org/abs/1502.03167 ///.
Definition: Stack.h:140
lwtDev::Stack::add_maxout_layers
size_t add_maxout_layers(size_t n_inputs, const LayerConfig &)
Definition: Stack.cxx:144
lwtDev::Activation::SIGMOID
@ SIGMOID
lwtDev::UnaryActivationLayer
Definition: Stack.h:92
lwtDev::throw_if_not_maxout
void throw_if_not_maxout(const LayerConfig &layer)
Definition: Stack.cxx:771
skip
bool skip
Definition: TrigGlobEffCorrValidation.cxx:190
lwtDev::Component::CARRY
@ CARRY
lwtDev::Stack::add_normalization_layers
size_t add_normalization_layers(size_t n_inputs, const LayerConfig &)
Definition: Stack.cxx:111
lwtDev::Architecture::NORMALIZATION
@ NORMALIZATION
python.compressB64.c
def c
Definition: compressB64.py:93
lwtDev::get_raw_activation_layer
ILayer * get_raw_activation_layer(ActivationConfig activation)
Definition: Stack.cxx:661
lwtDev::HighwayLayer
Definition: Stack.h:154