欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

theano扫盲

程序员文章站 2022-03-03 14:37:12
...

项目的原因不得不用一下theano,初见theano,简直是老年人写的代码啊

首先安装theano,常规操作

pip install theano

然后才是重点,要在gpu上运行。嫌麻烦不想用gpu?亲测告诉你,theano代码在一颗cpu上的运行速度可能是一颗gpu上的10分之一。

# 要安装Pygpu,这个包不能直接通过pip安装,需要下载源码编译安装。
# 不过编译过程很简单,也很顺畅。是我见过的最顺畅的编译安装了!!!!
# 参照:https://blog.csdn.net/jay463261929/article/details/78933091

$ git clone https://github.com/Theano/libgpuarray.git
$ cd libgpuarray/
$ mkdir build
$ cd build/
$ cmake .. -DCMAKE_BUILD_TYPE=Release
$ make
$ sudo make install
$ cd ..
$ python2 setup.py build

# 否则 install的过程中需要联网安装依赖包 mako
$ pip --trusted-host=pypi.org --trusted-host=files.pythonhosted.org --trusted-host=download.pytorch.org install mako --user
# 安装mpi4py,用于多gpu运行
$ pip2 --trusted-host=pypi.org --trusted-host=files.pythonhosted.org --trusted-host=download.pytorch.org install mpi4py --user

$ sudo python2 setup.py install
$ sudo ldconfig # 创建动态链接库文件

# 测试
$ THEANO_FLAGS=mode=FAST_RUN,device=cuda0,floatX=float32 python2 theano_gpu.py

测试代码参考:https://blog.csdn.net/autocyz/article/details/51674934

(结果输出虽然能看出调用了gpu(或者用nvidia-smi也能看),但是测试代码输出似乎不正确)

from  theano import function, config, shared, sandbox
import theano.tensor as T
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([], T.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, T.Elemwise) for x in f.maker.fgraph.toposort()]):
    print('Used the cpu')
else:
    print('Used the gpu')

测试代码输出:

$ THEANO_FLAGS=mode=FAST_RUN,device=cuda3,floatX=float32 python2 test.py

# 输出:
Using cuDNN version 7005 on context None
Mapped name None to device cuda3: TITAN Xp (0000:83:00.0)
[GpuElemwise{exp,no_inplace}(<GpuArrayType<None>(float32, vector)>), HostFromGpu(gpuarray)(GpuElemwise{exp,no_inplace}.0)]
Looping 1000 times took 0.228486 seconds
Result is [ 1.23178029  1.61879349  1.52278066 ...,  2.20771813  2.29967761
  1.62323296]
Used the cpu

# 实际是调用了gpu的,但是显示还是cpu
# 为什么会显示成上面这样呢?https://github.com/Theano/Theano/issues/5463

其他测试代码:

# 可能会报错

其他测试一:
$ python2 -c "import theano"

其他测试二:
$ DEVICE=cuda0 python2 -c 'import pygpu;pygpu.test()'

其他测试2可能会报错:
GpuArrayException: Could not load "libnccl.so": libnccl.so: cannot open shared object file: No such file or directory

解决方法:安装nccl,按照官网:https://docs.nvidia.com/deeplearning/sdk/nccl-install-guide/index.html

# 按照官网给出的步骤,下载nccl,可能需要登录nvidia账号
# 下载网址:https://developer.nvidia.com/nccl

# 一系列安装过程
$ sudo dpkg -i Downloads/nccl-repo-ubuntu1604-2.5.6-ga-cuda9.0_1-1_amd64.deb
$ sudo apt update
$ sudo apt install libnccl2 libnccl-dev

# 重新测试
$ DEVICE=cuda0 python2 -c 'import pygpu;pygpu.test()'