[英]How to pad with zeros a tensor along some axis (Python)
I would like to pad a numpy tensor with 0 along the chosen axis.我想沿所选轴用 0 填充 numpy 张量。 For instance, I have tensor r
with shape (4,3,2)
but I am only interested in padding only the last two axis (that is, pad only the matrix).例如,我有形状为(4,3,2)
的张量r
,但我只对填充最后两个轴(即只填充矩阵)感兴趣。 Is it possible to do it with the one-line python code?是否可以使用一行 python 代码来完成?
You can use np.pad()
:您可以使用np.pad()
:
a = np.ones((4, 3, 2))
# npad is a tuple of (n_before, n_after) for each dimension
npad = ((0, 0), (1, 2), (2, 1))
b = np.pad(a, pad_width=npad, mode='constant', constant_values=0)
print(b.shape)
# (4, 6, 5)
print(b)
# [[[ 0. 0. 0. 0. 0.]
# [ 0. 0. 1. 1. 0.]
# [ 0. 0. 1. 1. 0.]
# [ 0. 0. 1. 1. 0.]
# [ 0. 0. 0. 0. 0.]
# [ 0. 0. 0. 0. 0.]]
# [[ 0. 0. 0. 0. 0.]
# [ 0. 0. 1. 1. 0.]
# [ 0. 0. 1. 1. 0.]
# [ 0. 0. 1. 1. 0.]
# [ 0. 0. 0. 0. 0.]
# [ 0. 0. 0. 0. 0.]]
# [[ 0. 0. 0. 0. 0.]
# [ 0. 0. 1. 1. 0.]
# [ 0. 0. 1. 1. 0.]
# [ 0. 0. 1. 1. 0.]
# [ 0. 0. 0. 0. 0.]
# [ 0. 0. 0. 0. 0.]]
# [[ 0. 0. 0. 0. 0.]
# [ 0. 0. 1. 1. 0.]
# [ 0. 0. 1. 1. 0.]
# [ 0. 0. 1. 1. 0.]
# [ 0. 0. 0. 0. 0.]
# [ 0. 0. 0. 0. 0.]]]
This function would pad at the end of certain axis.此功能将在特定轴的末端填充。
If you wish to pad both side, just modify it.如果你想垫两边,只需修改它。
def pad_along_axis(array: np.ndarray, target_length: int, axis: int = 0):
pad_size = target_length - array.shape[axis]
if pad_size <= 0:
return array
npad = [(0, 0)] * array.ndim
npad[axis] = (0, pad_size)
return np.pad(array, pad_width=npad, mode='constant', constant_values=0)
example:例子:
>>> a = np.identity(5)
>>> b = pad_along_axis(a, 7, axis=1)
>>> print(a, a.shape)
[[1. 0. 0. 0. 0.]
[0. 1. 0. 0. 0.]
[0. 0. 1. 0. 0.]
[0. 0. 0. 1. 0.]
[0. 0. 0. 0. 1.]] (5, 5)
>>> print(b, b.shape)
[[1. 0. 0. 0. 0. 0. 0.]
[0. 1. 0. 0. 0. 0. 0.]
[0. 0. 1. 0. 0. 0. 0.]
[0. 0. 0. 1. 0. 0. 0.]
[0. 0. 0. 0. 1. 0. 0.]] (5, 7)
If you want a one-liner go with the solution proposed by @ali_m, but with that you should calculate and set by yourself the number of rows and columns with which to pad the input array.如果你想要一个单线 go 与@ali_m 提出的解决方案,但你应该自己计算和设置用于填充输入数组的行数和列数。 With the following function you can specify directly the target shape and it will return the array symmetrically padded with the value specified:使用以下 function 您可以直接指定目标形状,它将返回用指定值对称填充的数组:
def symmetric_pad_array(input_array: np.ndarray, target_shape: tuple, pad_value: int) -> np.ndarray:
for dim_in, dim_target in zip(input_array.shape, target_shape):
if dim_target < dim_in:
raise Exception("`target_shape` should be greater or equal than `input_array` shape for each axis.")
pad_width = []
for dim_in, dim_target in zip(input_array.shape, target_shape):
if (dim_in-dim_target)%2 == 0:
pad_width.append((int(abs((dim_in-dim_target)/2)), int(abs((dim_in-dim_target)/2))))
else:
pad_width.append((int(abs((dim_in-dim_target)/2)), (int(abs((dim_in-dim_target)/2))+1)))
return np.pad(input_array, pad_width, 'constant', constant_values=pad_value)
>>> a = np.array(np.arange(0,27)).reshape(3,3,3)
>>> target_shape = (3,5,5)
>>> symmetric_pad_array(a, target_shape, pad_value=0)
array([[[ 0, 0, 0, 0, 0],
[ 0, 0, 1, 2, 0],
[ 0, 3, 4, 5, 0],
[ 0, 6, 7, 8, 0],
[ 0, 0, 0, 0, 0]],
[[ 0, 0, 0, 0, 0],
[ 0, 9, 10, 11, 0],
[ 0, 12, 13, 14, 0],
[ 0, 15, 16, 17, 0],
[ 0, 0, 0, 0, 0]],
[[ 0, 0, 0, 0, 0],
[ 0, 18, 19, 20, 0],
[ 0, 21, 22, 23, 0],
[ 0, 24, 25, 26, 0],
[ 0, 0, 0, 0, 0]]])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.