python - 使用 nolearn-DBN 分类器时,“garray”对象没有属性 'size'

标签 python python-2.7 numpy python-nolearn

我正在研究openface 。 Openface有unknown classification python cod e.

我正在测试lfw-classification-unknown.py的训练部分。 它使用

进行训练
nolearn-DBN classifier

我安装了nolearn 0.5 版DBN 分类器 有一个对 /usr/local/lib/python2.7/dist-packages/gnumpy.py 的函数调用,并且 我的错误为

File "/usr/local/lib/python2.7/dist-packages/gnumpy.py", line 738, in as_numpy_array
    if self.size==0: return numpy.zeros(self.shape, dtype)
AttributeError: 'garray' object has no attribute 'size'

如何修复该错误?

全部错误是

Traceback (most recent call last):
  File "/usr/lib/python2.7/pdb.py", line 1314, in main
    pdb._runscript(mainpyfile)
  File "/usr/lib/python2.7/pdb.py", line 1233, in _runscript
    self.run(statement)
  File "/usr/lib/python2.7/bdb.py", line 400, in run
    exec cmd in globals, locals
  File "<string>", line 1, in <module>
  File "evaluation/lfw-classification-unknown.py", line 519, in <module>
    train(args)
  File "evaluation/lfw-classification-unknown.py", line 130, in train
    clf.fit(embeddings, labelsNum)
  File "/usr/local/lib/python2.7/dist-packages/nolearn/dbn.py", line 409, in fit
    self.use_dropout,
  File "/usr/local/lib/python2.7/dist-packages/gdbn/dbn.py", line 202, in fineTune
    err, outMB = step(inpMB, targMB, self.learnRates, self.momentum, self.L2Costs, useDropout)
  File "/usr/local/lib/python2.7/dist-packages/gdbn/dbn.py", line 296, in stepNesterov
    targetBatch = targetBatch if isinstance(targetBatch, gnp.garray) else gnp.garray(targetBatch)
  File "/usr/local/lib/python2.7/dist-packages/gnumpy.py", line 735, in __new__
    def __new__(cls, *args, **kwarg): return object.__new__(cls)
  File "/usr/lib/python2.7/bdb.py", line 53, in trace_dispatch
    return self.dispatch_return(frame, arg)
  File "/usr/lib/python2.7/bdb.py", line 88, in dispatch_return
    self.user_return(frame, arg)
  File "/usr/lib/python2.7/pdb.py", line 190, in user_return
    self.interaction(frame, None)
  File "/usr/lib/python2.7/pdb.py", line 209, in interaction
    self.print_stack_entry(self.stack[self.curindex])
  File "/usr/lib/python2.7/pdb.py", line 900, in print_stack_entry
    prompt_prefix)
  File "/usr/lib/python2.7/bdb.py", line 381, in format_stack_entry
    s = s + repr.repr(rv)
  File "/usr/lib/python2.7/repr.py", line 24, in repr
    return self.repr1(x, self.maxlevel)
  File "/usr/lib/python2.7/repr.py", line 34, in repr1
    s = __builtin__.repr(x)
  File "/usr/local/lib/python2.7/dist-packages/gnumpy.py", line 1133, in __repr__
    def __repr__(self): return self.as_numpy_array().__repr__().replace('array(', 'garray(').replace('\n', '\n ').replace(', dtype=float32', '').replace(', dtype=float64', '') # 64 happens for empty arrays
  File "/usr/local/lib/python2.7/dist-packages/gnumpy.py", line 738, in as_numpy_array
    if self.size==0: return numpy.zeros(self.shape, dtype)
AttributeError: 'garray' object has no attribute 'size'
> Uncaught exception. Entering post mortem debugging
Running 'cont' or 'step' will restart the program
> /usr/local/lib/python2.7/dist-packages/gnumpy.py(738)as_numpy_array()
-> if self.size==0: return numpy.zeros(self.shape, dtype)

编辑: 如果不是Debug模式,错误如下。

Traceback (most recent call last):
  File "evaluation/lfw-classification-unknown.py", line 519, in <module>
    train(args)
  File "evaluation/lfw-classification-unknown.py", line 130, in train
    clf.fit(embeddings, labelsNum)
  File "/usr/local/lib/python2.7/dist-packages/nolearn/dbn.py", line 407, in fit
    self.use_dropout,
  File "/usr/local/lib/python2.7/dist-packages/gdbn/dbn.py", line 202, in fineTune
    err, outMB = step(inpMB, targMB, self.learnRates, self.momentum, self.L2Costs, useDropout)
  File "/usr/local/lib/python2.7/dist-packages/gdbn/dbn.py", line 303, in stepNesterov
    errSignals, outputActs, error = self.fpropBprop(inputBatch, targetBatch, useDropout)
  File "/usr/local/lib/python2.7/dist-packages/gdbn/dbn.py", line 262, in fpropBprop
    outputErrSignal = -self.outputActFunct.dErrordNetInput(targetBatch, self.state[-1], outputActs)
  File "/usr/local/lib/python2.7/dist-packages/gdbn/activationFunctions.py", line 138, in dErrordNetInput
    return acts - targets
  File "/usr/local/lib/python2.7/dist-packages/gnumpy.py", line 965, in __sub__
    else: return self + -as_garray(other) # if i need to broadcast, making use of the row add and col add methods is probably faster
  File "/usr/local/lib/python2.7/dist-packages/gnumpy.py", line 926, in __add__
    def __add__(self, other): return _check_number_types(self._broadcastable_op(as_garray_or_scalar(other), 'add'))
  File "/usr/local/lib/python2.7/dist-packages/gnumpy.py", line 614, in _broadcastable_op
    if reduce(operator.or_, ( other.shape[i] not in (1, self.shape[i]) for i in range(self.ndim)), False): raise ValueError('shape mismatch: objects cannot be broadcast to a single shape')
ValueError: shape mismatch: objects cannot be broadcast to a single shape

最佳答案

您尚未发布任何实际代码,因此实际上不可能确切知道您的问题是什么。

编辑

从堆栈跟踪中,很明显您遇到的问题是由于 pdb 尝试在 之后立即获取并打印 garray 的值garray.__new__ 被调用。这是在调用 __init__ 之前,因此 .size 属性还不存在。

gnumpy.garray代码中似乎没有必要重新定义__new__,因此您只需打开即可解决您的问题/usr/local/lib/python2.7/dist-packages/gnumpy.py 然后注释掉第 735 行(即定义 __new__ 的行)。

更简单的是,当您仅运行脚本而不使用pdb(例如python lfw-classification-unknown.py)时会发生什么?看来这个特定的错误会消失。另一方面,您可能首先出于实际原因使用调试器。您最初是否遇到了不同的错误?在这种情况下,这是 XY problem ,您可能应该发布一个新问题来询问原始错误。

不幸的是,gnumpy.garray 需要一个实际的 NVidia GPU 才能运行,而我没有,因此我无法直接测试这些解决方案。

一般问题

话虽如此,似乎在没有设置 .size 属性的情况下创建了一个 garray 对象。这可能是由于您自己的代码或 4 个不同包(openface 加上 3 个依赖项)中的任何一个中的错误所致。以下是对这种情况可能如何发生的非常笼统的概述。

openface依赖于 nolearn ,它依赖于 gbdn ,它依赖于 gnumpy 。详细:

因此,大概是在 buildDBNDBN 中创建 garrays 之一时出现了问题。 .size 属性仅在 the gnumpy.garray._set_shape_info method 时设置叫做。粗略地查看一下 garray 的实现并没有发现任何明显的方式导致初始化期间无法调用 _set_shape_info。然而,确实值得注意的一件事是,至少有十几个不同的代码路径可以用来初始化 garray。如果有一个边缘情况在没有调用 _set_shape_info 的情况下溜过去,这并不奇怪。

关于python - 使用 nolearn-DBN 分类器时,“garray”对象没有属性 'size',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54158005/

相关文章:

python - 如何使用随机值验证单元测试

python - 如何计算和测试并发连接数?

python - 我们如何访问列表中的多个字典?

Python 极其令人费解的正则表达式 unicode 行为

python - 将参数传递给 PyRun_File(***)

Python 2.7 程序(带搁置字典)为特定的键和值组合返回致命的 "dbrunrecoveryerror",但不是其他

python - 查找为线性函数返回相同结果的参数范围

python-3.x - 按列构造一个带有 dtypes 的 numpy 数组

python - 从 R 到 Python 的 case_when 函数

python - 如何在x轴上以正确的频率绘制信号的FFT?