Fork me on GitHub

src/arraymancer/nn_primitives/nnp_convolution

  Source Edit

Types

Conv2DAlgorithm = enum
  Im2ColGEMM, NNPackAuto
  Source Edit

Procs

proc conv2d[T](input, weight, bias: Tensor[T]; padding: Size2D = (0, 0);
               stride: Size2D = (1, 1); algorithm = Conv2DAlgorithm.Im2ColGEMM): Tensor[
    T] {.inline.}

Computes a 2D convolution over input images. Intended to be used in 2d convolution forward pass. This applies a 2D cross-correlation, not to be confused with the mathematical convolution.

Input:

- ``input`` 4D Tensor batch of images of the size [N,C_in,H_in,W_in]
- ``weight`` 4D Tensor convolving kernel weights of the size [C_out,C_in,kH,kW]
- ``bias`` 3D Tensor bias of the size [C_out,1,1] or an empty tensor for no bias
- ``padding`` Size2D tuple with height and width of the padding
- ``stride`` Size2D tuple with height and width of the stride
- ``algorithm`` algorithm to be used in the convolution

Returns:

- A 4D Tensor of sized [N,C_out,H_out,W_out], where
   H_out = (H_in + (2*padding.height) - kH) / stride.height + 1
   W_out = (W_in + (2*padding.width) - kW) / stride.width + 1

Valid algorithms:

  • Im2ColGEMM im2col + GEMM algorithm, this is the default
  • NNPackAuto Use NNPack and let it auto detect the best algorithm

Future: bias will leverage the upcoming Optional type to be really optional.

  Source Edit
proc conv2d_backward[T](input, weight, bias: Tensor[T]; padding: Size2D;
                        stride: Size2D; grad_output: Tensor[T];
                        grad_input, grad_weight, grad_bias: var Tensor[T];
                        algorithm = Conv2DAlgorithm.Im2ColGEMM)

Computes gradients of a 2D convolution. Intended to be used after conv2d to calculate gradients in backward pass.

Input:

- ``input`` 4D Tensor batch of images of the size [N,C_in,H_in,W_in]
- ``weight`` 4D Tensor convolving kernel weights of the size [C_out,C_in,kH,kW]
- ``bias`` 3D Tensor bias of the size [C_out,1,1] or an empty tensor for no bias
- ``padding`` Size2D tuple with height and width of the padding
- ``stride`` Size2D tuple with height and width of the stride
- ``grad_output`` 4D tensor gradient of the next layer of the size [N,C_out,H_out,W_out]
- ``grad_input`` tensor where the gradient w.r.t input will be written
- ``grad_weight`` tensor where the gradient w.r.t weight will be written
- ``grad_bias`` tensor where the gradient w.r.t bias will be written
- ``algorithm`` algorithm to be used in the convolution

Valid algorithms:

  • Im2ColGEMM im2col + GEMM algorithm, this is the default
  • NNPackAuto Use NNPack and let it auto detect the best algorithm
  Source Edit
Arraymancer Technical reference Tutorial Spellbook (How-To's) Under the hood