mlpack
concat_performance_impl.hpp
Go to the documentation of this file.
1 
12 #ifndef MLPACK_METHODS_ANN_LAYER_CONCAT_PERFORMANCE_IMPL_HPP
13 #define MLPACK_METHODS_ANN_LAYER_CONCAT_PERFORMANCE_IMPL_HPP
14 
15 // In case it hasn't yet been included.
16 #include "concat_performance.hpp"
17 
18 namespace mlpack {
19 namespace ann {
20 
21 template<
22  typename OutputLayerType,
23  typename InputDataType,
24  typename OutputDataType
25 >
26 ConcatPerformance<
27  OutputLayerType,
28  InputDataType,
29  OutputDataType
30 >::ConcatPerformance(const size_t inSize, OutputLayerType&& outputLayer) :
31  inSize(inSize),
32  outputLayer(std::move(outputLayer))
33 {
34  // Nothing to do here.
35 }
36 
37 template<
38  typename OutputLayerType,
39  typename InputDataType,
40  typename OutputDataType
41 >
42 template<typename eT>
43 double ConcatPerformance<
44  OutputLayerType,
45  InputDataType,
46  OutputDataType
47 >::Forward(const arma::Mat<eT>& input, arma::Mat<eT>& target)
48 {
49  const size_t elements = input.n_elem / inSize;
50 
51  double output = 0;
52  for (size_t i = 0; i < input.n_elem; i+= elements)
53  {
54  arma::mat subInput = input.submat(i, 0, i + elements - 1, 0);
55  output += outputLayer.Forward(subInput, target);
56  }
57 
58  return output;
59 }
60 
61 template<
62  typename OutputLayerType,
63  typename InputDataType,
64  typename OutputDataType
65 >
66 template<typename eT>
68  OutputLayerType,
69  InputDataType,
70  OutputDataType
72  const arma::Mat<eT>& input,
73  const arma::Mat<eT>& target,
74  arma::Mat<eT>& output)
75 {
76  const size_t elements = input.n_elem / inSize;
77 
78  arma::mat subInput = input.submat(0, 0, elements - 1, 0);
79  arma::mat subOutput;
80 
81  outputLayer.Backward(subInput, target, subOutput);
82 
83  output = arma::zeros(subOutput.n_elem, inSize);
84  output.col(0) = subOutput;
85 
86  for (size_t i = elements, j = 0; i < input.n_elem; i+= elements, ++j)
87  {
88  subInput = input.submat(i, 0, i + elements - 1, 0);
89  outputLayer.Backward(subInput, target, subOutput);
90 
91  output.col(j) = subOutput;
92  }
93 }
94 
95 template<
96  typename OutputLayerType,
97  typename InputDataType,
98  typename OutputDataType
99 >
100 template<typename Archive>
101 void ConcatPerformance<
102  OutputLayerType,
103  InputDataType,
104  OutputDataType
105 >::serialize(Archive& ar, const uint32_t /* version */)
106 {
107  ar(CEREAL_NVP(inSize));
108 }
109 
110 } // namespace ann
111 } // namespace mlpack
112 
113 // Include implementation.
115 
116 #endif
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: cv.hpp:1
Definition: pointer_wrapper.hpp:23
Implementation of the concat performance class.
Definition: concat_performance.hpp:37
void Backward(const arma::Mat< eT > &input, const arma::Mat< eT > &target, arma::Mat< eT > &output)
Ordinary feed backward pass of a neural network.
Definition: concat_performance_impl.hpp:71
void serialize(Archive &, const uint32_t)
Serialize the layer.
Definition: concat_performance_impl.hpp:105