How Pytorch Tensor get the index of specific value(Pytorch Tensor 如何获取特定值的索引)
问题描述
在python列表中,我们可以使用list.index(somevalue)
.pytorch 如何做到这一点?
例如:
In python list, we can use list.index(somevalue)
. How can pytorch do this?
For example:
a=[1,2,3]
print(a.index(2))
然后,将输出1
.pytorch 张量如何在不将其转换为 python 列表的情况下执行此操作?
Then, 1
will be output. How can a pytorch tensor do this without converting it to a python list?
推荐答案
我认为没有从 list.index()
到 pytorch 函数的直接转换.但是,您可以使用 tensor==number
和 nonzero()
函数获得类似的结果.例如:
I think there is no direct translation from list.index()
to a pytorch function. However, you can achieve similar results using tensor==number
and then the nonzero()
function. For example:
t = torch.Tensor([1, 2, 3])
print ((t == 2).nonzero(as_tuple=True)[0])
这段代码返回
1
[大小为 1x1 的torch.LongTensor]
[torch.LongTensor of size 1x1]
这篇关于Pytorch Tensor 如何获取特定值的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Pytorch Tensor 如何获取特定值的索引


基础教程推荐
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- 用于分类数据的跳跃记号标签 2022-01-01
- 线程时出现 msgbox 错误,GUI 块 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01
- 筛选NumPy数组 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01