【问题标题】:torch.Tensor() new() received an invalid combination of arguments - got (list, dtype=torch.dtype)torch.Tensor() new() 收到无效的参数组合 - got (list, dtype=torch.dtype)
【发布时间】:2021-05-29 07:16:20
【问题描述】:

当我尝试运行以下简单行时,我遇到了一个非常奇怪的错误:

a = torch.Tensor([0,0,0],dtype = torch.int64)

TypeError: new() received an invalid combination of arguments - got (list, dtype=torch.dtype), but expected one of:
 * (*, torch.device device)
      didn't match because some of the keywords were incorrect: dtype
 * (torch.Storage storage)
 * (Tensor other)
 * (tuple of ints size, *, torch.device device)
 * (object data, *, torch.device device)

如果我们查看官方文档

torch.tensor(data, dtype=None, device=None, requires_grad=False, pin_memory=False) → Tensor

参数

data (array_like) – 张量的初始数据。可以是一个 列表、元组、NumPy ndarray、标量和其他类型。

dtype (torch.dtype, optional) – 返回的所需数据类型 张量。默认值:如果为 None,则从数据中推断数据类型。

为什么官方文档支持sn-p代码却不起作用?

【问题讨论】:

    标签: python pytorch tensor


    【解决方案1】:

    大写很重要——在上面的例子中,Tensor 带有大写 T,但文档摘录是在谈论 tensor 带有小写 t。

    【讨论】:

    • 你有定义“Tensor()”函数的链接吗?由于找不到大写的 t,我一开始以为小写的 t 和大写的 t 是一样的(可能大写的 T 是小写的别名)
    • @infiNity9819 大多数现代编程语言都区分大小写。这里,torch.Tensor 是一个类,torch.Tensor(...) 是它的构造函数,torch.tensor(...) 是一个创建实例的函数(可能与其他魔法一起调用构造函数)。似乎不支持直接使用构造函数构造Tensor 实例。 (在其他语言中,构造函数将设置为私有,但 Python 不支持。)official documentation 列出了支持的构造张量的方式。
    猜你喜欢
    • 2021-08-10
    • 1970-01-01
    • 1970-01-01
    • 2019-07-26
    • 2020-08-27
    • 1970-01-01
    • 2019-06-12
    • 1970-01-01
    • 2019-08-04
    相关资源
    最近更新 更多