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)
57  {
58  // Determine the number of parameter/weights of the given network.
59  size_t weights = 0;
60  for (size_t i = 0; i < network.size(); ++i)
61  weights += boost::apply_visitor(weightSizeVisitor, network[i]);
62  parameter.set_size(weights, 1);
63 
64  // Initialize the network layer by layer or the complete network.
66  {
67  for (size_t i = 0, offset = 0; i < network.size(); ++i)
68  {
69  // Initialize the layer with the specified parameter/weight
70  // initialization rule.
71  const size_t weight = boost::apply_visitor(weightSizeVisitor,
72  network[i]);
73  arma::mat tmp = arma::mat(parameter.memptr() + offset,
74  weight, 1, false, false);
75  initializeRule.Initialize(tmp, tmp.n_elem, 1);
76 
77  // Increase the parameter/weight offset for the next layer.
78  offset += weight;
79  }
80  }
81  else
82  {
83  initializeRule.Initialize(parameter, parameter.n_elem, 1);
84  }
85 
86  // Note: We can't merge the for loop into the for loop above because
87  // WeightSetVisitor also sets the parameter/weights of the inner modules.
88  // Inner Modules are held by the parent module e.g. the concat module can
89  // hold various other modules.
90  for (size_t i = 0, offset = 0; i < network.size(); ++i)
91  {
92  offset += boost::apply_visitor(WeightSetVisitor(std::move(parameter),
93  offset), network[i]);
94 
95  boost::apply_visitor(resetVisitor, network[i]);
96  }
97  }
98 
99  private:
102  InitializationRuleType initializeRule;
103 
105  ResetVisitor resetVisitor;
106 
108  WeightSizeVisitor weightSizeVisitor;
109 }; // class NetworkInitialization
110 
111 } // namespace ann
112 } // namespace mlpack
113 
114 #endif
.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.
void Initialize(const std::vector< LayerTypes< CustomLayers... > > &network, arma::mat &parameter)
Initialize the specified network and store the results in the given parameter.
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.
boost::variant< Add< arma::mat, arma::mat > *, AddMerge< 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 > *, BatchNorm< arma::mat, arma::mat > *, BilinearInterpolation< arma::mat, arma::mat > *, Concat< 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 > *, CrossEntropyError< arma::mat, arma::mat > *, DropConnect< arma::mat, arma::mat > *, Dropout< arma::mat, arma::mat > *, ELU< arma::mat, arma::mat > *, Glimpse< arma::mat, arma::mat > *, HardTanH< arma::mat, arma::mat > *, Join< arma::mat, arma::mat > *, LeakyReLU< arma::mat, arma::mat > *, Linear< arma::mat, arma::mat > *, LinearNoBias< arma::mat, arma::mat > *, 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 > *, MeanSquaredError< arma::mat, arma::mat > *, MultiplyConstant< arma::mat, arma::mat > *, NegativeLogLikelihood< arma::mat, arma::mat > *, PReLU< arma::mat, arma::mat > *, Recurrent< arma::mat, arma::mat > *, RecurrentAttention< arma::mat, arma::mat > *, ReinforceNormal< arma::mat, arma::mat > *, SigmoidCrossEntropyError< arma::mat, arma::mat > *, Select< arma::mat, arma::mat > *, Sequential< arma::mat, arma::mat > *, VRClassReward< arma::mat, arma::mat > *, CustomLayers *... > LayerTypes