声明为torch.long的PyTorch张量变为torch.int64
我是PyTorch的新手,所以我对PyTorch张量没有太多研究。我感到困惑的是,如果我将张量的dytpe声明为声明为torch.long的PyTorch张量变为torch.int64,pytorch,tensor,dtype,Pytorch,Tensor,Dtype,我是PyTorch的新手,所以我对PyTorch张量没有太多研究。我感到困惑的是,如果我将张量的dytpe声明为torch.long,然后检查它是int64。例如: In [62]: a = torch.tensor([[0, 1, 1, 2], [1, 0, 2, 1]], dtype=torch.long) a.dtype Out[62]: torch.int64 我可能犯了一些愚蠢的错误 为什么会这样 编辑: 在我的例子中
torch.long
,然后检查它是int64
。例如:
In [62]: a = torch.tensor([[0, 1, 1, 2],
[1, 0, 2, 1]], dtype=torch.long)
a.dtype
Out[62]: torch.int64
我可能犯了一些愚蠢的错误
为什么会这样
编辑:
在我的例子中,
a
是edge\u index
从中我们可以看到torch.long
和torch.int64
是同义词,两者都指64位有符号整数类型。啊,我明白了,问题是我需要它来输出torch.long
,因为我使用的软件需要它作为输出。这看起来像是你正在使用的一些非常脆弱的软件,我无法想象这样的软件是为了与pytorch兼容而编写的。请注意,torch.long
永远不会打印,因为torch.long
只是引用与torch.int64
相同的底层对象。也就是说,torch.long是torch.int64
是true,如果运行print(torch.long)
,则将打印“torch.int64”
。我使用的软件是PyTorch Geometric。我已经在上面的第一篇文章中添加了错误出现的地方。edge\u index.dtype==torch.long
如果edge\u index
有dtypetorch.int64
则应计算为true,因为torch.int64==torch.long
为true。我猜你的问题在别处。我猜,edge\u类型
在那一点上没有被转换成long
张量。可能相关,但请记住Tensor.to(dtype=torch.long)
和Tensor.long()
不是就地操作,因此需要分配从它们返回的值。例如,你需要做x=x.long()
,仅仅把x.long()
放在自己身上是不会有任何效果的。
89 if isinstance(edge_index, Tensor):
---> 90 assert edge_index.dtype == torch.long
91 assert edge_index.dim() == 2
92 assert edge_index.size(0) == 2