关键词:theano安装,搭建theano环境, python, 深度学习
因为需要安装theano,结果发现这又是一个难以安装的python包…虽然网上教程不少,然而鱼龙混杂,试验了各种方法流程,最后总算是弄好了,现在把我的过程总结如下:
conda install mingw libpython
pip install theano
D:\Anaconda;D:\Anaconda\Scripts;
D:\Anaconda\Lib\site-packages\theano;
[global]
openmp=False
[blas]
ldflags =
[gcc]
cxxflags = -ID:\Anaconda\MinGW\include
下面测试theano是否安装成功:
import theano
print theano.config.blas.ldflags
没有出错(没有返回值)则说明已经配置成功。
其实单单是import theano不报错就已经谢天谢地了。
import theano
theano.test()
例如我这里提示没有nose-parameterized这个模块,安装方法:
pip install nose-parameterized
例如会提示PyCUDA的相关错误信息等。
注意:测试2必须在cpu下运行,在下面的GPU搭建中,如果配置了theano的device = gpu,则测试2就不能运行了。
import numpy
id(numpy.dot) == id(numpy.core.multiarray.dot)
如果结果为False,表示成功依赖了BLAS加速,如果是TRUE则表示用的是python自己的实现,并没有加速。(我这里总是显示TRUE,暂时不知道为什么,但是前面的测试又表明theano已经安装成功)
上面的theano配置只是完成了上半部分,这个时候还不能进行gpu加速。如果使用GPU则需要继续以下步骤:
[nvcc]
fastmath = True
flags = -LD:\Anaconda\libs
compiler_bindir = D:\Microsoft Visual Studio 2013\VC\bin
[global]
device = gpu
floatX = float32
最终的Theano配置文件内容为:
[global]
device = gpu
floatX = float32
openmp=False
[blas]
ldflags =
[gcc]
cxxflags = -ID:\Anaconda\MinGW\include
[nvcc]
flags = -LD:\Anaconda\libs
compiler_bindir = D:\Microsoft Visual Studio 2013\VC\bin
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
功能:测试是否使用GPU
时间:2016年6月10日 11:20:10
"""
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' % iters, t1 - t0, 'seconds')
print('Result is', 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')
使用CPU的测试结果:17.28秒
切换成cpu的方法,我是通过更改theano配置文件,将.theanorc.txt的内容只保留以下内容(可能需要重启电脑):
[blas]
ldflags =
[gcc]
cxxflags = -ID:\Anaconda\MinGW\include