python - 从张量张量流中获取索引
问题描述
all_max = tf.convert_to_tensor([[4, 2, 3], [3, 4, 5]], dtype=tf.float32)
如何[3,4,5]
从张量数组中获取元素的索引all_max
?
在列表中,我们只是list.index(element)
用来获取列表中存在的元素的索引。
谢谢
解决方案
我发现这个:https://www.py4u.net/discuss/147615 为我工作
要查找 2d/3d 张量的元素索引,请将其转换为 1d #ie example.view(number of elements)
例子:
mat=torch.tensor([[1,2],[4,3])
#to find index of 2
five = 2
mat=mat.view(4)
numb_of_col = 4
for o in range(numb_of_col):
if mat[o] == five:
print(torch.tensor([o]))
推荐阅读
- neo4j - 在 Neo4j 网络浏览器中显示图标
- javascript - '无法读取未定义的属性'XX'' - API 响应对象处于解构状态
- python - 在 Django 视图中加载 CSV 文件并转换为 HTML 表
- angularjs - 如何使用 AngularJs $emit 正确发送事件
- python - 安装成功时无法导入库
- python - 有没有办法简化这个while条件(python)
- javascript - javascript“等待”是否会立即暂停它正在做的任何事情并在承诺解决后恢复异步功能?
- ios - 警报 Xamarin.Ios 版本不匹配
- three.js - 如何单独访问/更改三个.js 中导入的 gltf 对象的材料?
- tfs - TFS Code Review 无法添加审阅者的评论