Pad

Pad // General padding operation

Description

Adds edge and interior padding.

Inputs

Name Element Type Shape
arg Any \((d_1, \ldots, d_n)\)
arg_pad_value Same as arg Scalar

Attributes

Name Description
padding_below Shape of padding added before arg
padding_above Shape of padding added after arg
padding_interior Shape of padding inserted between elements of arg

Outputs

Name Element Type Shape
output Same as arg \((d'_1, \ldots, d'_n)\)
\[d'_i = \mathtt{padding\_below}_i+d_i\cdot(\mathtt{padding\_interior}_i)+\mathtt{padding\_above}_i\]

Takes an input tensor of shape \((d_1,\dots,d_n)\) and pads by inserting a scalar \(x\) supplied as input, in three possible ways:

  1. exterior padding inserts copies of \(x\) below or above the bounds of existing rows, columns, etc.,
  2. interior padding inserts copies of \(x\) between rows, columns, etc., or
  3. both of the above.

The number and position of elements to be inserted along a given axis is determined by three attributes:

  1. the padding-below Shape \((p_1,\ldots,p_n)\),
  2. the padding-above Shape \((q_1,\ldots,q_n)\), and
  3. the interior padding Shape \((r_1,\ldots,r_n)\).

The output tensor will have the shape \((d'_1,\dots,d'_n)\) where \(d'_i = p_i + (d_i - 1)(r_i + 1) + 1 + q_i\) if \(d_i > 0\), and \(d'_i = p_i + q_i\) if \(d_i = 0\).

Example: given a \(3\times 3\) tensor, with interior-padding sizes of \((1,2)\), padding-below of \((1,2)\), padding-above of \((1,0)\), and a pad-value of \(42\), we obtain:

          42 42 42 42 42 42 42 42 42
          42 42  1 42 42  2 42 42  3
1 2 3     42 42 42 42 42 42 42 42 42
4 5 6 --> 42 42  4 42 42  5 42 42  6
7 8 9     42 42 42 42 42 42 42 42 42
          42 42  7 42 42  8 42 42  9
          42 42 42 42 42 42 42 42 42

In other words we have inserted one new row between each pair of adjacent rows, two new columns between each pair of adjacent columns, one new row at the top and two new columns on the left, and one new row at the bottom and zero new columns on the right; then filled the new rows and columns with 42.

Note

The terms below and above here refer respectively to lower- or higher-numbered coordinate indices, and numbering starts at the upper-left corner; thus inserting a row “below” actually inserts it at the “top” of the matrix.

C++ Interface

class Pad : public ngraph::op::Op

Generic constant-padding operation.

Public Functions

Pad(const std::shared_ptr<Node> &arg, const std::shared_ptr<Node> &arg_pad_value, const Shape &padding_below, const Shape &padding_above, const Shape &padding_interior)

Constructs a generic padding operation.

Parameters
  • arg: The node producing input tensor to be padded.
  • arg_pad_value: The node producing the scalar value to be inserted for padding.
  • padding_below: The padding-below widths.
  • padding_above: The padding-above widths.
  • padding_interior: The interior-padding widths.

const Shape &get_padding_below() const

Return
The padding-below sizes.

const Shape &get_padding_above() const

Return
The padding-above sizes.

const Shape &get_padding_interior() const

Return
The interior padding sizes.

std::shared_ptr<Node> get_default_value() const

Return
The default value for Pad.