简体   繁体   English

如何有效地为多个矩阵中的每个元素分别找到 N 个最大值?

[英]How to efficiently find separately for each element N maximum values among multiple matrices?

I am looping through a large number of H x W matrices.我正在遍历大量 H x W 矩阵。 I cannot store them all in memory.我无法将它们全部存储在 memory 中。 I need to get N matrices.我需要得到 N 个矩阵。 For example, the element of the 1st of N matrix in position (i, j) will be the largest among all elements in position (i, j) of all processed matrix matrices.例如,position (i, j) 中 N 矩阵的第一个元素将是所有已处理矩阵矩阵中 position (i, j) 中所有元素中最大的元素。 For the second of the N matrix, the elements that are the second-largest will be taken, and so on.对于 N 矩阵中的第二个,将取第二大的元素,依此类推。

Example.例子。

在此处输入图像描述 在此处输入图像描述 在此处输入图像描述

Let N = 2. Then the 1st matrix will look like this.让 N = 2。那么第一个矩阵将如下所示。

在此处输入图像描述

And the second matrix is like this.第二个矩阵是这样的。

在此处输入图像描述

How to do such an operation inside a loop so as not to store all matrices in memory?如何在循环内进行这样的操作,以免将所有矩阵存储在 memory 中?

The comments suggested using the np.partition function.评论建议使用np.partition function。 I replaced the use of numpy with cupy , which uses the GPU.我用 cupy 替换了numpy的使用,它使用了 GPU。 And also added a buffer to sort less frequently.并且还添加了一个缓冲区来减少排序频率。

import cupy as np

buf = // # As much as fits into the GPU
largests = np.zeros((buf + N, h, w))
for i in range(num):
    val = //
    largests[i % buf] = val
    if i % buf == buf - 1:
        largests.partition(range(buf, buf + N), axis=0)
largests.partition(range(buf, buf + N), axis=0)  # Let's not forget the tail
res = largests[:-(N + 1):-1]

The solution does not work very quickly, but I have come to terms with this speed.该解决方案的工作速度不是很快,但我已经接受了这种速度。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何获得在 Python/Numpy 中以元素为单位的“n”矩阵的最大值的最有效方法? - How to get the fatest way of getting the maximum values element-wised of "n" matrices in Python/Numpy? 在第一个元素相同的所有值中找到第二个元素最大的元组 - Find those tuples in which the second element is maximum among all values with same first element 如何使用python numpy将多个矩阵的函数应用于矩阵列表的每个元素? - How to apply function of multiple matrices to each element of a list of matrices with python numpy? 如何使用 Pytorch 和/或 Numpy 在多维矩阵数组中有效查找最大值的索引 - How to Efficiently Find the Indices of Max Values in a Multidimensional Array of Matrices using Pytorch and/or Numpy 如何通过R在未排序的数据文件中的类之间找到最大值的索引? - How to find the index of maximum values among classes in an unsorted data file by R? 有效地计算过渡矩阵的元素乘积(m * m)*(n * n),得到(mn * mn)矩阵 - Efficiently computing element wise product of transition matrices (m*m) * (n*n) to give (mn*mn) matrix 如何在sscipy csr_matrix中有效地找到最大元素及其索引? - How can I efficiently find the maximum element and its indices in a scipy csr_matrix? 如何在硒中分别获取类中的每个元素 - how to get each element in a class separately in selenium 如何查找每次迭代中获得的最大值以将其追加到列表中 - how to find maximum of values obtained in each iteration to append it to a list 如何分别显示数组的每个元素 - How to show each element of array separately
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM