Pytorch 增加尺寸大小

问题描述 投票:0回答:1

假设我有一个大小为 (4,4,4,4) 的张量,如何制作一个大小为 (4,4,4,5) 的张量,使得新添加的条目为 1s?

即执行以下操作的正确方法是什么?

#given a tensor points of some size
pshape = list(points.size())
pshape[-1] = 1
z = torch.ones(pshape)
return torch.cat((points, z), -1)
pytorch tensor
1个回答
0
投票

据我所知,对于非单一维度没有特殊的方法来做到这一点。

这里有用的是:

#given a tensor points of some size
pshape = list(points.size())
pshape[-1] += 1
z = torch.ones(pshape)
z[..., 0:-1] = points
© www.soinside.com 2019 - 2024. All rights reserved.