1.创建虚拟环境
conda create -n th_py2 python=2.7
2. 安装theano
conda install theano
默认安装1.0版本
3.配置GPU
各种采坑……
运行下列命令,配置theanorc文件
vi ~/.theanorc
在其中编写
[global]
floatX=float32
device=gpu
optimizer=fast_compile
optimizer_including=cudnn # if you have successfully installed cudnn, otherwise remove it.
[lib]
cnmem=0.8
[blas]
ldflas=-lopenblas
[nvcc]
fastmath=True
flags=-D_FORCE_INLINES
flags=-arch=sm_50 # if your nvidia card is too recent but cuda-toolkit is older, for example gtx1060 and cuda7.5, you have to set this.
[cuda]
root=/usr/local/cuda-9.0/
保存并退出。
运行测试文档:
from theano import function, config, shared, tensor
import numpy
import time
vlen = 10 * 30 * 768 # 10 x #cores x # threads per core
iters = 1000
rng = numpy.random.RandomState(22)
x = shared(numpy.asarray(rng.rand(vlen), config.floatX))
f = function([], tensor.exp(x))
print(f.maker.fgraph.toposort())
t0 = time.time()
for i in range(iters):
r = f()
t1 = time.time()
print("Looping %d times took %f seconds" % (iters, t1 - t0))
print("Result is %s" % (r,))
if numpy.any([isinstance(x.op, tensor.Elemwise) and
('Gpu' not in type(x.op).__name__)
for x in f.maker.fgraph.toposort()]):
print('Used the cpu')
else:
print('Used the gpu')
如果出现报错:
/home/cc/anaconda3/envs/th_py2/lib/python2.7/site-packages/numpy/core/include/numpy/ndarraytypes.h(84): error: expected a "}"
/home/cc/anaconda3/envs/th_py2/lib/python2.7/site-packages/numpy/core/include/numpy/ndarraytypes.h(89): warning: parsing restarts here after previous syntax error
/home/cc/anaconda3/envs/th_py2/lib/python2.7/site-packages/numpy/core/include/numpy/ndarraytypes.h(453): error: identifier "NPY_NTYPES_ABI_COMPATIBLE" is undefined
...
WARNING (theano.sandbox.cuda): CUDA is installed, but device gpu is not available (error: cuda unavailable)
...
Used the cpu
更改 /home/cc/anaconda3/envs/th_py2/lib/python2.7/site-packages/numpy/core/include/numpy/ndarraytypes.h
, 在第84行,将整个NPY_ATTR_DEPRECATE行放在注释符号 / * * /中,包括结束逗号,保存和重新测试。
继续报错:
ERROR (theano.sandbox.cuda): Failed to compile cuda_ndarray.cu: libcublas.so.9.0: cannot open shared object file: No such file or directory
...
WARNING (theano.sandbox.cuda): CUDA is installed, but device gpu is not available (error: cuda unavailable)
在~/.bashrc
文件中添加
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/cuda-9.0/lib64
export PATH=$PATH:/usr/local/cuda-9.0/bin
export CUDA_HOME=$CUDA_HOME:/usr/local/cuda-9.0
报错
UserWarning: Your cuDNN version is more recent than the one Theano officially supports. If you see any problems, try updating Theano or downgrading cuDNN to version 5.1.
...
Exception: The nvidia driver version installed with this OS does not give good results for reduction.Installing the nvidia driver available on the same download page as the cuda package will fix the problem: http://developer.nvidia.com/cuda-downloads
这是因为我安装的是theano-0.9,而cudnn版本是9.0,因此升级theano……
conda install theano
继续测试,报错:
ValueError: You are tring to use the old GPU back-end. It was removed from Theano. Use device=cuda* now. See https://github.com/Theano/Theano/wiki/Converting-to-the-new-gpu-back-end%28gpuarray%29 for more information.
更改 ~/.theanorc
device = cuda
运行测试文档显示
Using cuDNN version 7104 on context None
Mapped name None to device cuda: GeForce GTX 1080 Ti (0000:02:00.0)
[GpuElemwise{exp,no_inplace}(<GpuArrayType<None>(float32, vector)>), HostFromGpu(gpuarray)(GpuElemwise{exp,no_inplace}.0)]
('Looping 1000 times took', 0.3902170658111572, 'seconds')
('Result is', array([1.2317803, 1.6187935, 1.5227807, ..., 2.2077181, 2.2996776,
1.623233 ], dtype=float32))
used the cpu
运行时间为0.4s,应该是已经在gpu上运行了,但一直显示“used the cpu”(未解决)