network_init.hpp
Go to the documentation of this file.
1 
13 #ifndef MLPACK_METHODS_ANN_INIT_RULES_NETWORK_INIT_HPP
14 #define MLPACK_METHODS_ANN_INIT_RULES_NETWORK_INIT_HPP
15 
16 #include <mlpack/prereqs.hpp>
17 
18 #include "../visitor/reset_visitor.hpp"
19 #include "../visitor/weight_size_visitor.hpp"
20 #include "../visitor/weight_set_visitor.hpp"
21 #include "init_rules_traits.hpp"
22 
24 
25 namespace mlpack {
26 namespace ann {
27 
32 template<typename InitializationRuleType, typename... CustomLayers>
34 {
35  public:
42  const InitializationRuleType& initializeRule = InitializationRuleType()) :
43  initializeRule(initializeRule)
44  {
45  // Nothing to do here.
46  }
47 
56  void Initialize(const std::vector<LayerTypes<CustomLayers...> >& network,
57  arma::mat& parameter, size_t parameterOffset = 0)
58  {
59  // Determine the number of parameter/weights of the given network.
60  if (parameter.is_empty())
61  {
62  size_t weights = 0;
63  for (size_t i = 0; i < network.size(); ++i)
64  weights += boost::apply_visitor(weightSizeVisitor, network[i]);
65  parameter.set_size(weights, 1);
66  }
67 
68  // Initialize the network layer by layer or the complete network.
70  {
71  for (size_t i = 0, offset = parameterOffset; i < network.size(); ++i)
72  {
73  // Initialize the layer with the specified parameter/weight
74  // initialization rule.
75  const size_t weight = boost::apply_visitor(weightSizeVisitor,
76  network[i]);
77  arma::mat tmp = arma::mat(parameter.memptr() + offset,
78  weight, 1, false, false);
79  initializeRule.Initialize(tmp, tmp.n_elem, 1);
80 
81  // Increase the parameter/weight offset for the next layer.
82  offset += weight;
83  }
84  }
85  else
86  {
87  initializeRule.Initialize(parameter, parameter.n_elem, 1);
88  }
89 
90  // Note: We can't merge the for loop into the for loop above because
91  // WeightSetVisitor also sets the parameter/weights of the inner modules.
92  // Inner Modules are held by the parent module e.g. the concat module can
93  // hold various other modules.
94  for (size_t i = 0, offset = parameterOffset; i < network.size(); ++i)
95  {
96  offset += boost::apply_visitor(WeightSetVisitor(parameter, offset),
97  network[i]);
98 
99  boost::apply_visitor(resetVisitor, network[i]);
100  }
101  }
102 
103  private:
106  InitializationRuleType initializeRule;
107 
109  ResetVisitor resetVisitor;
110 
112  WeightSizeVisitor weightSizeVisitor;
113 }; // class NetworkInitialization
114 
115 } // namespace ann
116 } // namespace mlpack
117 
118 #endif
boost::variant< AdaptiveMaxPooling< arma::mat, arma::mat > *, AdaptiveMeanPooling< arma::mat, arma::mat > *, Add< arma::mat, arma::mat > *, AddMerge< arma::mat, arma::mat > *, AlphaDropout< arma::mat, arma::mat > *, AtrousConvolution< NaiveConvolution< ValidConvolution >, NaiveConvolution< FullConvolution >, NaiveConvolution< ValidConvolution >, arma::mat, arma::mat > *, BaseLayer< LogisticFunction, arma::mat, arma::mat > *, BaseLayer< IdentityFunction, arma::mat, arma::mat > *, BaseLayer< TanhFunction, arma::mat, arma::mat > *, BaseLayer< SoftplusFunction, arma::mat, arma::mat > *, BaseLayer< RectifierFunction, arma::mat, arma::mat > *, BatchNorm< arma::mat, arma::mat > *, BilinearInterpolation< arma::mat, arma::mat > *, CELU< arma::mat, arma::mat > *, Concat< arma::mat, arma::mat > *, Concatenate< arma::mat, arma::mat > *, ConcatPerformance< NegativeLogLikelihood< arma::mat, arma::mat >, arma::mat, arma::mat > *, Constant< arma::mat, arma::mat > *, Convolution< NaiveConvolution< ValidConvolution >, NaiveConvolution< FullConvolution >, NaiveConvolution< ValidConvolution >, arma::mat, arma::mat > *, CReLU< arma::mat, arma::mat > *, DropConnect< arma::mat, arma::mat > *, Dropout< arma::mat, arma::mat > *, ELU< arma::mat, arma::mat > *, FastLSTM< arma::mat, arma::mat > *, FlexibleReLU< arma::mat, arma::mat > *, GRU< arma::mat, arma::mat > *, HardTanH< arma::mat, arma::mat > *, Join< arma::mat, arma::mat > *, LayerNorm< arma::mat, arma::mat > *, LeakyReLU< arma::mat, arma::mat > *, Linear< arma::mat, arma::mat, NoRegularizer > *, LinearNoBias< arma::mat, arma::mat, NoRegularizer > *, LogSoftMax< arma::mat, arma::mat > *, Lookup< arma::mat, arma::mat > *, LSTM< arma::mat, arma::mat > *, MaxPooling< arma::mat, arma::mat > *, MeanPooling< arma::mat, arma::mat > *, MiniBatchDiscrimination< arma::mat, arma::mat > *, MultiplyConstant< arma::mat, arma::mat > *, MultiplyMerge< arma::mat, arma::mat > *, NegativeLogLikelihood< arma::mat, arma::mat > *, NoisyLinear< arma::mat, arma::mat > *, Padding< arma::mat, arma::mat > *, PReLU< arma::mat, arma::mat > *, Softmax< arma::mat, arma::mat > *, TransposedConvolution< NaiveConvolution< ValidConvolution >, NaiveConvolution< ValidConvolution >, NaiveConvolution< ValidConvolution >, arma::mat, arma::mat > *, WeightNorm< arma::mat, arma::mat > *, MoreTypes, CustomLayers *... > LayerTypes
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: add_to_po.hpp:21
The core includes that mlpack expects; standard C++ includes and Armadillo.
WeightSizeVisitor returns the number of weights of the given module.
This is a template class that can provide information about various initialization methods...
WeightSetVisitor update the module parameters given the parameters set.
ResetVisitor executes the Reset() function.
NetworkInitialization(const InitializationRuleType &initializeRule=InitializationRuleType())
Use the given initialization rule to initialize the specified network.
This class is used to initialize the network with the given initialization rule.
void Initialize(const std::vector< LayerTypes< CustomLayers... > > &network, arma::mat &parameter, size_t parameterOffset=0)
Initialize the specified network and store the results in the given parameter.