Theano - NameError: name 'register_gpu_opt' is not defined

Theano - NameError: name 'register_gpu_opt' is not defined

我已经在 GPU 上训练了 Theano 模型,现在想在服务器(没有 GPU)上将其设置为 运行。

首先,由于缺少 CudaNdarray 类型,我遇到了无法解开我的模型的问题。然后,根据 this post 的建议,我将选项 config.experimental.unpickle_gpu_on_cpu 设置为 True

但后来我得到了这个错误:

>>> import cPickle
>>> f = open('results/model.save')
>>> cPickle.load(f)

/home/ubuntu/anaconda/lib/python2.7/site-packages/theano/sandbox/cuda/type.py:541: UserWarning: config.experimental.unpickle_gpu_on_cpu is set to True. Unpickling CudaNdarray as numpy.ndarray
warnings.warn("config.experimental.unpickle_gpu_on_cpu is set to True. Unpickling CudaNdarray as numpy.ndarray")
 Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
   File "/home/ubuntu/anaconda/lib/python2.7/site-packages/theano/sandbox/cuda/opt.py", line 2192, in <module>
     import theano.sandbox.cuda.extra_ops
   File "/home/ubuntu/anaconda/lib/python2.7/site-packages/theano/sandbox/cuda/extra_ops.py", line 424, in <module>
     @register_gpu_opt()
  NameError: name 'register_gpu_opt' is not defined

model.save 文件是通过以下方式生成的:

import cPickle
f = file('results/model.save', 'wb')
model_soft_predict = theano.function([x], layer3.p_y_given_x)
cPickle.dump(model_soft_predict, f, protocol=cPickle.HIGHEST_PROTOCOL)
f.close()

是的,这是一个典型的问题。 为什么会这样?因为theano将模型编译成c/cudac代码,所以当模型发现来自不一致的编译器的代码时会发生错误。

遇到问题如何处理?我会选择将所有参数保存为 numpy 值。例如

values_to_pickle = [p.get_value() for p in model.all_parameters()]