经过一系列的努力,我已经在搭载AMD显卡的机器上安装了Theano – Radeon HD 5450 (Cedar)
。
现在,请考虑以下代码。
import numpyimport theanoimport theano.tensor as Trng = numpy.randomN = 400 #样本数量feats = 784 #特征维度D = (rng.randn(N, feats), rng.randint(size=N, low=0, high=2))training_steps = 10000# theano符号变量x = T.matrix("x")y = T.vector("y")w = theano.shared(rng.randn(784), name="w")b = theano.shared(0., name="b")print("初始模型:")print(str(w.get_value()) + " " + str(b.get_value()) )p_1 = 1/(1 + T.exp(-T.dot(x, w) - b)) # 目标为1的概率prediction = p_1 > 0.5 # 预测阈值xent = -y * T.log(p_1) - (1-y)*T.log(1-p_1) # 交叉熵损失函数cost = xent.mean() + 0.01 * (w**2).sum() # 成本 - 要最小化gw, gb = T.grad(cost, [w, b])#编译它train = theano.function( inputs = [x, y], outputs = [prediction, xent], updates = {w: w - 0.1*gw, b: b - 0.1*gb} )predict = theano.function(inputs = [x], outputs = prediction)#训练它for i in range (training_steps): pred, err = train(D[0], D[1])print("最终模型: ")print(str(w.get_value()) + " " + str(b.get_value()) )print("D的目标值: " + str(D[1]))print("D上的预测: " + str(D[0]))
我认为这段代码应该能正常工作。但我得到了一系列错误:
ERROR (theano.gof.opt): 优化失败原因:local_gpua_hgemmERROR (theano.gof.opt): 节点: dot(x.T, Elemwise{sub,no_inplace}.0)ERROR (theano.gof.opt): TRACEBACK:ERROR (theano.gof.opt): Traceback (most recent call last): File "/home/user/anaconda3/lib/python3.5/site-packages/theano/gof/opt.py", line 1772, in process_node replacements = lopt.transform(node) File "/home/user/anaconda3/lib/python3.5/site-packages/theano/sandbox/gpuarray/opt.py", line 140, in local_opt new_op = maker(node, context_name) File "/home/user/anaconda3/lib/python3.5/site-packages/theano/sandbox/gpuarray/opt.py", line 732, in local_gpua_hgemm if nvcc_compiler.nvcc_version < '7.5':TypeError: unorderable types: NoneType() < str()
我多次收到了相同的一组消息。最后显示的是:
File "/home/user/anaconda3/lib/python3.5/site-packages/pygpu-0.2.1-py3.5-linux-x86_64.egg/pygpu/elemwise.py", line 286, in __init__ **self.flags) File "pygpu/gpuarray.pyx", line 1950, in pygpu.gpuarray.GpuKernel.__cinit__ (pygpu/gpuarray.c:24214) File "pygpu/gpuarray.pyx", line 467, in pygpu.gpuarray.kernel_init (pygpu/gpuarray.c:7174)pygpu.gpuarray.UnsupportedException: ('编译节点时发生以下错误', GpuElemwise{Composite{((-i0) - i1)}}[(0, 0)]<gpuarray>(GpuFromHost<None>.0, InplaceGpuDimShuffle{x}.0), '\n', b'设备不支持此操作')
这是否意味着我无法使用此GPU,或者我的代码中有什么问题?此外,从错误信息来看,似乎在寻找nvcc
。但我没有CUDA,我使用的是OpenCL。
>>> import theanoMapped name None to device opencl0:0: Cedar
还有:
>>> from theano import config>>> config.device'opencl0:0'>>> config.cuda<theano.configparser.AddConfigVar.<locals>.SubObj object at 0x7fba9dee7d30>>>> config.nvcc<theano.configparser.AddConfigVar.<locals>.SubObj object at 0x7fba9e5967f0>>>> config.gpu<theano.configparser.AddConfigVar.<locals>.SubObj object at 0x7fbaa9f61828>
那么我接下来该怎么做?有没有办法确保搜索的是clcc
而不是nvcc
。
PS_1: 你好世界程序可以运行。PS_2: 系统 = 14.04 64位
回答:
Theano目前还不支持OpenCL。因此,仅支持NVIDIA GPU。
OpenCL的支持状态在GitHub上有所记录。
你需要通过在Theano配置中设置device=cpu
来禁用GPU操作。有多种方法可以做到这一点(例如通过THEANO_FLAGS
环境变量或通过.theanorc
文件;请查看文档)。
在运行脚本之前,请尝试设置
export THEANO_FLAGS=device=cpu,floatX=float64
你的情况可能需要额外的配置选项。请查看文档了解更多信息。