【问题标题】:Why Tensor must be Integer in Pytorch?为什么张量在 Pytorch 中必须是整数?
【发布时间】:2019-11-05 05:42:12
【问题描述】:

我想得到我的准确性。所以我将正确的数字(张量)除以总数(张量)。然后我得到一个积分而不是浮点数。我想知道为什么。代码如下。另外,如果我将张量转移到 numpy,为什么我不能得到一个确切的数字?我得到类似"built-in method numpy of Tensor object at 0x00000296AEDC1168" 的描述我仍然不知道确切的数字。

我试过 tensor.numpy

print(correct)
tensor(9668)

print(total)
10000

print(correct/total)
tensor(0)

print(100 * correct/total)
tensor(96)

print((100 * correct/total).numpy)
<built-in method numpy of Tensor object at 0x00000296AEDC1168>

【问题讨论】:

    标签: python pytorch


    【解决方案1】:

    您可以使用.float()correcttotal 转换为浮点类型。像这样:

    correct.float()/total
    

    另外,如果我将张量转移到numpy,为什么我不能得到一个确切的数字?

    转换为numpy的正确方法是.numpy()。在你的情况下:

    (100 * correct/total).numpy()
    

    【讨论】:

    • 谢谢!但我不确定为什么 .numpy 的输出类似于 ,而不是直接数字。如何查看这个号码?
    • .numpy 是对函数的引用。您需要使用.numpy() 调用它。就像您尝试运行 print 而不是 print() 一样。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-09-10
    • 1970-01-01
    • 2020-06-10
    • 1970-01-01
    • 2020-02-21
    • 2016-07-19
    • 2019-05-02
    相关资源
    最近更新 更多