python - 获取连续最小比较的索引
问题描述
说如果我有张量
values = torch.tensor([5., 4., 8., 3.])
我想对每两个连续的值取最小值,意思是
min(5., 4.) = 4.
min(8., 3.) = 3.
有没有一种矢量化的方式来做它并且仍然获得最小值的相对索引?意思是我想要的输出是:
min_index = [1, 1]
#min_index[0] == 1 as 4. is the minimum of (5., 4.) and is in index 1 of (5., 4.)
#min_index[1] == 1 as 3. is the minimum of (8., 3.) and is in index 1 of (8., 3.)
解决方案
我认为重塑你的张量会使它更容易。之后torch.min
自动返回最小值和索引。
import torch
values = torch.tensor([5., 4., 8., 3.])
values_reshaped = values.reshape(-1,2) # works for any length
minimums, index = torch.min(values_reshaped, axis = -1)
print(minimums) # tensor of the minimum values
print(index) # tensor of indexes
推荐阅读
- windows - 应用程序的文件夹
- laravel - 获取没有电子邮件列的用户
- android - 从 Facebook 中的开发人员帐户登录时,范围错误消息无效
- java - 我要求用户输入他的(int)ID,然后我想获取 ID 并将其存储在数组中以继续
- rhel - Redhat 的 RHEL8 相当于“apk add --no-cache gcc musl-dev linux-headers”
- r - 如何将图例添加到 geom_point PCA 图?
- javascript - 如何仅更改对象中键的第一个实例
- python - 标记数据时出错。C 错误:第 202 行中应有 1 个字段,在组合 CSV 时看到 2
- java - Java HttpComponents“线程“主”java.lang.NoClassDefFoundError 中的异常:org/apache/hc/core5/http/ParseException”
- javascript - 如何处理 catch 函数?