highway.hpp
Go to the documentation of this file.
1 
13 #ifndef MLPACK_METHODS_ANN_LAYER_HIGHWAY_HPP
14 #define MLPACK_METHODS_ANN_LAYER_HIGHWAY_HPP
15 
16 #include <mlpack/prereqs.hpp>
17 
18 #include <boost/ptr_container/ptr_vector.hpp>
19 
20 #include "../visitor/delete_visitor.hpp"
21 #include "../visitor/delta_visitor.hpp"
22 #include "../visitor/output_height_visitor.hpp"
23 #include "../visitor/output_parameter_visitor.hpp"
24 #include "../visitor/output_width_visitor.hpp"
25 
26 #include "layer_types.hpp"
27 #include "add_merge.hpp"
28 
29 namespace mlpack {
30 namespace ann {
31 
56 template <
57  typename InputDataType = arma::mat,
58  typename OutputDataType = arma::mat,
59  typename... CustomLayers>
60 class Highway
61 {
62  public:
64  Highway();
65 
72  Highway(const size_t inSize, const bool model = true);
73 
75  ~Highway();
76 
80  void DeleteModules();
81 
85  void Reset();
86 
94  template<typename eT>
95  void Forward(arma::Mat<eT>&& input, arma::Mat<eT>&& output);
96 
106  template<typename eT>
107  void Backward(const arma::Mat<eT>&& /* input */,
108  arma::Mat<eT>&& gy,
109  arma::Mat<eT>&& g);
110 
118  template<typename eT>
119  void Gradient(arma::Mat<eT>&& input,
120  arma::Mat<eT>&& error,
121  arma::Mat<eT>&& gradient);
122 
128  template <class LayerType, class... Args>
129  void Add(Args... args) { network.push_back(new LayerType(args...)); }
130 
136  void Add(LayerTypes<CustomLayers...> layer) { network.push_back(layer); }
137 
139  std::vector<LayerTypes<CustomLayers...> >& Model()
140  {
141  if (model)
142  {
143  return network;
144  }
145 
146  return empty;
147  }
148 
150  OutputDataType const& Parameters() const { return weights; }
152  OutputDataType& Parameters() { return weights; }
153 
155  InputDataType const& InputParameter() const { return inputParameter; }
157  InputDataType& InputParameter() { return inputParameter; }
158 
160  OutputDataType const& OutputParameter() const { return outputParameter; }
162  OutputDataType& OutputParameter() { return outputParameter; }
163 
165  OutputDataType const& Delta() const { return delta; }
167  OutputDataType& Delta() { return delta; }
168 
170  OutputDataType const& Gradient() const { return gradient; }
172  OutputDataType& Gradient() { return gradient; }
173 
177  template<typename Archive>
178  void serialize(Archive& ar, const unsigned int /* version */);
179 
180  private:
182  size_t inSize;
183 
185  bool model;
186 
188  bool reset;
189 
191  std::vector<LayerTypes<CustomLayers...> > network;
192 
194  std::vector<LayerTypes<CustomLayers...> > empty;
195 
197  OutputDataType weights;
198 
200  OutputDataType delta;
201 
203  OutputDataType gradient;
204 
206  OutputDataType transformWeight;
207 
209  OutputDataType transformBias;
210 
212  OutputDataType transformGate;
213 
215  OutputDataType transformGateActivation;
216 
218  OutputDataType transformGateError;
219 
221  InputDataType inputParameter;
222 
224  OutputDataType outputParameter;
225 
227  size_t width;
228 
230  size_t height;
231 
233  OutputDataType networkOutput;
234 
236  DeltaVisitor deltaVisitor;
237 
239  OutputParameterVisitor outputParameterVisitor;
240 
242  DeleteVisitor deleteVisitor;
243 
245  OutputWidthVisitor outputWidthVisitor;
246 
248  OutputHeightVisitor outputHeightVisitor;
249 }; // class Highway
250 
251 } // namespace ann
252 } // namespace mlpack
253 
254 // Include implementation.
255 #include "highway_impl.hpp"
256 
257 #endif
DeleteVisitor executes the destructor of the instantiated object.
OutputHeightVisitor exposes the OutputHeight() method of the given module.
.hpp
Definition: add_to_po.hpp:21
InputDataType & InputParameter()
Modify the input parameter.
Definition: highway.hpp:157
~Highway()
Destroy the Highway object.
void serialize(Archive &ar, const unsigned int)
Serialize the layer.
void Add(LayerTypes< CustomLayers... > layer)
Add a new module to the model.
Definition: highway.hpp:136
void DeleteModules()
Destroy all the modules added to the Highway object.
The core includes that mlpack expects; standard C++ includes and Armadillo.
OutputDataType const & Delta() const
Get the delta.
Definition: highway.hpp:165
OutputDataType & OutputParameter()
Modify the output parameter.
Definition: highway.hpp:162
OutputDataType & Parameters()
Modify the parameters.
Definition: highway.hpp:152
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< FullConvolution >, 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 > *, MoreTypes, CustomLayers *... > LayerTypes
OutputDataType & Delta()
Modify the delta.
Definition: highway.hpp:167
OutputDataType const & OutputParameter() const
Get the output parameter.
Definition: highway.hpp:160
OutputDataType const & Parameters() const
Get the parameters.
Definition: highway.hpp:150
Implementation of the Highway layer.
Definition: highway.hpp:60
OutputParameterVisitor exposes the output parameter of the given module.
void Add(Args... args)
Add a new module to the model.
Definition: highway.hpp:129
void Forward(arma::Mat< eT > &&input, arma::Mat< eT > &&output)
Ordinary feed-forward pass of a neural network, evaluating the function f(x) by propagating the activ...
OutputDataType const & Gradient() const
Get the gradient.
Definition: highway.hpp:170
OutputDataType & Gradient()
Modify the gradient.
Definition: highway.hpp:172
void Backward(const arma::Mat< eT > &&, arma::Mat< eT > &&gy, arma::Mat< eT > &&g)
Ordinary feed-backward pass of a neural network, calculating the function f(x) by propagating x backw...
DeltaVisitor exposes the delta parameter of the given module.
void Reset()
Reset the layer parameter.
OutputWidthVisitor exposes the OutputWidth() method of the given module.
std::vector< LayerTypes< CustomLayers... > > & Model()
Return the modules of the model.
Definition: highway.hpp:139
InputDataType const & InputParameter() const
Get the input parameter.
Definition: highway.hpp:155
Highway()
Create the Highway object.