您当前的位置: 首页 >  pytorch

Xavier Jiezou

暂无认证

  • 1浏览

    0关注

    394博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

【pytorch】.item()的用法

Xavier Jiezou 发布时间:2020-11-08 15:00:46 ,浏览量:1

用法描述

Use torch.Tensor.item() to get a Python number from a tensor containing a single value.

.item()方法返回张量元素的值。

用法示例
>>> import torch
>>> x = torch.tensor([[1]])
>>> x
tensor([[1]])
>>> x.item()
1
>>> x = torch.tensor(2.5)
>>> x
tensor(2.5000)
>>> x.item()
2.5
注意事项

张量中只有一个元素才能调用该方法,多个元素会报错:

>>> import torch
>>> x = torch.tensor([1, 2]) 
>>> x
tensor([1, 2])
>>> x.item()
Traceback (most recent call last):
  File "", line 1, in 
ValueError: only one element tensors can be converted to Python scalars
引用参考
https://pytorch.org/docs/stable/tensors.html
关注
打赏
1661408149
查看更多评论
立即登录/注册

微信扫码登录

0.0406s