mindspore.nn.ConstantPad1d
- class mindspore.nn.ConstantPad1d(padding, value)[source]
Using a given constant value to pads the last dimensions of input tensor.
- Parameters
padding (Union[int, tuple]) – The padding size to pad the last dimension of input tensor. If is int, uses the same padding in both boundaries of input’s last dimension. If a 2-tuple, uses (padding_0, padding_1) to pad. If the input is x, the size of last dimension of output is \(padding\_0 + x.shape[-1] + padding\_1\). The remaining dimensions of the output are consistent with those of the input.
- Returns
Tensor, the tensor after padding.
- Raises
TypeError – If padding is not a tuple or int.
TypeError – If value is not int or float.
ValueError – If the length of padding with tuple type is not equal to 2.
ValueError – If the output shape after padding is not positive.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> import numpy as np >>> from mindspore import Tensor >>> from mindspore.nn import ConstantPad1d >>> x = np.ones(shape=(1, 2, 3, 4)).astype(np.float32) >>> x = Tensor(x) >>> # padding is tuple >>> padding = (0, 1) >>> value = 0.5 >>> pad1d = ConstantPad1d(padding, value) >>> out = pad1d(x) >>> print(out) [[[[1. 1. 1. 1. 0.5] [1. 1. 1. 1. 0.5] [1. 1. 1. 1. 0.5]] [[1. 1. 1. 1. 0.5] [1. 1. 1. 1. 0.5] [1. 1. 1. 1. 0.5]]]] >>> print(out.shape) (1, 2, 3, 5) >>> # padding is int >>> padding = 1 >>> value = 0.5 >>> pad1d = ConstantPad1d(padding, value) >>> out = pad1d(x) >>> print(out) [[[[0.5 1. 1. 1. 1. 0.5] [0.5 1. 1. 1. 1. 0.5] [0.5 1. 1. 1. 1. 0.5]] [[0.5 1. 1. 1. 1. 0.5] [0.5 1. 1. 1. 1. 0.5] [0.5 1. 1. 1. 1. 0.5]]]] >>> print(out.shape) (1, 2, 3, 6) >>> # padding is negative >>> padding = (-1, 0) >>> value = 0.5 >>> pad1d = ConstantPad1d(padding, value) >>> out = pad1d(x) >>> print(out) [[[[1. 1. 1.] [1. 1. 1.] [1. 1. 1.]] [[1. 1. 1.] [1. 1. 1.] [1. 1. 1.]]]] >>> print(out.shape) (1, 2, 3, 3)