最近在学习深度学习的相关内容,照着http://deeplearning.net/tutorial/中每个章节的例子一个个对着代码做。并且将部分代码重构了,代码地址在https://github.com/wangzhics/deeplearning里面了。
deeplearning里面的代码是基于http://deeplearning.net/software/theano/tutorial/index.html#tutorial的,theano中我们可以定义变量,使用其内置的公式并组合,而不用关心具体的实现过程,能够加快我们的学习速度。theano中的代码可以在GPU上跑,前面为了快速上手学习算法,一直没有折腾这个,这两天花了些时间弄好了。
其实官方文档http://deeplearning.net/software/theano/install_windows.html#gpu-windows已经比较全了,但是由于其是基于比较旧的软件版本进行介绍的,在用新版本时难免会遇到一些问题,下面是过程的一些记录
conda install mingw libpython来安装mingw和libpython,当使用python3.x的Anaconda时只能正常安装mingw,libpython并不能安装成功,会出现如下的错误
C:\Users\WangZ>conda install libpython
Using Anaconda Cloud api site https://api.anaconda.org
Fetching package metadata: ....
Solving package specifications: ....
The following specifications were found to be in conflict:
- conda -> conda-env
- conda -> menuinst
- conda -> pycosat
- conda -> python 2.7*|3.3*|3.4*
- conda -> pyyaml
- conda -> requests
- conda-env (target=conda-env-2.4.5-py35_0.tar.bz2) -> python 2.7*|3.3*|3.4*|3.5*
- libpython -> python 2.6*|2.7*|3.3*|3.4*
- menuinst (target=menuinst-1.3.2-py35_0.tar.bz2) -> python 2.6*|2.7*|3.3*|3.4*|3.5*
- pip (target=pip-8.1.1-py35_1.tar.bz2) -> python 2.7*|3.3*|3.4*
- pycosat (target=pycosat-0.6.1-py35_0.tar.bz2) -> python 2.6*|2.7*|3.3*|3.4*|3.5*
- python 3.5*
- pyyaml (target=pyyaml-3.11-py35_3.tar.bz2) -> python 2.6*|2.7*|3.3*|3.4*
- requests (target=requests-2.9.1-py35_0.tar.bz2) -> python 2.6*|2.7*|3.3*|3.4*|3.5*
- setuptools (target=setuptools-20.3-py35_0.tar.bz2) -> python 2.6*|2.7*|3.3*|3.4*|3.5*
- wheel (target=wheel-0.29.0-py35_0.tar.bz2) -> python 2.7*|3.3*|3.4*|3.5*
Use "conda info " to see the dependencies for each package.
这是因为libpython只支持到python3.4.x,而Anaconda3默认的环境是python3.5.1
C:\Users\WangZ>conda info libpython
Using Anaconda Cloud api site https://api.anaconda.org
Fetching package metadata: ....
libpython 1.0 py26_0
--------------------
file name : libpython-1.0-py26_0.tar.bz2
name : libpython
version : 1.0
build number: 0
build string: py26_0
channel : defaults
size : 29 KB
date : 2014-08-22
license_family: Other
md5 : 11614b71090b7ad155e033c14f8216db
installed environments:
dependencies:
mingw 4.7
python 2.6*
...
libpython 1.0 py34_1
--------------------
file name : libpython-1.0-py34_1.tar.bz2
name : libpython
version : 1.0
build number: 1
build string: py34_1
channel : defaults
size : 88 KB
date : 2014-08-22
license_family: Other
md5 : b13c6d4f28d81e05f85cb50e0f6b63eb
installed environments:
dependencies:
mingw 4.7
python 3.4*
那咋办呢?Anaconda很强大我们可以创建指定版本的python环境,执行如下命令:
C:\Users\WangZ>cd /d E:\Anaconda3\Scripts
E:\Anaconda3\Scripts>conda create -n python34 python=3.4.4
Using Anaconda Cloud api site https://api.anaconda.org
Fetching package metadata: ....
Solving package specifications: .........
Package plan for installation in environment E:\Anaconda3\envs\python34:
The following packages will be downloaded:
package | build
---------------------------|-----------------
vs2010_runtime-10.00.40219.1| 0 1.1 MB
python-3.4.4 | 4 31.7 MB
setuptools-20.7.0 | py34_0 767 KB
wheel-0.29.0 | py34_0 123 KB
pip-8.1.1 | py34_1 1.6 MB
------------------------------------------------------------
Total: 35.3 MB
The following NEW packages will be INSTALLED:
pip: 8.1.1-py34_1
python: 3.4.4-4
setuptools: 20.7.0-py34_0
vs2010_runtime: 10.00.40219.1-0
wheel: 0.29.0-py34_0
Proceed ([y]/n)? y
Fetching packages ...
vs2010_runtime 100% |###############################| Time: 0:00:09 121.08 kB/s
python-3.4.4-4 100% |###############################| Time: 0:02:51 194.47 kB/s
setuptools-20. 100% |###############################| Time: 0:00:05 134.58 kB/s
wheel-0.29.0-p 100% |###############################| Time: 0:00:01 81.65 kB/s
pip-8.1.1-py34 100% |###############################| Time: 0:00:17 99.02 kB/s
Extracting packages ...
[ COMPLETE ]|##################################################| 100%
Linking packages ...
[ COMPLETE ]|##################################################| 100%
#
# To activate this environment, use:
# > activate python34
#
E:\Anaconda3\Scripts>
该指令会在env下创建一个python3.4.4的环境,文件名就是-n的参数。
E:\Anaconda3\Scripts>conda install libpython -n python34
Using Anaconda Cloud api site https://api.anaconda.org
Fetching package metadata: ....
Solving package specifications: .........
Package plan for installation in environment E:\Anaconda3\envs\python34:
The following packages will be downloaded:
package | build
---------------------------|-----------------
mingw-4.7 | 1 56.1 MB
libpython-1.0 | py34_1 88 KB
------------------------------------------------------------
Total: 56.2 MB
The following NEW packages will be INSTALLED:
libpython: 1.0-py34_1
mingw: 4.7-1
Proceed ([y]/n)? y
Fetching packages ...
mingw-4.7-1.ta 100% |###############################| Time: 0:04:28 218.86 kB/s
libpython-1.0- 100% |###############################| Time: 0:00:01 72.85 kB/s
Extracting packages ...
[ COMPLETE ]|##################################################| 100%
Linking packages ...
[ COMPLETE ]|##################################################| 100%
E:\Anaconda3\Scripts>
E:\Anaconda3\Scripts>cd E:\Anaconda3\envs\python34\Scripts
E:\Anaconda3\envs\python34\Scripts>pip install theano
Collecting theano
Collecting scipy>=0.11 (from theano)
Using cached scipy-0.17.0.tar.gz
Complete output from command python setup.py egg_info:
non-existing path in 'numpy\\distutils': 'site.cfg'
Running from numpy source directory.
C:\Users\WangZ\AppData\Local\Temp\easy_install-z7doyvws\numpy-1.11.0\setup.py:327: UserWarning: Unrecognized setuptools command, proceeding with generating Cython sources and expanding templates
warnings.warn("Unrecognized setuptools command, proceeding with "
File "E:\Anaconda3\envs\python34\lib\site-packages\setuptools-20.7.0-py3.4.egg\pkg_resources\__init__.py", line 1104, in obtain
File "E:\Anaconda3\envs\python34\lib\site-packages\setuptools-20.7.0-py3.4.egg\setuptools\dist.py", line 380, in fetch_build_egg
File "E:\Anaconda3\envs\python34\lib\site-packages\setuptools-20.7.0-py3.4.egg\setuptools\command\easy_install.py", line 640, in easy_install
File "E:\Anaconda3\envs\python34\lib\site-packages\setuptools-20.7.0-py3.4.egg\setuptools\command\easy_install.py", line 670, in install_item
File "E:\Anaconda3\envs\python34\lib\site-packages\setuptools-20.7.0-py3.4.egg\setuptools\command\easy_install.py", line 850, in install_eggs
File "E:\Anaconda3\envs\python34\lib\site-packages\setuptools-20.7.0-py3.4.egg\setuptools\command\easy_install.py", line 1078, in build_and_install
File "E:\Anaconda3\envs\python34\lib\site-packages\setuptools-20.7.0-py3.4.egg\setuptools\command\easy_install.py", line 1066, in run_setup
distutils.errors.DistutilsError: Setup script exited with error: Microsoft Visual C++ 10.0 is required (Unable to find vcvarsall.bat).
----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in C:\Users\WangZ\AppData\Local\Temp\pip-build-y5922frd\scipy\
E:\Anaconda3\envs\python34\Scripts>
E:\Anaconda3\Scripts>conda install scipy -n python34
Using Anaconda Cloud api site https://api.anaconda.org
Fetching package metadata: ....
Solving package specifications: .........
Package plan for installation in environment E:\Anaconda3\envs\python34:
The following packages will be downloaded:
package | build
---------------------------|-----------------
numpy-1.11.0 | py34_0 2.7 MB
scipy-0.17.0 | np111py34_1 10.3 MB
------------------------------------------------------------
Total: 13.0 MB
The following NEW packages will be INSTALLED:
mkl: 11.3.1-0
numpy: 1.11.0-py34_0
scipy: 0.17.0-np111py34_1
Proceed ([y]/n)? y
Fetching packages ...
numpy-1.11.0-p 100% |###############################| Time: 0:00:18 147.81 kB/s
scipy-0.17.0-n 100% |###############################| Time: 0:01:06 162.73 kB/s
Extracting packages ...
[ COMPLETE ]|##################################################| 100%
Linking packages ...
[ COMPLETE ]|##################################################| 100%
E:\Anaconda3\Scripts>
E:\Anaconda3\envs\python34\Scripts>pip install theano
Collecting theano
Requirement already satisfied (use --upgrade to upgrade): numpy>=1.7.1 in e:\anaconda3\envs\python34\lib\site-packages (from theano)
Collecting six>=1.9.0 (from theano)
Using cached six-1.10.0-py2.py3-none-any.whl
Requirement already satisfied (use --upgrade to upgrade): scipy>=0.11 in e:\anaconda3\envs\python34\lib\site-packages (from theano)
Installing collected packages: six, theano
Successfully installed six-1.10.0 theano-0.8.2
E:\Anaconda3\envs\python34\Scripts>
[global]
floatX = float32
device = gpu
[nvcc]
flags=-LE:\Anaconda3\envs\python34\libs
compiler_bindir=C:/Program Files (x86)/Microsoft Visual Studio 12.0/VC/bin
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')
直接运行,会报错
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xd5 in position 11: invalid continuation byte
这是因为掉用nvcc返回的字符串是使用的cwindows默认的默认编码,把源码出错的地方改为 *.decode("GBK")就可以,当时改源码忘记截图了...
E:\Anaconda3\envs\python34\python.exe E:/ML/deeplearning/test_gpu.py
Using gpu device 0: GeForce GT 750M (CNMeM is disabled, cuDNN not available)
DEBUG: nvcc STDOUT mod.cu
正在创建库 C:/Users/WangZ/AppData/Local/Theano/compiledir_Windows-10-10.0.10586-Intel64_Family_6_Model_58_Stepping_9_GenuineIntel-3.4.4-64/tmp313wmfq0/meb404c8cd39208f6884dd773b584b7d7.lib 和对象 C:/Users/WangZ/AppData/Local/Theano/compiledir_Windows-10-10.0.10586-Intel64_Family_6_Model_58_Stepping_9_GenuineIntel-3.4.4-64/tmp313wmfq0/meb404c8cd39208f6884dd773b584b7d7.exp
DEBUG: nvcc STDOUT mod.cu
正在创建库 C:/Users/WangZ/AppData/Local/Theano/compiledir_Windows-10-10.0.10586-Intel64_Family_6_Model_58_Stepping_9_GenuineIntel-3.4.4-64/tmpk8he1yq1/md9b7b5ef5d727b2e8e3d2cc34bb9d0eb.lib 和对象 C:/Users/WangZ/AppData/Local/Theano/compiledir_Windows-10-10.0.10586-Intel64_Family_6_Model_58_Stepping_9_GenuineIntel-3.4.4-64/tmpk8he1yq1/md9b7b5ef5d727b2e8e3d2cc34bb9d0eb.exp
DEBUG: nvcc STDOUT mod.cu
正在创建库 C:/Users/WangZ/AppData/Local/Theano/compiledir_Windows-10-10.0.10586-Intel64_Family_6_Model_58_Stepping_9_GenuineIntel-3.4.4-64/tmp3mv9lkx8/mfeb646abb02bed7115bdc204940fcfff.lib 和对象 C:/Users/WangZ/AppData/Local/Theano/compiledir_Windows-10-10.0.10586-Intel64_Family_6_Model_58_Stepping_9_GenuineIntel-3.4.4-64/tmp3mv9lkx8/mfeb646abb02bed7115bdc204940fcfff.exp
[GpuElemwise{exp,no_inplace}(), HostFromGpu(GpuElemwise{exp,no_inplace}.0)]
Looping 1000 times took 1.319867 seconds
Result is [ 1.23178029 1.61879349 1.52278066 ..., 2.20771813 2.29967761
1.62323296]
Used the gpu
BINGO!
C:\Users\WangZ\AppData\Local\Temp\ccU0pNP5.o: In function `_import_array':
E:/Anaconda3/lib/site-packages/numpy/core/include/numpy/__multiarray_api.h:1641: undefined reference to `__imp_PyExc_ImportError'
E:/Anaconda3/lib/site-packages/numpy/core/include/numpy/__multiarray_api.h:1647: undefined reference to `__imp_PyExc_AttributeError'
E:/Anaconda3/lib/site-packages/numpy/core/include/numpy/__multiarray_api.h:1652: undefined reference to `__imp_PyCapsule_Type'
E:/Anaconda3/lib/site-packages/numpy/core/include/numpy/__multiarray_api.h:1653: undefined reference to `__imp_PyExc_RuntimeError'
b) 网上有人在.theanorc.txt里面配置了gcc,其实并不需要,因为在Anaconda的环境变量里面已经有了