pad¶
- nvtripy.pad(input: Tensor, pad: Sequence[Tuple[int | DimensionSize, int | DimensionSize]], mode: str = 'constant', value: int | float = 0) Tensor [source]¶
Pads the input tensor.
- Parameters:
input (Tensor) – [dtype=T1] The input tensor.
pad (Sequence[Tuple[int | DimensionSize, int | DimensionSize]]) – A sequence of padding sizes of each dimension. Its length must be equal to the rank of
input
. Each element ofpad
is a tuple of integers orDimensionSize
s(low, high)
, which represents the padding sizes before the lowest index and after the highest index at the corresponding dimension.mode (str) –
The padding mode. Must be one of:
"constant"
: Pads with a constant value (default)."reflect"
: Pads by reflecting the input tensor.
value (int | float) – The padding value for “constant” mode. Has no effect for other modes.
- Returns:
[dtype=T1] The padded tensor.
- Return type:
Example: Constant Padding
1input = tp.reshape(tp.arange(6, dtype=tp.float32), (2, 3)) 2output = tp.pad(input, [(1, 0), (0, 1)])
Local Variables¶>>> input tensor( [[0, 1, 2], [3, 4, 5]], dtype=float32, loc=gpu:0, shape=(2, 3)) >>> output tensor( [[0, 0, 0, 0], [0, 1, 2, 0], [3, 4, 5, 0]], dtype=float32, loc=gpu:0, shape=(3, 4))
Example: Reflect Padding
1input = tp.reshape(tp.arange(6, dtype=tp.float32), (2, 3)) 2output = tp.pad(input, [(1, 1), (1, 1)], mode="reflect")
Local Variables¶>>> input tensor( [[0, 1, 2], [3, 4, 5]], dtype=float32, loc=gpu:0, shape=(2, 3)) >>> output tensor( [[4, 3, 4, 5, 4], [1, 0, 1, 2, 1], [4, 3, 4, 5, 4], [1, 0, 1, 2, 1]], dtype=float32, loc=gpu:0, shape=(4, 5))