为什么广播会给一个空张量?

时间:2019-08-18 10:43:22

标签: pytorch broadcasting

x = torch.randn(1, 1, 0)

y = torch.randn(4, 1, 1)
(x+y)

张量([],size =(4,1,0))

(x + y).shape

torch.Size([4,1,0])

不是应该是4、1、1吗?

2 个答案:

答案 0 :(得分:0)

您的x的第3维等于0-它是一个空张量。 x的三次维度确定结果的第三维:torch.Size([4, 1, 0])-也为空。

尝试

x = torch.randn(1,1,1)

答案 1 :(得分:0)

是设计使然。

张量<ModalDelete parentMethod={setDeleteStateAndDelete} title="Suppresion utilisateur" message="Vous allez supprimer un utilisateur, êtes-vous sur ? "/>} 的一维为0。

x

这些张量有限,我认为设计不好,但这是我的看法。 例如,此类张量无法串联。

import torch
x = torch.randn(1, 1, 0)
print(x) # tensor([], size=(1, 1, 0))

此外,您发现您无法与static void check_cat_no_zero_dim(TensorList tensors) { for(size_t i = 0; i < tensors.size(); ++i) { auto& t = tensors[i]; TORCH_CHECK(t.dim() > 0, "zero-dimensional tensor (at position ", i, ") cannot be concatenated"); } } 运算符一起使用。

所以很可能就像PyTorch张量检查维度是否为负数,实际上应该进行+检查。

>0

再次,这只是我的观点。

相关问题