python - Theano:无法为 "shape.eval()"但不是 "np.asarray"分配内存错误

标签 python theano

当我尝试使用 shape.eval() 打印共享变量的形状时,出现“无法分配内存”错误。但是当我将它转换为 numpy 数组(从而将其移动到 CPU)时,我不仅可以获得形状,还可以获得数组本身。请参阅下面 pdb 中停止的位置,数据已加载到 CPU/RAM 中,然后我尝试打印 p1 的形状:

(Pdb) p1
W
(Pdb) type(p1)
<class 'theano.sandbox.cuda.var.CudaNdarraySharedVariable'>
(Pdb) p1.shape.eval()
Problem occurred during compilation with the command line below:
/usr/bin/g++ -shared -g -O3 -fno-math-errno -Wno-unused-label -Wno-unused-variable -Wno-write-strings -D NPY_NO_DEPRECATED_API=NPY_1_7_API_VERSION -m64 -fPIC -I/usr/lib/python2.7/dist-packages/numpy/core/include -I/usr/include/python2.7 -o /home/ubuntu/.theano/compiledir_Linux-3.13--generic-x86_64-with-Ubuntu-14.04-trusty-x86_64-2.7.6-64/tmpe2w2zz/990b9feb030f7691b8412ea91249349d.so /home/ubuntu/.theano/compiledir_Linux-3.13--generic-x86_64-with-Ubuntu-14.04-trusty-x86_64-2.7.6-64/tmpe2w2zz/mod.cpp -L/usr/lib -lpython2.7
ERROR (theano.gof.cmodule): [Errno 12] Cannot allocate memory
2015-08-20 12:09:19,032 theano.gof.cmodule cmodule.py:1083 ERROR [Errno 12] Cannot allocate memory
*** OSError: [Errno 12] Cannot allocate memory
(Pdb) a1 = np.asarray(p1.eval())
(Pdb) a1.shape
(21504, 2048)
(Pdb) a1
array([[ 0.03940072, -0.03306604, -0.00602638, ..., -0.00931167,
         0.05510775,  0.02733043],
       [snip]
       ...,
       [-0.06175347,  0.03134078, -0.06079643, ..., -0.03223133,
         0.00347659,  0.03415193]], dtype=float32)

p1 的借用设置为 True,但这应该不重要,因为我使用的是 GPU,对吧?

内存中有足够的可用空间:

$ free -m
             total       used       free     shared    buffers     cached
Mem:         15039      14815        224          0          1       4112
-/+ buffers/cache:      10701       4338
Swap:            0          0          0

此错误是 RAM(而不是 GPU 内存)错误,对吧?为什么我能够在将变量获取到 CPU 后打印形状(使用 np.asarray),但 shape.eval() 打印错误?

解决此问题的好方法是什么(此外,可能是获得更多 RAM)?

最佳答案

发现问题了。请参阅here .

eval() 调用 subprocess()(用于编译),而 subprocess() 又调用 fork()

该进程分配了非常大量的虚拟内存 (vsize)。此外,该主机上没有配置交换。尽管执行此 eval() 所需的实际内存很小,但我相信 fork 未能分配虚拟内存,因此会出现错误。

添加交换空间使问题消失。交换空间从未被使用过,但我相信足以满足虚拟内存分配。

关于python - Theano:无法为 "shape.eval()"但不是 "np.asarray"分配内存错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32118273/

相关文章:

python - 在数据框中不使用循环的情况下进行简单的 Excel min 计算

python - Keras 逻辑回归在第一个纪元返回 nan

python - RetryParams 的 min_retries 的目的是什么?

python - dask 数组上的 if 语句

python - python中类方法的自解封

tensorflow - 将 Lasagne BatchNormLayer 转换为 Keras BatchNormalization 层

python - 使用 theano 函数在 keras 中自定义损失函数

python - 如何在 Python 字典中打印给定值的键?

tensorflow - 不可训练参数的定义是什么?

python - Theano:设备 gpu 初始化失败!原因=CNMEM_STATUS_OUT_OF_MEMORY