I would like to pad a numpy tensor with 0 along the chosen axis. 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). Is it possible to do it with the one-line python code?
我想在选定的轴上用0填充一个numpy张量。例如,我有张量r形状(4,3,2),但我只对填充最后两个轴(即仅填充矩阵)感兴趣。是否可以使用单行python代码?
2 个解决方案
#1
50
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.]]]
#2
1
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, axis=0):
pad_size = target_length - array.shape[axis]
axis_nb = len(array.shape)
if pad_size < 0:
return a
npad = [(0, 0) for x in range(axis_nb)]
npad[axis] = (0, pad_size)
b = np.pad(array, pad_width=npad, mode='constant', constant_values=0)
return b
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)
#1
50
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.]]]
#2
1
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, axis=0):
pad_size = target_length - array.shape[axis]
axis_nb = len(array.shape)
if pad_size < 0:
return a
npad = [(0, 0) for x in range(axis_nb)]
npad[axis] = (0, pad_size)
b = np.pad(array, pad_width=npad, mode='constant', constant_values=0)
return b
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)