参考链接: torch.Tensor.item() → number
原文及翻译:
item() → number
方法: item() 返回一个数
Returns the value of this tensor as a standard Python number.
This only works for tensors with one element. For other cases,
see tolist().
该方法的功能是以标准的Python数字的形式来返回这个张量的值.这个方法
只能用于只包含一个元素的张量.对于其他的张量,请查看方法tolist().
This operation is not differentiable.
该操作是不可微分的,即不可求导.
(译者注:返回的结果是普通Python数据类型,
自然不能调用backward()方法来进行梯度的反向传播)
Example: 例子:
>>> x = torch.tensor([1.0])
>>> x.item()
1.0
代码实验展示:
Microsoft Windows [版本 10.0.18363.1316]
(c) 2019 Microsoft Corporation。保留所有权利。
C:\Users\chenxuqi>conda activate ssd4pytorch1_2_0
(ssd4pytorch1_2_0) C:\Users\chenxuqi>python
Python 3.7.7 (default, May 6 2020, 11:45:54) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
>>> torch.manual_seed(seed=20200910)
<torch._C.Generator object at 0x0000026F148DD330>
>>>
>>> x = torch.tensor([1.0])
>>> x.item()
1.0
>>> xt = x.item()
>>> xt
1.0
>>> type(xt)
<class 'float'>
>>> type(2.1)
<class 'float'>
>>>
>>> y = torch.tensor([1.0],requires_grad=True)
>>> y
tensor([1.], requires_grad=True)
>>> yt = y.item()
>>> yt
1.0
>>> type(yt)
<class 'float'>
>>> type(2.3)
<class 'float'>
>>>
>>>
>>>
>>>
>>>
>>> yt.backward()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'float' object has no attribute 'backward'
>>> xt.backward()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'float' object has no attribute 'backward'
>>>
>>>
>>>
代码实验展示:
Microsoft Windows [版本 10.0.18363.1316]
(c) 2019 Microsoft Corporation。保留所有权利。
C:\Users\chenxuqi>conda activate ssd4pytorch1_2_0
(ssd4pytorch1_2_0) C:\Users\chenxuqi>python
Python 3.7.7 (default, May 6 2020, 11:45:54) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
>>> x = torch.tensor([1])
>>> x.item()
1
>>> xt = x.item()
>>> xt
1
>>> type(xt)
<class 'int'>
>>> type(20200910)
<class 'int'>
>>> type(33)
<class 'int'>
>>>
>>>
>>>
代码实验展示:
Microsoft Windows [版本 10.0.18363.1316]
(c) 2019 Microsoft Corporation。保留所有权利。
C:\Users\chenxuqi>conda activate ssd4pytorch1_2_0
(ssd4pytorch1_2_0) C:\Users\chenxuqi>python
Python 3.7.7 (default, May 6 2020, 11:45:54) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
>>> torch.manual_seed(seed=20200910)
<torch._C.Generator object at 0x000001B54B7FD330>
>>>
>>> x = torch.randn(1)
>>> x
tensor([0.2824])
>>> x.item()
0.2823888957500458
>>> xt = x.item()
>>> xt
0.2823888957500458
>>>
>>> type(xt)
<class 'float'>
>>> type(1.2)
<class 'float'>
>>>
>>> y = torch.randn(3, 4)
>>> y
tensor([[-0.3715, 0.9088, -1.7601, -0.1806],
[ 2.0937, 1.0406, -1.7651, 1.1216],
[ 0.8440, 0.1783, 0.6859, -1.5942]])
>>>
>>>
>>> y.item()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: only one element tensors can be converted to Python scalars
>>>
>>>
>>>