[英]Expand Pytorch tensor
The tensor a
in shape of torch.Size([2, 2, 1, 2])
how to expand this tensor to the shape of torch.Size([2, 4, 1, 2])
without using for
loop which takes time torch.Size([2, 2, 1, 2])
形状的张量a
如何在不使用需要时间for
循环的情况下将此张量扩展为torch.Size([2, 4, 1, 2])
的形状
For example, I have tensor a
例如,我有张量a
>>> print(a, a.shape)
tensor([[[[0.2955, 0.8836]],
[[0.7607, 0.6657]]],
[[[0.6779, 0.5109]],
[[0.0785, 0.6564]]]]) torch.Size([2, 2, 1, 2])
I want to expand it to become tensor b
我想将其扩展为张量b
>>> print(b, b.shape)
tensor([[[[0.2955, 0.8836]],
[[0.2955, 0.8836]],
[[0.7607, 0.6657]],
[[0.7607, 0.6657]]],
[[[0.6779, 0.5109]],
[[0.6779, 0.5109]],
[[0.0785, 0.6564]],
[[0.0785, 0.6564]]]]) torch.Size([2, 4, 1, 2])
I tried torch.expand()
but it only expands when the dimension=1.我尝试torch.expand()
但它仅在维度 = 1 时扩展。 How to achieve this format?如何实现这种格式? Thank you谢谢
Pytorch provides multiple functions with different styles to repeat the tensors. Pytorch 提供了多种功能与不同的 styles 重复张量。 The function that you are looking for is named repeat_interleave .您正在寻找的 function 名为repeat_interleave 。 Simple example:简单的例子:
>>> a.repeat_interleave(2,dim=1)
tensor([[[[0.2955, 0.8836]],
[[0.2955, 0.8836]],
[[0.7607, 0.6657]],
[[0.7607, 0.6657]]],
[[[0.6779, 0.5109]],
[[0.6779, 0.5109]],
[[0.0785, 0.6564]],
[[0.0785, 0.6564]]]])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.