如何在 PyTorch 中获取张量的值? [英] How do I get the value of a tensor in PyTorch?

查看:59
本文介绍了如何在 PyTorch 中获取张量的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

打印张量给出:

<预><代码>>>>x = torch.tensor([3])>>>打印(x)张量([3])

同样索引它的 .data 给出:

<预><代码>>>>x.data[0]张量(3)

我如何获得 3 的值?

解决方案

将张量转换为 numpy:

x.numpy()[0]

Printing the tensor gives:

>>> x = torch.tensor([3])
>>> print(x)
tensor([3])

Likewise indexing its .data gives:

>>> x.data[0]
tensor(3)

How do I get just the value 3?

解决方案

Convert tensor to numpy:

x.numpy()[0]

这篇关于如何在 PyTorch 中获取张量的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆