python中item()的用法

item作用就是把张量tensor的元素取出来,比如一个张量x,指定索引位置后把张量类型转化为python中的一种内置类型,如tensor( 2.5)中的2.5取出来,得到2.5这个浮点数,方便进行数据类型的操作

x = torch.tensor([[1]])
print(x)
print(x.item())
x = torch.tensor(2.5)
print(x)
print(x.item())

python中item()的用法_第1张图片
原API文档也是这么描述的
python中item()的用法_第2张图片
参考链接:https://pytorch.org/docs/stable/tensors.html?highlight=item#torch.Tensor.item

你可能感兴趣的:(计算机视觉,python,深度学习,item)