标签归档:Theano

从零开始搭建深度学习服务器: 深度学习工具安装(Theano + MXNet)

Deep Learning Specialization on Coursera

这个系列写了好几篇文章,这是相关文章的索引,仅供参考:

以下是相关深度学习工具包的安装,包括Theano, MXNet

4. Theano

Theano虽然官宣不在更新,但是它的价值依然很大,很多早期深度学习工具的底层依然依赖的是它。在Ubuntu下安装Theano有两种模式,一种是通过Conda安装,Theano的官方安装文档给得是这个方式;另外一种是pip安装模式,官方文档没有给出很好的描述,我参考了网上其他的文章,安装过程中遇到了几个小问题,不过顺利解决。首先安装相关的依赖:

sudo apt-get install python-numpy python-scipy python-dev python-pip python-nose g++ libopenblas-dev git

这个时候可以先尝试用pip的方式安装Theano:

pip install Theano

测试时会遇到类似找不到pygpu模块的提示,而这个模块,是无法用pip安装的,必须通过Theano提供的libgpuarray编译,官方安装文档也给了专门的说明

git clone https://github.com/Theano/libgpuarray.git
cd libgpuarray/
mkdir Build
cd Build/
cmake .. -DCMAKE_BUILD_TYPE=Release
make
sudo make install
cd ..
sudo pip install Cython(如果提示cython没有安装需要先安装Cython)
sudo python setup.py build
sudo python setup.py install
sudo ldconfig

还有最后一步,配置文件

vim ~/.theanorc

[global]
floatX=float32
device=cuda
[cuda]
root=/usr/local/cuda
[nvcc]
flags=-D_FORCE_INLINES

然后可以试一下在ipython中导入Theano是否ok:

Python 2.7.13 (default, Jan 19 2017, 14:48:08) 
Type "copyright", "credits" or "license" for more information.
 
IPython 5.1.0 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.
 
In [1]: import theano
Using cuDNN version 6021 on context None
Mapped name None to device cuda: GeForce GTX 1080 Ti (0000:05:00.0)

5. MXNet

MXNet的安装还是比较方便的,按照MXNet官方的安装指南,我是在Ubuntu17.04的环境下用virtualenv安装的:

Python2.x的安装方式如下:

如果没有安装python环境和virtualenv,可以先安装:
sudo apt-get update
sudo apt-get install -y python-dev python-virtualenv

然后用virtualenv生成MXNet的虚拟环境:
virtualenv --system-site-packages venv
source venv/bin/activate

要升级pip到最新版(不清楚是为什么):
pip install --upgrade pip

目前MXNet的最新版是1.0:
pip install mxnet-cu80==1.0.0

如果需要可视化训练过程,则可以选择安装graphviz:
sudo apt-get install graphviz
pip install graphviz

最后测试一下MXNet在GPU环境下是否生效:

Python 2.7.13 (default, Nov 23 2017, 15:37:09) 
[GCC 6.3.0 20170406] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import mxnet as mx
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/textminer/mxnet/venv/local/lib/python2.7/site-packages/mxnet/__init__.py", line 25, in <module>
    from . import engine
  File "/home/textminer/mxnet/venv/local/lib/python2.7/site-packages/mxnet/engine.py", line 23, in <module>
    from .base import _LIB, check_call
  File "/home/textminer/mxnet/venv/local/lib/python2.7/site-packages/mxnet/base.py", line 111, in <module>
    _LIB = _load_lib()
  File "/home/textminer/mxnet/venv/local/lib/python2.7/site-packages/mxnet/base.py", line 103, in _load_lib
    lib = ctypes.CDLL(lib_path[0], ctypes.RTLD_LOCAL)
  File "/usr/lib/python2.7/ctypes/__init__.py", line 362, in __init__
    self._handle = _dlopen(self._name, mode)
OSError: libgfortran.so.3: cannot open shared object file: No such file or directory

报了如上libgfortran.so.3的错误,google了一下,需要安装gfortran:

sudo apt-get install gfortran

再次测试,就没有问题了:

(venv) textminer@textminer:~/mxnet$ python
Python 2.7.13 (default, Nov 23 2017, 15:37:09) 
[GCC 6.3.0 20170406] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import mxnet as mx
>>> a = mx.nd.ones((2,3), mx.gpu())
>>> b = a * 2 + 1
>>> b.asnumpy()
array([[ 3.,  3.,  3.],
       [ 3.,  3.,  3.]], dtype=float32)

Python3.x下的安装基本上过程相同。

注:原创文章,转载请注明出处及保留链接“我爱自然语言处理”:http://www.52nlp.cn

本文链接地址:从零开始搭建深度学习服务器: 深度学习工具安装(Theano + MXNet) http://www.52nlp.cn/?p=10058

深度学习主机环境配置: Ubuntu16.04+GeForce GTX 1080+TensorFlow

Deep Learning Specialization on Coursera

Update: 文章写于一年前,有些地方已经不适合了,最近升级了一下深度学习服务器,同时配置了一下环境,新写了文章,可以同时参考: 从零开始搭建深度学习服务器: 基础环境配置(Ubuntu + GTX 1080 TI + CUDA + cuDNN) 从零开始搭建深度学习服务器: 深度学习工具安装(TensorFlow + PyTorch + Torch)

这个系列写了好几篇文章,这是相关文章的索引,仅供参考:

接上文《深度学习主机环境配置: Ubuntu16.04+Nvidia GTX 1080+CUDA8.0》,我们继续来安装 TensorFlow,使其支持GeForce GTX 1080显卡。

1 下载和安装cuDNN

cuDNN全称 CUDA Deep Neural Network library,是NVIDIA专门针对深度神经网络设计的一套GPU计算加速库,被广泛用于各种深度学习框架,例如Caffe, TensorFlow, Theano, Torch, CNTK等。

The NVIDIA CUDA® Deep Neural Network library (cuDNN) is a GPU-accelerated library of primitives for deep neural networks. cuDNN provides highly tuned implementations for standard routines such as forward and backward convolution, pooling, normalization, and activation layers. cuDNN is part of the NVIDIA Deep Learning SDK.

Deep learning researchers and framework developers worldwide rely on cuDNN for high-performance GPU acceleration. It allows them to focus on training neural networks and developing software applications rather than spending time on low-level GPU performance tuning. cuDNN accelerates widely used deep learning frameworks, including Caffe, TensorFlow, Theano, Torch, and CNTK. See supported frameworks for more details.

首先需要下载cuDNN,直接从Nvidia官方下载链接选择一个版本,不过下载cuDNN前同样需要登录甚至填写一个简单的调查问卷: https://developer.nvidia.com/rdp/cudnn-download,这里选择的是支持CUDA8.0的cuDNN v5版本,而支持CUDA8的5.1版本虽然显示在下载选择项里,但是提示:cuDNN 5.1 RC for CUDA 8RC will be available soon - please check back again.

屏幕快照 2016-07-17 上午11.17.39

安装cuDNN比较简单,解压后把相应的文件拷贝到对应的CUDA目录下即可:

tar -zxvf cudnn-8.0-linux-x64-v5.0-ga.tgz

cuda/include/cudnn.h
cuda/lib64/libcudnn.so
cuda/lib64/libcudnn.so.5
cuda/lib64/libcudnn.so.5.0.5
cuda/lib64/libcudnn_static.a

sudo cp cuda/include/cudnn.h /usr/local/cuda/include/
sudo cp cuda/lib64/libcudnn* /usr/local/cuda/lib64/
sudo chmod a+r /usr/local/cuda/include/cudnn.h
sudo chmod a+r /usr/local/cuda/lib64/libcudnn*

继续阅读