Pad#
Pad - 18#
Version
name: Pad (GitHub)
domain: main
since_version: 18
function: False
support_level: SupportType.COMMON
shape inference: True
This version of the operator has been available since version 18.
Summary
Given a tensor containing the data to be padded (data), a tensor containing the number of start and end pad values for axis (pads), (optionally) a mode, and (optionally) constant_value, a padded tensor (output) is generated.
The three supported modes are (similar to corresponding modes supported by numpy.pad):
constant`(default) - pads with a given constant value as specified by `constant_value (which defaults to 0, empty string, or False)
reflect - pads with the reflection of the vector mirrored on the first and last values of the vector along each axis
edge - pads with the edge values of array
- Example 1 (constant mode):
Insert 0 pads to the beginning of the second dimension.
data = [
[1.0, 1.2], [2.3, 3.4], [4.5, 5.7],
]
pads = [0, 2, 0, 0]
mode = ‘constant’
constant_value = 0.0
output = [
[0.0, 0.0, 1.0, 1.2], [0.0, 0.0, 2.3, 3.4], [0.0, 0.0, 4.5, 5.7],
]
- Example 2 (reflect mode):
data = [
[1.0, 1.2], [2.3, 3.4], [4.5, 5.7],
]
pads = [0, 2, 0, 0]
mode = ‘reflect’
output = [
[1.0, 1.2, 1.0, 1.2], [2.3, 3.4, 2.3, 3.4], [4.5, 5.7, 4.5, 5.7],
]
- Example 3 (edge mode):
data = [
[1.0, 1.2], [2.3, 3.4], [4.5, 5.7],
]
pads = [0, 2, 0, 0]
mode = ‘edge’
output = [
[1.0, 1.0, 1.0, 1.2], [2.3, 2.3, 2.3, 3.4], [4.5, 4.5, 4.5, 5.7],
]
Attributes
mode: Supported modes: constant`(default), `reflect, edge
Inputs
Between 2 and 4 inputs.
data (heterogeneous) - T: Input tensor.
pads (heterogeneous) - tensor(int64): Tensor of integers indicating the number of padding elements to add or remove (if negative) at the beginning and end of each axis. For 2D input tensor, it is the number of pixels. pads should be a 1D tensor of shape [2 * num_axes] where num_axes refers to the number of elements in the axes input or the input rank if axes are not provided explicitly. pads format should be: [x1_begin, x2_begin, …, x1_end, x2_end,…], where xi_begin is the number of pad values added at the beginning of axis axes[i] and xi_end, the number of pad values added at the end of axis axes[i].
constant_value (optional, heterogeneous) - T: (Optional) A scalar value to be used if the mode chosen is constant (by default it is 0, empty string or False).
axes (optional, heterogeneous) - Tind: 1-D tensor of axes that pads apply to. Negative value means counting dimensions from the back. Accepted range is [-r, r-1] where r = rank(data). Behavior is undefined if an axis is repeated. If not provided, all axes are assumed ([0, 1, …, input_rank-1]).
Outputs
output (heterogeneous) - T: Tensor after padding.
Type Constraints
T in ( tensor(bfloat16), tensor(bool), tensor(complex128), tensor(complex64), tensor(double), tensor(float), tensor(float16), tensor(int16), tensor(int32), tensor(int64), tensor(int8), tensor(string), tensor(uint16), tensor(uint32), tensor(uint64), tensor(uint8) ): Constrain input and output types to all tensor types.
Tind in ( tensor(int32), tensor(int64) ): Constrain indices to integer types
Examples
_constant_pad
import numpy as np
import onnx
node = onnx.helper.make_node(
"Pad", inputs=["x", "pads", "value"], outputs=["y"], mode="constant"
)
x = np.random.randn(1, 3, 4, 5).astype(np.float32)
pads = np.array([0, 0, 1, 3, 0, 0, 2, 4]).astype(
np.int64
) # pad order [x1_begin, x2_begin, ..., x1_end, x2_end, ...]
value = np.float32(1.2)
y = pad_impl(x, pads, "constant", 1.2)
expect(node, inputs=[x, pads, value], outputs=[y], name="test_constant_pad")
_reflection_and_edge_pad
import numpy as np
import onnx
for mode in ["edge", "reflect"]:
node = onnx.helper.make_node(
"Pad", inputs=["x", "pads"], outputs=["y"], mode=mode
)
x = np.random.randn(1, 3, 4, 5).astype(np.int32)
pads = np.array([0, 0, 1, 1, 0, 0, 1, 1]).astype(
np.int64
) # pad order [x1_begin, x2_begin, ..., x1_end, x2_end, ...]
y = pad_impl(x, pads, mode)
expect(node, inputs=[x, pads], outputs=[y], name=f"test_{mode}_pad")
_constant_pad_axes
import numpy as np
import onnx
node = onnx.helper.make_node(
"Pad", inputs=["x", "pads", "value", "axes"], outputs=["y"], mode="constant"
)
x = np.random.randn(1, 3, 4, 5).astype(np.float32)
pads = np.array([0, 3, 0, 4]).astype(
np.int64
) # pad order [x1_begin, x2_begin, ..., x1_end, x2_end, ...]
value = np.float32(1.2)
axes = np.array([1, 3], dtype=np.int64)
y = pad_impl(
x,
pads,
"constant",
1.2,
[1, 3],
)
expect(
node,
inputs=[x, pads, value, axes],
outputs=[y],
name="test_constant_pad_axes",
)
Pad - 13#
Version
name: Pad (GitHub)
domain: main
since_version: 13
function: False
support_level: SupportType.COMMON
shape inference: True
This version of the operator has been available since version 13.
Summary
Given a tensor containing the data to be padded (data), a tensor containing the number of start and end pad values for axis (pads), (optionally) a mode, and (optionally) constant_value, a padded tensor (output) is generated.
The three supported modes are (similar to corresponding modes supported by numpy.pad):
constant`(default) - pads with a given constant value as specified by `constant_value (which defaults to 0, empty string, or False)
reflect - pads with the reflection of the vector mirrored on the first and last values of the vector along each axis
edge - pads with the edge values of array
- Example 1 (constant mode):
Insert 0 pads to the beginning of the second dimension.
data = [
[1.0, 1.2], [2.3, 3.4], [4.5, 5.7],
]
pads = [0, 2, 0, 0]
mode = ‘constant’
constant_value = 0.0
output = [
[0.0, 0.0, 1.0, 1.2], [0.0, 0.0, 2.3, 3.4], [0.0, 0.0, 4.5, 5.7],
]
- Example 2 (reflect mode):
data = [
[1.0, 1.2], [2.3, 3.4], [4.5, 5.7],
]
pads = [0, 2, 0, 0]
mode = ‘reflect’
output = [
[1.0, 1.2, 1.0, 1.2], [2.3, 3.4, 2.3, 3.4], [4.5, 5.7, 4.5, 5.7],
]
- Example 3 (edge mode):
data = [
[1.0, 1.2], [2.3, 3.4], [4.5, 5.7],
]
pads = [0, 2, 0, 0]
mode = ‘edge’
output = [
[1.0, 1.0, 1.0, 1.2], [2.3, 2.3, 2.3, 3.4], [4.5, 4.5, 4.5, 5.7],
]
Attributes
mode: Supported modes: constant`(default), `reflect, edge
Inputs
Between 2 and 3 inputs.
data (heterogeneous) - T: Input tensor.
pads (heterogeneous) - tensor(int64): Tensor of integers indicating the number of padding elements to add or remove (if negative) at the beginning and end of each axis. For 2D input tensor, it is the number of pixels. pads should be a 1D tensor of shape [2 * input_rank]. pads format should be: [x1_begin, x2_begin,…,x1_end, x2_end,…], where xi_begin is the number of pad values added at the beginning of axis i and xi_end, the number of pad values added at the end of axis i.
constant_value (optional, heterogeneous) - T: (Optional) A scalar value to be used if the mode chosen is constant (by default it is 0, empty string or False).
Outputs
output (heterogeneous) - T: Tensor after padding.
Type Constraints
T in ( tensor(bfloat16), tensor(bool), tensor(complex128), tensor(complex64), tensor(double), tensor(float), tensor(float16), tensor(int16), tensor(int32), tensor(int64), tensor(int8), tensor(string), tensor(uint16), tensor(uint32), tensor(uint64), tensor(uint8) ): Constrain input and output types to all tensor types.
Pad - 11#
Version
name: Pad (GitHub)
domain: main
since_version: 11
function: False
support_level: SupportType.COMMON
shape inference: True
This version of the operator has been available since version 11.
Summary
Given a tensor containing the data to be padded (data), a tensor containing the number of start and end pad values for axis (pads), (optionally) a mode, and (optionally) constant_value, a padded tensor (output) is generated.
The three supported modes are (similar to corresponding modes supported by numpy.pad):
constant`(default) - pads with a given constant value as specified by `constant_value (which defaults to 0)
reflect - pads with the reflection of the vector mirrored on the first and last values of the vector along each axis
edge - pads with the edge values of array
- Example 1 (constant mode):
Insert 0 pads to the beginning of the second dimension.
data = [
[1.0, 1.2], [2.3, 3.4], [4.5, 5.7],
]
pads = [0, 2, 0, 0]
mode = ‘constant’
constant_value = 0.0
output = [
[0.0, 0.0, 1.0, 1.2], [0.0, 0.0, 2.3, 3.4], [0.0, 0.0, 4.5, 5.7],
]
- Example 2 (reflect mode):
data = [
[1.0, 1.2], [2.3, 3.4], [4.5, 5.7],
]
pads = [0, 2, 0, 0]
mode = ‘reflect’
output = [
[1.0, 1.2, 1.0, 1.2], [2.3, 3.4, 2.3, 3.4], [4.5, 5.7, 4.5, 5.7],
]
- Example 3 (edge mode):
data = [
[1.0, 1.2], [2.3, 3.4], [4.5, 5.7],
]
pads = [0, 2, 0, 0]
mode = ‘edge’
output = [
[1.0, 1.0, 1.0, 1.2], [2.3, 2.3, 2.3, 3.4], [4.5, 4.5, 4.5, 5.7],
]
Attributes
mode: Supported modes: constant`(default), `reflect, edge
Inputs
Between 2 and 3 inputs.
data (heterogeneous) - T: Input tensor.
pads (heterogeneous) - tensor(int64): Tensor of integers indicating the number of padding elements to add or remove (if negative) at the beginning and end of each axis. For 2D input tensor, it is the number of pixels. pads should be a 1D tensor of shape [2 * input_rank]. pads format should be: [x1_begin, x2_begin,…,x1_end, x2_end,…], where xi_begin is the number of pad values added at the beginning of axis i and xi_end, the number of pad values added at the end of axis i.
constant_value (optional, heterogeneous) - T: (Optional) A scalar value to be used if the mode chosen is constant (by default it is 0).
Outputs
output (heterogeneous) - T: Tensor after padding.
Type Constraints
T in ( tensor(double), tensor(float), tensor(float16), tensor(int16), tensor(int32), tensor(int64), tensor(int8), tensor(uint16), tensor(uint32), tensor(uint64), tensor(uint8) ): Constrain input and output to only numeric types.
Pad - 2#
Version
name: Pad (GitHub)
domain: main
since_version: 2
function: False
support_level: SupportType.COMMON
shape inference: True
This version of the operator has been available since version 2.
Summary
Given data tensor, pads, mode, and value. Example:
Insert 0 pads to the beginning of the second dimension. data = [
[1.0, 1.2], [2.3, 3.4], [4.5, 5.7],
] pads = [0, 2, 0, 0] output = [
- [
[0.0, 0.0, 1.0, 1.2], [0.0, 0.0, 2.3, 3.4], [0.0, 0.0, 4.5, 5.7],
],
]
Attributes
mode: Three modes: constant(default), reflect, edge
pads (required): List of integers indicating the number of padding elements to add or remove (if negative) at the beginning and end of each axis. For 2D it is the number of pixels. pads rank should be double of the input’s rank. pads format should be as follow [x1_begin, x2_begin…x1_end, x2_end,…], where xi_begin the number of pixels added at the beginning of axis i and xi_end, the number of pixels added at the end of axis i.
value: One float, indicates the value to be filled.
Inputs
data (heterogeneous) - T: Input tensor.
Outputs
output (heterogeneous) - T: Tensor after padding.
Type Constraints
T in ( tensor(double), tensor(float), tensor(float16) ): Constrain input and output types to float tensors.
Pad - 1#
Version
name: Pad (GitHub)
domain: main
since_version: 1
function: False
support_level: SupportType.COMMON
shape inference: False
This version of the operator has been available since version 1.
Summary
Given data tensor, paddings, mode, and value. Example:
Insert 0 paddings to the beginning of the second dimension. data = [
[1.0, 1.2], [2.3, 3.4], [4.5, 5.7],
] paddings = [0, 0, 2, 0] output = [
- [
[0.0, 0.0, 1.0, 1.2], [0.0, 0.0, 2.3, 3.4], [0.0, 0.0, 4.5, 5.7],
],
]
Attributes
mode: Three modes: constant(default), reflect, edge
paddings (required): List of integers indicate the padding element count at the beginning and end of each axis, for 2D it is the number of pixel. paddings rank should be double of the input’s rank. paddings format should be as follow [x1_begin, x2_begin…x1_end, x2_end,…], where xi_begin the number of pixels added at the beginning of axis i and xi_end, the number of pixels added at the end of axis i.
value: One float, indicates the value to be filled, default is 0
Inputs
data (heterogeneous) - T: Input tensor.
Outputs
output (heterogeneous) - T: Tensor after padding.
Type Constraints
T in ( tensor(double), tensor(float), tensor(float16) ): Constrain input and output types to float tensors.