Pad // General padding operation


## Description¶

### Inputs¶

Name

Element Type

Shape

arg

Any

$$(d_1, \ldots, d_n)$$

arg_pad_value

Same as arg

Scalar

### Attributes¶

Name

Description

padding_below

Padding added before arg. May be negative.

padding_above

Padding added after arg. May be negative.

pad_mode

Padding mode: CONSTANT(default), EDGE or REFLECT.

### 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 CoordinateDiff $$(p_1,\ldots,p_n)$$,

2. the padding-above CoordinateDiff $$(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

Public Functions

const NodeTypeInfo &get_type_info() const

Returns the NodeTypeInfo for the node’s class. During transition to type_info, returns a dummy type_info for Node if the class has not been updated yet.

Pad()

Pad(const Output<Node> &arg, const Output<Node> &arg_pad_value, const CoordinateDiff &padding_below, const CoordinateDiff &padding_above, PadMode pad_mode = PadMode::CONSTANT)

Constructs a padding operation. Padding embeds the values of the input tensor into a larger tensor initialized to arg_pad_value.

Parameters
• arg: The node producing the input tensor to be padded.

• arg_pad_value: The node producing the scalar value to be used outside the are initialized by arg when pad_mode is CONSTANT.

• padding_below: How many elements to add on each axis before index 0 of arg. Rank must match arg.

• padding_above: How many elements to add on each axis after the last element of arg. Rank must match arg.

• pad_mode: The padding mode: CONSTANT(default), EDGE, REFLECT or SYMMETRIC. CONSTANT initializes new elements with arg_pad_value, EDGE uses the nearest value from arg. REFLECT and SYMMETRIC tile the background by flipping arg at the edge (SYMMETRIC) or on the last row/column/etc. (REFLECT).

void validate_and_infer_types()

Throws if the node is invalid.

const CoordinateDiff &get_padding_below() const

Return

const CoordinateDiff &get_padding_above() const

Return

const Shape &get_padding_interior() const

DEPRECATED. This is just a stub for backends that used to implement the interior padding feature, which is no longer supported.

Return

Returns a shape full of zeros, with the same rank as get_padding_below().

PadMode get_pad_mode() const

Return

virtual std::shared_ptr<Node> get_default_value() const