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 
55  void Initialize(const std::vector<LayerTypes<CustomLayers...> >& network,
56  arma::mat& parameter, size_t parameterOffset = 0)
57  {
58  // Determine the number of parameter/weights of the given network.
59  if (parameter.is_empty())
60  {
61  size_t weights = 0;
62  for (size_t i = 0; i < network.size(); ++i)
63  weights += boost::apply_visitor(weightSizeVisitor, network[i]);
64  parameter.set_size(weights, 1);
65  }
66 
67  // Initialize the network layer by layer or the complete network.
69  {
70  for (size_t i = 0, offset = parameterOffset; i < network.size(); ++i)
71  {
72  // Initialize the layer with the specified parameter/weight
73  // initialization rule.
74  const size_t weight = boost::apply_visitor(weightSizeVisitor,
75  network[i]);
76  arma::mat tmp = arma::mat(parameter.memptr() + offset,
77  weight, 1, false, false);
78  initializeRule.Initialize(tmp, tmp.n_elem, 1);
79 
80  // Increase the parameter/weight offset for the next layer.
81  offset += weight;
82  }
83  }
84  else
85  {
86  initializeRule.Initialize(parameter, parameter.n_elem, 1);
87  }
88 
89  // Note: We can't merge the for loop into the for loop above because
90  // WeightSetVisitor also sets the parameter/weights of the inner modules.
91  // Inner Modules are held by the parent module e.g. the concat module can
92  // hold various other modules.
93  for (size_t i = 0, offset = parameterOffset; i < network.size(); ++i)
94  {
95  offset += boost::apply_visitor(WeightSetVisitor(std::move(parameter),
96  offset), network[i]);
97 
98  boost::apply_visitor(resetVisitor, network[i]);
99  }
100  }
101 
102  private:
105  InitializationRuleType initializeRule;
106 
108  ResetVisitor resetVisitor;
109 
111  WeightSizeVisitor weightSizeVisitor;
112 }; // class NetworkInitialization
113 
114 } // namespace ann
115 } // namespace mlpack
116 
117 #endif
strip_type.hpp
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.
boost::variant< Add< arma::mat, arma::mat > *, AddMerge< 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< RectifierFunction, arma::mat, arma::mat > *, BaseLayer< SoftplusFunction, arma::mat, arma::mat > *, BatchNorm< arma::mat, arma::mat > *, BilinearInterpolation< 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 > *, TransposedConvolution< NaiveConvolution< ValidConvolution >, NaiveConvolution< ValidConvolution >, NaiveConvolution< ValidConvolution >, arma::mat, arma::mat > *, DropConnect< arma::mat, arma::mat > *, Dropout< arma::mat, arma::mat > *, AlphaDropout< arma::mat, arma::mat > *, ELU< arma::mat, arma::mat > *, FlexibleReLU< arma::mat, arma::mat > *, Glimpse< arma::mat, arma::mat > *, HardTanH< arma::mat, arma::mat > *, Highway< arma::mat, arma::mat > *, Join< arma::mat, arma::mat > *, LayerNorm< arma::mat, arma::mat > *, LeakyReLU< arma::mat, arma::mat > *, CReLU< 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 > *, GRU< arma::mat, arma::mat > *, FastLSTM< 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 > *, Padding< arma::mat, arma::mat > *, PReLU< arma::mat, arma::mat > *, WeightNorm< arma::mat, arma::mat > *, MoreTypes, CustomLayers *... > LayerTypes
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.