pytorch张量的尺寸扩展方式是什么?
-before:torch.Size([3,3,3])
tensor([[[ 0., 1., 2.],
[ 3., 4., 5.],
[ 6., 7., 8.]],
[[ 9., 10., 11.],
[12., 13., 14.],
[15., 16., 17.]],
[[18., 19., 20.],
[21., 22., 23.],
[24., 25., 26.]]], device='cuda:0', dtype=torch.float64)
-之后:torch.Size([2,3,3,3])
tensor([[[[ 0., 1., 2.],
[ 3., 4., 5.],
[ 6., 7., 8.]],
[[ 9., 10., 11.],
[12., 13., 14.],
[15., 16., 17.]],
[[18., 19., 20.],
[21., 22., 23.],
[24., 25., 26.]]],
[[[0., 1., 2.],
[ 3., 4., 5.],
[ 6., 7., 8.]],
[[ 9., 10., 11.],
[12., 13., 14.],
[15., 16., 17.]],
[[18., 19., 20.],
[21., 22., 23.],
[24., 25., 26.]]]], device='cuda:0', dtype=torch.float64)
在numpy下将像这样工作:
b = np.broadcast_to(a1[None, :,:,:], (2,3,3,3))
在pytorch下如何工作?我想利用GPU。先谢谢您的帮助!
可以添加一个新维度unsqeeze
(0
用于在下面指定第一个维度,即position 0
),然后沿该维度重复两次数据(沿其他维度重复一次,即无重复)。
before = torch.tensor(..., dtype=torch.float64, device='cuda:0')
after = before.unsqueeze(0).repeat(2, 1, 1, 1)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句