13 #ifndef MLPACK_METHODS_ANN_LAYER_SEQUENTIAL_IMPL_HPP 14 #define MLPACK_METHODS_ANN_LAYER_SEQUENTIAL_IMPL_HPP 19 #include "../visitor/forward_visitor.hpp" 20 #include "../visitor/backward_visitor.hpp" 21 #include "../visitor/gradient_visitor.hpp" 22 #include "../visitor/set_input_height_visitor.hpp" 23 #include "../visitor/set_input_width_visitor.hpp" 24 #include "../visitor/input_shape_visitor.hpp" 29 template <
typename InputDataType,
typename OutputDataType,
bool Residual,
30 typename... CustomLayers>
33 model(model), reset(false), width(0), height(0), ownsLayers(!model)
38 template <
typename InputDataType,
typename OutputDataType,
bool Residual,
39 typename... CustomLayers>
42 model(model), reset(false), width(0), height(0), ownsLayers(ownsLayers)
47 template <
typename InputDataType,
typename OutputDataType,
bool Residual,
48 typename... CustomLayers>
55 ownsLayers(layer.ownsLayers)
60 template <
typename InputDataType,
typename OutputDataType,
bool Residual,
61 typename... CustomLayers>
62 Sequential<InputDataType, OutputDataType, Residual, CustomLayers...>&
71 height = layer.height;
72 ownsLayers = layer.ownsLayers;
73 parameters = layer.parameters;
76 for (
size_t i = 0; i < layer.network.size(); ++i)
78 this->network.push_back(boost::apply_visitor(copyVisitor,
86 template <
typename InputDataType,
typename OutputDataType,
bool Residual,
87 typename... CustomLayers>
89 InputDataType, OutputDataType, Residual, CustomLayers...>
::~Sequential()
91 if (!model && ownsLayers)
93 for (LayerTypes<CustomLayers...>& layer : network)
94 boost::apply_visitor(deleteVisitor, layer);
98 template<
typename InputDataType,
typename OutputDataType,
bool Residual,
99 typename... CustomLayers>
103 size_t inputShape = 0;
105 for (
size_t l = 0; l < network.size(); ++l)
116 template<
typename InputDataType,
typename OutputDataType,
bool Residual,
117 typename... CustomLayers>
118 template<
typename eT>
120 Forward(
const arma::Mat<eT>& input, arma::Mat<eT>& output)
123 boost::apply_visitor(outputParameterVisitor, network.front())),
128 if (boost::apply_visitor(outputWidthVisitor, network.front()) != 0)
130 width = boost::apply_visitor(outputWidthVisitor, network.front());
133 if (boost::apply_visitor(outputHeightVisitor, network.front()) != 0)
135 height = boost::apply_visitor(outputHeightVisitor, network.front());
139 for (
size_t i = 1; i < network.size(); ++i)
151 outputParameterVisitor, network[i - 1]),
152 boost::apply_visitor(outputParameterVisitor, network[i])),
158 if (boost::apply_visitor(outputWidthVisitor, network[i]) != 0)
160 width = boost::apply_visitor(outputWidthVisitor, network[i]);
164 if (boost::apply_visitor(outputHeightVisitor, network[i]) != 0)
166 height = boost::apply_visitor(outputHeightVisitor, network[i]);
176 output = boost::apply_visitor(outputParameterVisitor, network.back());
180 if (arma::size(output) != arma::size(input))
182 Log::Fatal <<
"The sizes of the output and input matrices of the Residual" 183 <<
" block should be equal. Please examine the network architecture." 190 template<
typename InputDataType,
typename OutputDataType,
bool Residual,
191 typename... CustomLayers>
192 template<
typename eT>
194 InputDataType, OutputDataType, Residual, CustomLayers...>
::Backward(
195 const arma::Mat<eT>& ,
196 const arma::Mat<eT>& gy,
200 outputParameterVisitor, network.back()), gy,
201 boost::apply_visitor(deltaVisitor, network.back())),
204 for (
size_t i = 2; i < network.size() + 1; ++i)
207 outputParameterVisitor, network[network.size() - i]),
208 boost::apply_visitor(deltaVisitor, network[network.size() - i + 1]),
209 boost::apply_visitor(deltaVisitor, network[network.size() - i])),
210 network[network.size() - i]);
213 g = boost::apply_visitor(deltaVisitor, network.front());
221 template<
typename InputDataType,
typename OutputDataType,
bool Residual,
222 typename... CustomLayers>
223 template<
typename eT>
225 Gradient(
const arma::Mat<eT>& input,
226 const arma::Mat<eT>& error,
230 outputParameterVisitor, network[network.size() - 2]), error),
233 for (
size_t i = 2; i < network.size(); ++i)
236 outputParameterVisitor, network[network.size() - i - 1]),
237 boost::apply_visitor(deltaVisitor, network[network.size() - i + 1])),
238 network[network.size() - i]);
242 boost::apply_visitor(deltaVisitor, network[1])), network.front());
245 template<
typename InputDataType,
typename OutputDataType,
bool Residual,
246 typename... CustomLayers>
247 template<
typename Archive>
249 InputDataType, OutputDataType, Residual, CustomLayers...>
::serialize(
250 Archive& ar,
const uint32_t )
253 if (cereal::is_loading<Archive>())
255 for (LayerTypes<CustomLayers...>& layer : network)
257 boost::apply_visitor(deleteVisitor, layer);
261 ar(CEREAL_NVP(model));
264 ar(CEREAL_NVP(ownsLayers));
~Sequential()
Destroy the Sequential object.
Definition: sequential_impl.hpp:89
BackwardVisitor executes the Backward() function given the input, error and delta parameter...
Definition: backward_visitor.hpp:28
static MLPACK_EXPORT util::PrefixedOutStream Fatal
Prints fatal messages prefixed with [FATAL], then terminates the program.
Definition: log.hpp:90
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: cv.hpp:1
Sequential(const bool model=true)
Create the Sequential object using the specified parameters.
Definition: sequential_impl.hpp:32
ForwardVisitor executes the Forward() function given the input and output parameter.
Definition: forward_visitor.hpp:28
arma::mat const & Gradient() const
Get the gradient.
Definition: sequential.hpp:182
InShapeVisitor returns the input shape a Layer expects.
Definition: input_shape_visitor.hpp:29
Sequential & operator=(const Sequential &layer)
Copy assignment operator.
Definition: sequential_impl.hpp:64
SearchModeVisitor executes the Gradient() method of the given module using the input and delta parame...
Definition: gradient_visitor.hpp:28
void Backward(const arma::Mat< eT > &, const arma::Mat< eT > &gy, arma::Mat< eT > &g)
Ordinary feed backward pass of a neural network, using 3rd-order tensors as input, calculating the function f(x) by propagating x backwards through f.
Definition: sequential_impl.hpp:194
void Forward(const 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...
Definition: sequential_impl.hpp:120
Implementation of the Sequential class.
Definition: layer_types.hpp:145
void serialize(Archive &ar, const uint32_t)
Serialize the layer.
Definition: sequential_impl.hpp:249
#define CEREAL_VECTOR_VARIANT_POINTER(T)
Cereal does not support the serialization of raw pointer.
Definition: pointer_vector_variant_wrapper.hpp:92