在 Github 处定义的 torch.tensor.item() 在哪里?
where is torch.tensor.item() defined at the Github?
我是手电筒新手
当我学习下面的 torch 教程时,
我很好奇tensor.item() 的定义。
import torch
a = torch.tensor([1])
print( a.item() ) # it works without problem.
为了找到我不知道的东西,
先,我用的是VScode。但我明白了。
enter image description here
不是这样的
enter image description here
第二个,我在the torch Github
搜索了“def item()”
但是,我找不到。 T^T
你能告诉我在哪里吗tensor.item() is defined at the Github?
或
哪里定义了Class_torch.tensor's Menber functions(Method function)??
简答:
torch/csrc/autograd/python_variable_indexing.cpp:268
长答案:
希望您喜欢 C++。 ;)
首先要知道 item()
在大多数 Python class 中(通常)不是一种方法。相反,Python 将对 item()
的调用转换为其他底层方法,如 __getitem__()
以方便使用。知道:
class Tensor
定义在 torch/tensor.py:40.
Torch 的大部分底层计算密集型功能都是用 C 和 C++ 实现的,包括 Tensor
。 “class 张量”基于 Torch._C.TensorBase
,通过 torch/csrc/autograd/python_variable.cpp:812
的 C API 提供给 Python
THPVariableType
是给 Python 的映射,描述了 Python 的对象上可用的 C++ 函数。它定义在 torch/csrc/autograd/python_variable. The part relevant to you is the tp_as_mapping
entry (line 752), which provides functions for objects implementing the mapping protocol - basically Pyton Array-like objects (Python Documentation)
第 725 行的 THPVariable_as_mapping
结构提供映射 methods.The 第二个变量提供用于按索引获取项目的下标函数 (Python documentation)
因此,C++函数TPHVariable_getitem
提供了Torch.Tensor.item()
的实现,定义在torch/csrc/autograd/python_variable_indexing
我是手电筒新手
当我学习下面的 torch 教程时,
我很好奇tensor.item() 的定义。
import torch
a = torch.tensor([1])
print( a.item() ) # it works without problem.
为了找到我不知道的东西,
先,我用的是VScode。但我明白了。
enter image description here
不是这样的
enter image description here
第二个,我在the torch Github
搜索了“def item()”
但是,我找不到。 T^T
你能告诉我在哪里吗tensor.item() is defined at the Github?
或
哪里定义了Class_torch.tensor's Menber functions(Method function)??
简答:
torch/csrc/autograd/python_variable_indexing.cpp:268
长答案:
希望您喜欢 C++。 ;)
首先要知道 item()
在大多数 Python class 中(通常)不是一种方法。相反,Python 将对 item()
的调用转换为其他底层方法,如 __getitem__()
以方便使用。知道:
class Tensor
定义在 torch/tensor.py:40.
Torch 的大部分底层计算密集型功能都是用 C 和 C++ 实现的,包括 Tensor
。 “class 张量”基于 Torch._C.TensorBase
,通过 torch/csrc/autograd/python_variable.cpp:812
THPVariableType
是给 Python 的映射,描述了 Python 的对象上可用的 C++ 函数。它定义在 torch/csrc/autograd/python_variable. The part relevant to you is the tp_as_mapping
entry (line 752), which provides functions for objects implementing the mapping protocol - basically Pyton Array-like objects (Python Documentation)
第 725 行的 THPVariable_as_mapping
结构提供映射 methods.The 第二个变量提供用于按索引获取项目的下标函数 (Python documentation)
因此,C++函数TPHVariable_getitem
提供了Torch.Tensor.item()
的实现,定义在torch/csrc/autograd/python_variable_indexing