gpt4 book ai didi

python - 在不同机器上分配大 ndarray 时出现 MemoryError 与 "ValueError: array is too big"

转载 作者:行者123 更新时间:2023-12-01 08:14:02 25 4
gpt4 key购买 nike

我在两台独立的设备上运行 Python - 2013 年中的 MacBook Air(笔记本电脑 1)和 ThinkPad X1 Yoga 3G(笔记本电脑 2) - 并在两台设备上创建 numpy 数组。尽管两台笔记本电脑的内存相对相似:

笔记本电脑 1:内存 4 GB 1600 MHz DDR3

笔记本电脑 2:已安装 RAM 16.0 GB(15.8 GB 可用)

在观察 MemoryError 之前,我发现两台笔记本电脑上的阈值数组大小截然不同。例如:

笔记本电脑 1

>>> import numpy as np
>>> np.zeros(int(5.*10.**12))
array([0., 0., 0., ..., 0., 0., 0.])
>>> np.zeros(int(6.*10.**12))
Python(6138,0x7fffda9413c0) malloc: *** mach_vm_map(size=48000000000000) failed (error code=3)
*** error: can't allocate region
*** set a breakpoint in malloc_error_break to debug
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
MemoryError

笔记本电脑 2

>>> import numpy as np
>>> np.zeros(int(1.*10.**8.))
array([0., 0., 0., ..., 0., 0., 0.])
>>> np.zeros(int(5.*10.**8.))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: array is too big; `arr.size * arr.dtype.itemsize` is larger than the maximum possible size.
>>> np.zeros(int(5.*10.**9.))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: Maximum allowed dimension exceeded
>>> import numpy as np
>>> np.zeros(int(1.*10.**8.))
array([0., 0., 0., ..., 0., 0., 0.])
>>> np.zeros(int(5.*10.**8.))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: array is too big; `arr.size * arr.dtype.itemsize` is larger than the maximum possible size.
>>> np.zeros(int(5.*10.**9.))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: Maximum allowed dimension exceeded

Linux:

Python 3.6.7 |Anaconda, Inc.| (default, Oct 23 2018, 19:16:44) 
[GCC 7.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
>>> np.zeros(int(5.*10.**8))
array([0., 0., 0., ..., 0., 0., 0.])
>>> np.zeros(int(5.*10.**9))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
MemoryError

因此,在接收 MemoryError 之前,长度(大约)5 万亿的数组似乎是笔记本电脑 1 的限制。 ,而Laptop 2的阈值更接近5亿左右。这本质上是 4 个数量级的差异,但每台笔记本电脑上的可用内存相差并不大。我还尝试在两台具有大约 4 GB RAM 的 Linux 计算机上创建类似的数组,并观察到大约 10 亿的长度是极限。

我显然遗漏了一些可能显而易见的东西。如果能澄清为什么会出现这种差异,我们将不胜感激。

最佳答案

由于您收到 ValueError: array is too big; arr.size * arr.dtype.itemsize 大于最大可能大小。 而不是第二台机器上的 MemoryError,原因一定是该机器上的 Python 或 numpy 的体系结构限制,而不是系统内存限制。

最有可能的是,您在第二台机器上使用 32 位 Python:searching the error message in numpy's codebase显示 this error is raised when calculating array size results in integer overflow .

关于python - 在不同机器上分配大 ndarray 时出现 MemoryError 与 "ValueError: array is too big",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55072697/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com