theano使用GPU踩坑

Posted bluebluesea

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了theano使用GPU踩坑相关的知识,希望对你有一定的参考价值。

1.安装pygpu的部分

#使用豆瓣源or不使用,均安装失败
pip install pygpu  -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com
#报错:
Looking in indexes: http://pypi.douban.com/simple/
Collecting pygpu
  Could not find a version that satisfies the requirement pygpu (from versions: )
No matching distribution found for pygpu
#继续尝试使用conda,仍失败
conda install -c conda-forge pygpu`
#报错:
    Traceback (most recent call last):
      File "/data_d/public/miniconda2/lib/python2.7/site-packages/conda/exceptions.py", line 640, in conda_exception_handler
        return_value = func(*args, **kwargs)
      File "/data_d/public/miniconda2/lib/python2.7/site-packages/conda/cli/main.py", line 140, in _main
        exit_code = args.func(args, p)
      File "/data_d/public/miniconda2/lib/python2.7/site-packages/conda/cli/main_update.py", line 65, in execute
        install(args, parser, update)
      File "/data_d/public/miniconda2/lib/python2.7/site-packages/conda/cli/install.py", line 231, in install
        unknown=index_args[unknown], prefix=prefix)
      File "/data_d/public/miniconda2/lib/python2.7/site-packages/conda/core/index.py", line 101, in get_index
        index = fetch_index(channel_priority_map, use_cache=use_cache)
      File "/data_d/public/miniconda2/lib/python2.7/site-packages/conda/core/index.py", line 120, in fetch_index
        repodatas = collect_all_repodata(use_cache, tasks)
      File "/data_d/public/miniconda2/lib/python2.7/site-packages/conda/core/repodata.py", line 75, in collect_all_repodata
        repodatas = _collect_repodatas_serial(use_cache, tasks)
      File "/data_d/public/miniconda2/lib/python2.7/site-packages/conda/core/repodata.py", line 485, in _collect_repodatas_serial
        for url, schan, pri in tasks]
      File "/data_d/public/miniconda2/lib/python2.7/site-packages/conda/core/repodata.py", line 115, in func
        res = f(*args, **kwargs)
      File "/data_d/public/miniconda2/lib/python2.7/site-packages/conda/core/repodata.py", line 473, in fetch_repodata
        with open(cache_path, w) as fo:
    IOError: [Errno 13] Permission denied: u/data_d/public/miniconda2/pkgs/cache/6846611e.json
#尝试使用制定安装版本
pip install pygpu==0.7.5
#仍报错:
Collecting pygpu==0.7.5
  Could not find a version that satisfies the requirement pygpu==0.7.5 (from versions: )
No matching distribution found for pygpu==0.7.5

//已保证pip是最新的。

#尝试将其拷贝到我的用户目录下
conda create -n my_root --clone="/data_d/public/miniconda2"
#激活环境
source activate my_root
#查看环境信息
 conda info -e
#激活环境后再次尝试如下,仍旧失败
conda install pygpu
#使用如下命令之后
sudo chown -R $USER:$USER ~/.conda/
#尝试安装,仍失败
conda install pygpu

一些命令:

# 切换至对应环境安装包
activate env_nameconda install pandas
#指定环境参数进行安装
conda install -n env_name pandas 
# 查看已经安装的包
conda list
# 指定查看某环境下安装的package
conda list -n env_name
--------------------- 
作者:cathar 
来源:CSDN 
原文:https://blog.csdn.net/cathar/article/details/53729007 
版权声明:本文为博主原创文章,转载请附上博文链接!
#尝试更改文件所有者
chown  username 6846611e.json
#报错:
chown: 正在更改6846611e.json 的所有者: 不允许的操作
#继续尝试sudo,成功
sudo chown  username 6846611e.json
#再次尝试,失败
conda install pygpu
 with open(cache_path, w) as fo:
    IOError: [Errno 13] Permission denied: u/data_d/public/miniconda2/pkgs/cache/809318c1.json

是否是所有相关的json文件所有者都要变成当前用户?尝试一下。

#使用此命令,将cache文件夹下所有json及q等文件的所有者变为当前用户
sudo chown username cache -R

终于成功了!!!

The following NEW packages will be INSTALLED:

    libgcc-ng:   8.2.0-hdf63c60_1    
    libgpuarray: 0.7.6-h14c3975_0    
    mako:        1.0.9-py27_0        
    markupsafe:  1.1.1-py27h7b6447c_0
    pygpu:       0.7.6-py27h035aef0_0

Proceed ([y]/n)? y

libgpuarray-0. 100% |###################################################| Time: 0:00:01 223.06 kB/s
markupsafe-1.1 100% |###################################################| Time: 0:00:00 139.96 kB/s
mako-1.0.9-py2 100% |###################################################| Time: 0:00:00 176.48 kB/s
pygpu-0.7.6-py 100% |###################################################| Time: 0:00:01 428.93 kB/s
#python下import
>>> import pygpu
>>> pygpu.__version__
u0.7.6

>>> pygpu.__path__
[‘/data_d/old_home/home/username/.conda/envs/my_root/lib/python2.7/site-packages/pygpu‘]

对应的python版本是2.7,theno版本是1.0.

2.Theano使用GPU

#import theano报错:
RuntimeError: 
Could not import mkl.  If you are using conda, update the numpy
packages to the latest build otherwise, set MKL_THREADING_LAYER=GNU in
your environment for MKL 2018.

遂更新numpy

conda update numpy
#显示如下:
The following packages will be UPDATED:

    mkl:            2017.0.3-0            https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free --> 2019.3-199           
    numpy:          1.13.1-py27_0         https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free --> 1.16.3-py27h7e9f1db_0

Proceed ([y]/n)? y

尝试使用pip更新

pip install --upgrade theano

尝试:

THEANO_FLAGS=mode=FAST_RUN,device=cuda,floatX=float32 python test_gpu.py
#输出:
/.conda/envs/my_root/lib/python2.7/site-packages/theano/gpuarray/dnn.py:184: UserWarning: Your cuDNN version is more recent than Theano. If you encounter problems, try updating Theano or downgrading cuDNN to a version >= v5 and <= v7.
  warnings.warn("Your cuDNN version is more recent than "
Using cuDNN version 7201 on context None
Mapped name None to device cuda: GeForce GTX 1080 Ti (0000:03:00.0)#映射到了这个显卡
[GpuElemwise{exp,no_inplace}(<GpuArrayType<None>(float32, vector)>), HostFromGpu(gpuarray)(GpuElemwise{exp,no_inplace}.0)]
Looping 1000 times took 0.221133 seconds
Result is [1.2317803 1.6187935 1.5227807 ... 2.2077181 2.2996776 1.623233 ]
Used the cpu
#最后居然是使用CPU

若不使用GPU:

python test_gpu.py
#输出:
[Elemwise{exp,no_inplace}(<TensorType(float64, vector)>)]
Looping 1000 times took 31.611282 seconds
Result is [1.23178032 1.61879341 1.52278065 ... 2.20771815 2.29967753 1.62323285]
Used the cpu

//可见时间差距为143倍。。。

尝试:

THEANO_FLAGS=mode=FAST_RUN,device=gpu1,floatX=float32 python test_gpu.py

报错:

  File /.conda/envs/my_root/lib/python2.7/site-packages/theano/configdefaults.py", line 116, in filter
    You are tring to use the old GPU back-end. 
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.

 

以上是关于theano使用GPU踩坑的主要内容,如果未能解决你的问题,请参考以下文章

GPU 在执行 Tensorflow 或 Theano 代码期间丢失

Theano-gpu坑:ERROR (theano.gpuarray): Could not initialize pygpu, support disabled

让 Theano 使用 GPU

paper 167:GPU的使用Theano之tutorial

在 AWS g2 上的 Ubuntu 14.04 上使用带 GPU 的 Theano

如何让 Theano 使用 MacBook Air 中的集成 GPU?