torch.Tensor() new() 收到无效的参数组合 - got (list, dtype=torch.dtype)

AV *_*198 7 python pytorch tensor

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

\n
a = torch.Tensor([0,0,0],dtype = torch.int64)\n\nTypeError: new() received an invalid combination of arguments - got (list, dtype=torch.dtype), but expected one of:\n * (*, torch.device device)\n      didn't match because some of the keywords were incorrect: dtype\n * (torch.Storage storage)\n * (Tensor other)\n * (tuple of ints size, *, torch.device device)\n * (object data, *, torch.device device)\n
Run Code Online (Sandbox Code Playgroud)\n

而如果我们查看官方文档

\n
torch.tensor(data, dtype=None, device=None, requires_grad=False, pin_memory=False) \xe2\x86\x92 Tensor\n
Run Code Online (Sandbox Code Playgroud)\n
\n

参数

\n

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

\n

dtype(torch.dtype,可选)\xe2\x80\x93 返回张量所需的数据类型。默认值:如果无,则从数据推断数据类型。

\n
\n

为什么官方文档支持但代码片段不起作用?

\n

Jan*_*rný 12

大写很重要——在你的顶部示例中,你使用Tensor大写 T,但文档摘录正在谈论tensor小写 t。

  • @infiNity9819 大多数现代编程语言都区分大小写。这里,“torch.Tensor”是一个类,“torch.Tensor(...)”是它的构造函数,“torch.tensor(...)”是一个创建实例的函数(可能会调用构造函数以及一些函数)。其他魔法)。似乎不支持直接使用构造函数构造“Tensor”实例。(在其他语言中,构造函数将设置为私有,但Python不支持这一点。)[官方文档](https://pytorch.org/docs/stable/tensors.html#torch.Tensor)列出了支持的方式构造张量。 (2认同)