Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/352.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
类型错误Python类_Python_Caffe - Fatal编程技术网

类型错误Python类

类型错误Python类,python,caffe,Python,Caffe,我有一个具有以下定义的库(caffe): class NetSpec(object): def __init__(self): super(NetSpec, self).__setattr__('tops', OrderedDict()) def __setattr__(self, name, value): self.tops[name] = value def __getattr__(self, name): ret

我有一个具有以下定义的库(caffe):

class NetSpec(object):
    def __init__(self):
        super(NetSpec, self).__setattr__('tops', OrderedDict())

    def __setattr__(self, name, value):
        self.tops[name] = value

    def __getattr__(self, name):
        return self.tops[name]

    def to_proto(self):
        names = {v: k for k, v in self.tops.iteritems()}
        autonames = {}
        layers = OrderedDict()
        for name, top in self.tops.iteritems():
            top.fn._to_proto(layers, names, autonames)
        net = caffe_pb2.NetParameter()
        net.layer.extend(layers.values())
        return net
当我尝试使用
n=caffe.NetSpec()
调用它时,出现以下错误:

  File "../../python/caffe/layers.py", line 84, in __init__
    super(NetSpec, self).__setattr__('tops', OrderedDict())

TypeError: must be type, not None
我检查了一下,但没有找到解决办法。我的班级是一个新型班级,我不明白为什么它不起作用

完整跟踪:
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
运行文件('/home/shaunak/caffe-pr2086/examples/wine/classify.py',wdir='/home/shaunak/caffe-pr2086/examples/wine')
文件“/home/shaunak/anaconda/lib/python2.7/site packages/spyderlib/widgets/externalshell/sitecustomize.py”,第682行,在runfile中
execfile(文件名、命名空间)
文件“/home/shaunak/anaconda/lib/python2.7/site packages/spyderlib/widgets/externalshell/sitecustomize.py”,第78行,在execfile中
execfile(文件名,*其中)
文件“/home/shaunak/caffe-pr2086/examples/wine/classify.py”,第26行,在
f、 写入(str(logreg('examples/hdf5_classification/data/train.txt',10)))
logreg中第18行的文件“/home/shaunak/caffe-pr2086/examples/wine/classify.py”
n=caffe.NetSpec()
文件“./../python/caffe/layers.py”,第84行,在_init中__
super(NetSpec,self)。\uuuu setattr\uuuuu('top',OrderedDict())
TypeError:必须是type,而不是None

您设法将
NetSpec
绑定到
None
某个地方:

>>> super(None, object())
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: must be type, not None
从您共享的代码来看,这可能是罪魁祸首:

from .layers import layers, params, NetSpec

这将导入
caffe.layers
,但将
caffe.layers
重新绑定到
layers()
实例。这可能会触发Python再次删除该模块,因为还没有对它的其他引用(取决于
sys.modules
引用的创建时间和方式),导致所有全局变量都反弹到
None
(包括
NetSpec
)。

@MartijnPieters我重新检查了跟踪并取消了对库的更改。错误仍然是:
super(NetSpec,self)。\uuuu setattr\uuuu('tops',OrderedDict())
-正在更新code@shaunakde:您是否将
NetSpec
设置为
None
?或者在模块被清除时运行此代码?@MartijnPieters-否,它在初始化本身时给我错误<代码>def logreg(hdf5,批量大小):#逻辑回归:数据、矩阵乘法和2类softmax损耗n=caffe.NetSpec()。你能给我一些参考资料吗。我很难跟上你。@VivekSable-我的代码到底有什么问题?我使用的是标准示例。在
n=caffe.NetSpec()
行之前是否可以打印
print caffe.NetSpec
?我已经更新了堆栈轨迹。不幸的是,它来自我的修补文件“../../python/caffe/layers.py”,第84行,在init self中callable@shaunakde:是,因为所有全局变量都设置为
None
。这包括
OrderedDict
@MartijnPieters祝贺你获得了新的国防部职位!祝你好运@MartijnPieters-祝贺你成为国防部的一员。我无法完全解决这个问题,但我正准备修改这个库。但这是最好的解释。我投了你的票
class NetSpec(object):
    def __init__(self):
       self.__dict__['tops'] = OrderedDict()
from .layers import layers, params, NetSpec