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>
18 
19 #include "init_rules_traits.hpp"
20 
21 namespace mlpack {
22 namespace ann {
23 
28 template<typename InitializationRuleType>
30 {
31  public:
38  const InitializationRuleType& initializeRule = InitializationRuleType()) :
39  initializeRule(initializeRule)
40  {
41  // Nothing to do here.
42  }
43 
52  template <typename eT>
53  void Initialize(const std::vector<Layer<arma::Mat<eT>>*>& network,
54  arma::Mat<eT>& parameters,
55  size_t parameterOffset = 0)
56  {
57  // Determine the total number of parameters/weights of the given network.
58  if (parameters.is_empty())
59  {
60  size_t weights = 0;
61  for (size_t i = 0; i < network.size(); ++i)
62  weights += network[i]->WeightSize();
63 
64  parameters.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 = network[i]->WeightSize();
75  arma::Mat<eT> tmp = arma::mat(parameters.memptr() + offset,
76  weight, 1, false, false);
77  initializeRule.Initialize(tmp, tmp.n_elem, 1);
78 
79  // Increase the parameter/weight offset for the next layer.
80  offset += weight;
81  }
82  }
83  else
84  {
85  initializeRule.Initialize(parameters, parameters.n_elem, 1);
86  }
87  }
88 
89  private:
92  InitializationRuleType initializeRule;
93 }; // class NetworkInitialization
94 
95 } // namespace ann
96 } // namespace mlpack
97 
98 #endif
Linear algebra utility functions, generally performed on matrices or vectors.
The core includes that mlpack expects; standard C++ includes and Armadillo.
This is a template class that can provide information about various initialization methods...
NetworkInitialization(const InitializationRuleType &initializeRule=InitializationRuleType())
Use the given initialization rule to initialize the specified network.
A layer is an abstract class implementing common neural networks operations, such as convolution...
Definition: layer.hpp:52
This class is used to initialize the network with the given initialization rule.
void Initialize(const std::vector< Layer< arma::Mat< eT >> *> &network, arma::Mat< eT > &parameters, size_t parameterOffset=0)
Initialize the specified network and store the results in the given parameter.