【问题标题】:PyTorch tensor declared as torch.long becomes torch.int64PyTorch 张量声明为 torch.long 变为 torch.int64
【发布时间】:2021-04-27 17:03:02
【问题描述】:

我是 PyTorch 的新手,所以我对 PyTorch 张量的工作并不多。我感到困惑的是,如果我将张量的 dytpe 声明为torch.long,然后检查它的 dtype 是int64。例如:

In [62]: a = torch.tensor([[0, 1, 1, 2],
                       [1, 0, 2, 1]], dtype=torch.long)
         a.dtype

Out[62]: torch.int64

我可能犯了一些愚蠢的错误。

为什么会这样?

编辑:

     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

在我的情况下,aedge_index

【问题讨论】:

    标签: pytorch tensor dtype


    【解决方案1】:

    the documentation 中我们可以看出torch.longtorch.int64 是同义词,都是指64 位有符号整数类型。

    【讨论】:

    • 啊,我明白了,问题是我需要它来输出torch.long,因为我使用的软件需要它作为输出。
    • 这看起来像是你使用的一些非常脆弱的软件,我无法想象这样的软件被编写为与 pytorch 兼容。请注意,torch.long 永远不会打印,因为torch.long 只是引用与torch.int64 相同的底层对象。 IE。 torch.long is torch.int64 为真,如果你运行print(torch.long)"torch.int64" 将被打印出来。
    • 我使用的软件是 PyTorch Geometric。我在上面的初始帖子中添加了错误出现的位置。
    • edge_index.dtype == torch.long 如果 edge_index 具有 dtype torch.int64,则应评估为 true,因为 torch.int64 == torch.long 为 true。我猜你的问题在别处。
    • 我猜edge_type 那时还没有转换为long 张量。可能相关,但请记住 Tensor.to(dtype=torch.long)Tensor.long() 不是就地操作,因此您需要分配从它们返回的值。比如你需要做x = x.long(),只是把x.long()单独放上是不会有任何效果的。
    猜你喜欢
    • 2022-10-17
    • 1970-01-01
    • 1970-01-01
    • 2015-06-30
    • 1970-01-01
    • 2023-02-23
    • 2013-05-28
    • 2015-09-04
    • 2012-02-13
    相关资源
    最近更新 更多