假设我有一个numpy数组x
,形状为[1,5]
,我想沿着0轴展开它,使得到的数组y
的形状为[10,5],并且对于每个i,y[i:i+1,:]
等于x
。
如果x
是pytorchTensor,我可以简单地做
y = x.expand(10,-1)
但是numpy中没有expand
,看起来像它的那些(expand_dims
和repeat
)看起来也不像它。
示例:
>>> import torch
>>> x = torch.randn(1,5)
>>> print(x)
tensor([[ 1.3306, 0.0627, 0.5585, -1.3128, -1.4724]])
>>> print(x.expand(10,-1))
tensor([[ 1.3306, 0.0627, 0.5585, -1.3128, -1.4724],
[ 1.3306, 0.0627, 0.5585, -1.3128, -1.4724],
[ 1.3306, 0.0627, 0.5585, -1.3128, -1.4724],
[ 1.3306, 0.0627, 0.5585, -1.3128, -1.4724],
[ 1.3306, 0.0627, 0.5585, -1.3128, -1.4724],
[ 1.3306, 0.0627, 0.5585, -1.3128, -1.4724],
[ 1.3306, 0.0627, 0.5585, -1.3128, -1.4724],
[ 1.3306, 0.0627, 0.5585, -1.3128, -1.4724],
[ 1.3306, 0.0627, 0.5585, -1.3128, -1.4724],
[ 1.3306, 0.0627, 0.5585, -1.3128, -1.4724]])
3条答案
按热度按时间mqkwyuun1#
你可以用
np.broadcast_to
来实现,但不能用负数:gorkyyrv2#
您可以使用
np.tile
,它会重复指定轴的元素,如下所示:scyqe7ek3#
numpy拥有
numpy.newaxis