13 #ifndef MLPACK_METHODS_ANN_LAYER_TRANSPOSED_CONVOLUTION_HPP 14 #define MLPACK_METHODS_ANN_LAYER_TRANSPOSED_CONVOLUTION_HPP 43 typename ForwardConvolutionRule = NaiveConvolution<ValidConvolution>,
44 typename BackwardConvolutionRule = NaiveConvolution<ValidConvolution>,
45 typename GradientConvolutionRule = NaiveConvolution<ValidConvolution>,
46 typename InputDataType = arma::mat,
47 typename OutputDataType = arma::mat
49 class TransposedConvolution
81 const size_t kernelWidth,
82 const size_t kernelHeight,
83 const size_t strideWidth = 1,
84 const size_t strideHeight = 1,
85 const size_t padW = 0,
86 const size_t padH = 0,
87 const size_t inputWidth = 0,
88 const size_t inputHeight = 0,
89 const size_t outputWidth = 0,
90 const size_t outputHeight = 0,
91 const std::string& paddingType =
"None");
122 const size_t outSize,
123 const size_t kernelWidth,
124 const size_t kernelHeight,
125 const size_t strideWidth,
126 const size_t strideHeight,
127 const std::tuple<size_t, size_t>& padW,
128 const std::tuple<size_t, size_t>& padH,
129 const size_t inputWidth = 0,
130 const size_t inputHeight = 0,
131 const size_t outputWidth = 0,
132 const size_t outputHeight = 0,
133 const std::string& paddingType =
"None");
147 template<
typename eT>
148 void Forward(
const arma::Mat<eT>& input, arma::Mat<eT>& output);
159 template<
typename eT>
160 void Backward(
const arma::Mat<eT>& ,
161 const arma::Mat<eT>& gy,
171 template<
typename eT>
172 void Gradient(
const arma::Mat<eT>& ,
173 const arma::Mat<eT>& error,
174 arma::Mat<eT>& gradient);
182 arma::cube
const&
Weight()
const {
return weight; }
187 arma::mat
const&
Bias()
const {
return bias; }
189 arma::mat&
Bias() {
return bias; }
202 OutputDataType
const&
Delta()
const {
return delta; }
204 OutputDataType&
Delta() {
return delta; }
207 OutputDataType
const&
Gradient()
const {
return gradient; }
280 return inputHeight * inputWidth * inSize;
286 return (outSize * inSize * kernelWidth * kernelHeight) + outSize;
291 template<
typename Archive>
292 void serialize(Archive& ar,
const uint32_t );
301 template<
typename eT>
302 void Rotate180(
const arma::Cube<eT>& input, arma::Cube<eT>& output)
304 output = arma::Cube<eT>(input.n_rows, input.n_cols, input.n_slices);
307 for (
size_t s = 0; s < output.n_slices; s++)
308 output.slice(s) = arma::fliplr(arma::flipud(input.slice(s)));
314 void InitializeSamePadding();
322 template<
typename eT>
323 void Rotate180(
const arma::Mat<eT>& input, arma::Mat<eT>& output)
326 output = arma::fliplr(arma::flipud(input));
339 template<
typename eT>
340 void InsertZeros(
const arma::Mat<eT>& input,
341 const size_t strideWidth,
342 const size_t strideHeight,
343 arma::Mat<eT>& output)
345 if (output.n_rows != input.n_rows * strideWidth - strideWidth + 1 ||
346 output.n_cols != input.n_cols * strideHeight - strideHeight + 1)
348 output = arma::zeros(input.n_rows * strideWidth - strideWidth + 1,
349 input.n_cols * strideHeight - strideHeight + 1);
352 for (
size_t i = 0; i < output.n_rows; i += strideHeight)
354 for (
size_t j = 0; j < output.n_cols; j += strideWidth)
358 output(i, j) = input(i / strideHeight, j / strideWidth);
372 template<
typename eT>
373 void InsertZeros(
const arma::Cube<eT>& input,
374 const size_t strideWidth,
375 const size_t strideHeight,
376 arma::Cube<eT>& output)
378 output = arma::zeros(input.n_rows * strideWidth - strideWidth + 1,
379 input.n_cols * strideHeight - strideHeight + 1, input.n_slices);
381 for (
size_t i = 0; i < input.n_slices; ++i)
383 InsertZeros<eT>(input.slice(i), strideWidth, strideHeight,
428 OutputDataType weights;
449 arma::cube outputTemp;
452 arma::cube inputPaddedTemp;
455 arma::cube inputExpandedTemp;
461 arma::cube gradientTemp;
470 OutputDataType delta;
473 OutputDataType gradient;
476 InputDataType inputParameter;
479 OutputDataType outputParameter;
486 #include "transposed_convolution_impl.hpp" arma::mat & Bias()
Modify the bias of the layer.
size_t InputHeight() const
Get the input height.
size_t & PadWLeft()
Modify the left padding width.
OutputDataType const & Gradient() const
Get the gradient.
OutputDataType & Parameters()
Modify the parameters.
Linear algebra utility functions, generally performed on matrices or vectors.
Implementation of the Padding module class.
size_t & StrideHeight()
Modify the stride height.
size_t WeightSize() const
Get the size of the weight matrix.
OutputDataType & Delta()
Modify the delta.
The core includes that mlpack expects; standard C++ includes and Armadillo.
size_t OutputHeight() const
Get the output height.
size_t & InputHeight()
Modify the input height.
void serialize(Archive &ar, const uint32_t)
Serialize the layer.
size_t & KernelWidth()
Modify the kernel width.
size_t & PadHTop()
Modify the top padding height.
size_t & PadWRight()
Modify the right padding width.
size_t PadWLeft() const
Get the left padding width.
OutputDataType const & OutputParameter() const
Get the output parameter.
size_t PadHTop() const
Get the top padding height.
OutputDataType const & Parameters() const
Get the parameters.
arma::cube & Weight()
Modify the weight of the layer.
arma::mat const & Bias() const
Get the bias of the layer.
size_t & StrideWidth()
Modify the stride width.
TransposedConvolution()
Create the Transposed Convolution object.
size_t KernelHeight() const
Get the kernel height.
arma::cube const & Weight() const
Get the weight of the layer.
OutputDataType & Gradient()
Modify the gradient.
size_t InputShape() const
Get the shape of the input.
size_t & KernelHeight()
Modify the kernel height.
InputDataType const & InputParameter() const
Get the input parameter.
OutputDataType const & Delta() const
Get the delta.
size_t & PadHBottom()
Modify the bottom padding height.
size_t & OutputWidth()
Modify the output width.
size_t OutputSize() const
Get the output size.
size_t & InputWidth()
Modify input the width.
size_t KernelWidth() const
Get the kernel width.
size_t InputSize() const
Get the input size.
size_t PadHBottom() const
Get the bottom padding height.
size_t StrideWidth() const
Get the stride width.
OutputDataType & OutputParameter()
Modify the output parameter.
InputDataType & InputParameter()
Modify the input parameter.
size_t OutputWidth() const
Get the output width.
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...
size_t InputWidth() const
Get the input width.
size_t PadWRight() const
Get the right padding width.
size_t StrideHeight() const
Get the stride height.
size_t & OutputHeight()
Modify the output height.
void Backward(const arma::Mat< eT > &, const 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...