win10+Theano配置,网上的东西100条,也就一条有用

本文详细介绍如何在Windows环境下配置Theano与CUDA的过程,并通过实验验证GPU加速效果,结果显示GPU加速提升近10倍。

重点的重点来了,首先,我要说:theano支持到了cudnn5.1,就停止更新了。维护一年后彻底放弃,所以大部分都是用于实验室做一些toy data。
我是1060的显卡,于是安装了CUDA9和CUDA8,CUDA8是我在配置openpose的时候一起下的。在我用CUDA9配置完以后才知道CUDA9来做根本就不可能,因为支持的CUDNN是7.1.不信可以试试。

我下的Cnaconda3,为了以后配置tensor简单一些(ts在win下只支持3)

然后配置环境。script加到系统变量,以及Cnaconda也加进去。然后cmd执行
conda install numpy scipy mkl-service libpython m2w64-toolchain nose

conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda config --set show_channel_urls yes


conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/msy
这里是再添加镜像,避免不必要的弯路。
conda install numpy scipy mkl-service libpython m2w64-toolchain nose
conda install pygpu theano
theano-cache purge


还有一堆环境变量:

下面图里的上下位置,决定了是用cuda8还是9(对我个人而言)

随后在cmd的根目录加下面这个东西。

主要的问题在于一个.theanorc.txt的问题

[global] 
cxx=E:\AnaConda\AnaConda\Library\mingw-w64\bin\g++.exe
device=cuda 
floatX=float32 
[gpuarray]
preallocate=0.75 
[dnn] 
enabled = True 
include_path=C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v8.0\include
library_path=C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v8.0\lib\x64


就这些,网上的一大堆什么加这个加那个,不敢说不对,至少在我这行不通。
成功了以后是这样:

我们测试一下代码:证明我们配置成功了。
对比一下下面两段代码:


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')
结果为:
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).transfer(None))
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" % (numpy.asarray(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')

我的运行结果:
可以看到0.38和0.041359的差距,大概10倍,看来标压CPU就是快哈,我看官网的是100倍,总之成功。

时间就是生命,请不要浪费生命!
### 安装准备 为了成功在 Windows 10安装 Theano 深度学习库,建议先安装 Anaconda 发行版来简化 Python 和其他依赖项的管理[^2]。 ### 更新 Conda 确保 Conda 是最新版本可以减少后续安装过程中遇到的问题。通过命令提示符执行以下更新操作: ```bash conda update conda ``` ### 创建独立环境 创建一个新的虚拟环境有助于隔离不同项目之间的包冲突。使用如下指令创建并激活新环境: ```bash conda create -n theano_env python=3.8 conda activate theano_env ``` ### 安装必要组件 对于 Windows 用户来说,编译器支持是必需的。可以通过 MinGW 提供 GCC 编译工具链的支持。具体步骤如下所示: ```bash conda install libpython conda install mingw ``` 这些命令会为系统提供必要的构建工具以便于之后顺利安装 Theano 及其依赖关系[^4]。 ### 安装 Theano 库 完成上述准备工作后,可以直接利用 pip 工具来进行 Theano安装工作: ```bash pip install theano ``` 如果在此阶段遇到了 `no module named gof` 错误,则可能是因为缺少某些特定文件或路径设置不正确引起的。此时应该确认所有前置件都已经满足,并且尝试重新启动计算机后再试一次。 另外需要注意的是,在较新的操作系统以及 Python 版本上可能会存在兼容性问题;因此当常规方法无法解决问题时,考虑降级到更稳定的 Python 版本可能是有效的解决策略之一。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值