简体   繁体   English

numpy矩阵乘法或einsum运算中的MemoryError

[英]MemoryError in numpy matrix multiplication or einsum operations

I am working with binary (only 0's and 1's) matrices of rows and columns in the order of a few thousands. 我正在处理数千行的行和列的二进制(仅0和1)矩阵。 For example, the number of rows are between 2000 - 7000 and number of columns are between 4000 - 15000. My computer has more then 100g RAM. 例如,行数在2000-7000之间,列数在4000-15000之间。我的计算机具有100g以上的RAM。

I'm surprised that even with these sizes, I am getting MemoryError with the following code. 令我惊讶的是,即使具有这些大小,我也会收到以下代码的MemoryError For reproducibility, I'm including an example with a smaller matrix (10*20) Note than both of the following raise this error: 为了重现性,我提供了一个矩阵较小(10 * 20)的示例,但以下两个示例均会引起此错误:

   import numpy as np

   my_matrix = np.random.randint(2,size=(10,20))
   tr, tc = np.triu_indices(my_matrix.shape[0],1)
   ut_sums = np.sum(my_matrix[tr] * my_matrix[tc], 1)

   denominator = 100
   value = 1 - ut_sums.astype(float)/denominator
   np.einsum('i->', value)

I tried to replace the elementwise multiplication in the above code to einsum as below, but it also generates the same MemoryError: 我试图将上面代码中的逐元素乘法替换为einsum,如下所示,但它还会生成相同的MemoryError:

   import numpy as np

   my_matrix = np.random.randint(2,size=(10,20))
   tr, tc = np.triu_indices(my_matrix.shape[0],1)
   ut_sums = np.einsum('ij,ij->i', my_matrix[tr], my_matrix[tc])

   denominator = 100
   value = 1 - ut_sums.astype(float)/denominator
   np.einsum('i->', value)

In both cases, the printed Traceback points to the line where ut_sums is being calculated. 在这两种情况下,打印的回溯都指向计算ut_sums的行。

Please note that my code has other operations too, and there are other statistics calculated on matrices of similar sizes, but with more than 100 g, I thought it should not be a problem. 请注意,我的代码也有其他操作,还有其他类似尺寸的矩阵计算出的统计信息,但重量超过100 g时,我认为应该没问题。

Just because your computer has 100 GB of physical memory does not mean that your operating system is willing or able to allocate such large amounts of contiguous memory. 仅仅因为您的计算机具有100 GB的物理内存并不意味着您的操作系统愿意或能够分配如此大量的连续内存。 And it does have to be contiguous, because that's how NumPy arrays usually are. 它必须是连续的,因为那是NumPy数组通常的方式。

You should figure out how large your output matrix is meant to be, and then try creating a similar one by itself: 您应该弄清楚输出矩阵的大小,然后尝试自己创建一个类似的矩阵:

arr = np.zeros((10000, 10000))

See if you're able to allocate a single array as large as you want. 查看是否能够分配所需的单个数组。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM