感谢支持
我们一直在努力

Ubuntu 16.04+CUDA8.0+Theano深度学习环境搭建

详细分享在Ubuntu 16.04下搭建CUDA8.0+Theano深度学习环境的步骤,希望对大家有所帮助。
一、安装nvidia驱动

1、首先去官网上查看适合你GPU的驱动(http://www.nvidia.com/Download/index.aspx?lang=en-us),根据自己买的型号选择下载,放到/home/isi/cuda目录下面,我下载的是NVIDIA-Linux-x86_64-375.39.run

2、屏蔽开源驱动 nouveau(原文章说“可以不编辑blacklist.conf,NVIDIA.run驱动程序可以自己停止其他开源驱动,但是需要重启”)

sudo gedit /etc/modprobe.d/blacklist.conf

在文件末尾添加如下内容

blacklist vga16fb

blacklist nouveau

blacklist rivafb

blacklist nvidiafb

blacklist rivatv

(这里有一行空格)

3、删除旧NVIDIA驱动

sudo apt-get –purge remove nvidia-*(需要清除干净)

sudo apt-get –purge remove xserver-xorg-video-nouveau

4、重启电脑

sudo reboot

5、按Ctrl + Alt +F1(F1~F6均可)到x-server(tty), Ctrl+Alt+F7是返回

6、关闭图形环境,否则驱动无法正常安装

输入用户名密码登录,然后键入命令后执行(数字不能用小键盘输入)

sudo service lightdm stop

成功关闭服务后将出现:lightdm stop/waiting

7、进入驱动所在的文件夹,这里就是/home/isi/cuda,执行

sudo sh NVIDIA*.run

后面就一路Accept就可以~报错The distribution-provided pre-install script failed!不必理会,继续安装。最重要的一步,安装程序问你是否使用nv的xconfig文件,这里一点要选yes,否则在启动x-window时不会使用nv驱动。

8、重启 X-window 服务,并重启电脑

sudo service lightdm start

sudo reboot

9、查看显卡是否装好

方法1:运行下边代码,假如显示”direct rendering: Yes“,则已安装

glxinfo | grep rendering

方法2:运行下边代码,出现GPU列表,则说明驱动安装成功了

nvidia-smi

二、安装cuda

Ubuntu 15.04 下Caffe + + CUDA 7.0 安装配置指南  http://www.linuxidc.com/Linux/2016-11/137497.htm

Ubuntu 16.04系统下CUDA7.5配置Caffe教程 http://www.linuxidc.com/Linux/2016-07/132859.htm

Ubuntu 16.04+CUDA7.5+Caffe深度学习环境搭建  http://www.linuxidc.com/Linux/2017-10/147612.htm

1、从这里下载cuda的安装文件

https://developer.nvidia.com/cuda-release-candidate-download


 

注意这里下载的是cuda8.0的runfile(local)文件,我放在了/home/isi/cuda文件夹下

2、进入到/home/isi/cuda文件夹,进行安装

cd /home/isi/cuda

sudo sh cuda_8.0.27_linux.run

执行后会有一系列提示让你确认,但是注意,有个让你选择是否安装nvidia361驱动时,一定要选择否,因为前面我们已经安装了更加新的nvidia367,所以这里不要选择安装。其余的都直接默认或者选择是即可。

3、安装成功后会出现如下界面:

===========

= Summary =

===========

Driver:NotSelected

Toolkit: Installedin/usr/local/cuda-8.0

Samples: Installedin/home/textminer

Please make sure that

– PATH includes /usr/local/cuda-8.0/bin

– LD_LIBRARY_PATH includes /usr/local/cuda-8.0/lib64,or,add/usr/local/cuda-8.0/lib64to/etc/ld.so.confandrun ldconfigasrootTouninstall the CUDA Toolkit, run the uninstall scriptin/usr/local/cuda-8.0/bin

Please see CUDA_Installation_Guide_Linux.pdfin/usr/local/cuda-8.0/doc/pdffordetailed informationonsetting up CUDA.

***WARNING: Incomplete installation! This installation didnotinstall the CUDA Driver. A driverofversion at least361.00isrequiredforCUDA8.0functionalitytowork.Toinstall the driverusingthis installer, run the following command, replacingwiththe nameofthis run file:

sudo .run -silent -driver

Logfileis/opt/temp//cuda_install_6583.log

4、安装完毕后,再声明一下环境变量,并将其写入到 ~/.bashrc 的尾部:

export PATH=/usr/local/cuda-8.0/bin${PATH:+:${PATH}}

export LD_LIBRARY_PATH=/usr/local/cuda-8.0/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}

5、然后设置环境变量和动态链接库,在命令行输入:

sudo gedit /etc/profile

在打开的文件末尾加入:

exportPATH=/usr/local/cuda/bin:$PATH

6、保存之后,创建链接文件:

sudo gedit /etc/ld.so.conf.d/cuda.conf

在打开的文件中添加如下语句:

/usr/local/cuda/lib64

然后执行下列代码使链接立即生效

sudo ldconfig

更多详情见请继续阅读下一页的精彩内容: http://www.linuxidc.com/Linux/2017-10/147614p2.htm

一、安装anaconda

1、 到官网下载anaconda,根据自己python版本和位数下载对应的版本

 


 

2、打开anaconda下载的文件夹,然后安装anaconda,在终端输入:

cd ~/Downloads

bash Anaconda3-.4.0-Linux-x86_64.sh

然后一路回车和yes

3、在终端输入Python发现依然是gnome自带的python版本,这是因为.bashrc的更新还没有生效,命令行输入:source ~/.bashrc

二、安装Theano

sudo pip install Theano

安装之后默认的是用cpu,这个时候需要修改设置来使用gpu进行计算:

步骤一:ctrl+alt+t打开终端,打开theanorc文件

sudo gedit ~/.theanorc

空白文件中写入:

[global]

floatX=float32

device=cpu

步骤二:修改内容在上边基础上修改

使用gpu加速时,把上边device修改,同时添加新的内容

[global]

floatX=float32

device=gpu

[cuda]

root=/usr/local/cuda-8.0

三、安装Keras

pip install keras

安装之后的默认keras的后端是TensorFlow,所以如果想要切换到Theano的话需要

1、keras后端配置是在文件keras.json 这个文件中进行的,因此找到目录,不存的话会自动创建

sudo gedit ~/.keras/keras.json

2、theano作为backend

{

“image_dim_ordering”:”th”,

“epsilon”:1e-07,

“floatx”:”float32″,

“backend”:”theano”

}

四、程序测试

from theano import function, config, shared, sandbox

import theano.tensor as T

import numpy

import time

vlen=10*30*768 #10*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)forxinf.maker.fgraph.toposort()]):

    print(‘Used the cpu’)

else:

    print(‘Used the gpu’)

当出现以下结果的时候说明安装成功:

runfile(‘/home/isi/.config/spyder/temp.py’, wdir=’/home/isi/.config/spyder’)WARNING (theano.sandbox.cuda): The cuda backend is deprecated and will be removed in the next release (v0.10).  Please switch to the gpuarray backend. You can get more information about how to switch at this URL: https://github.com/Theano/Theano/wiki/Converting-to-the-new-gpu-back-end%28gpuarray%29Using gpu device 0: GeForce GTX 1050 (CNMeM is disabled, cuDNN not available)

或者

runfile(‘/home/isi/.config/spyder/temp.py’, wdir=’/home/isi/.config/spyder’)Reloaded modules: cuda_ndarray, cutils_ext, cuda_ndarray.cuda_ndarray, lazylinker_ext.lazylinker_ext, tmpxIZo3D.8caca893ab41ad4849afd885dc106b92, tmpihAji6.544270fe7a21a748315f83abfe0913cc, lazylinker_ext, tmpxIZo3D, tmpihAji6, cutils_ext.cutils_ext[GpuElemwise{exp,no_inplace}(), HostFromGpu(GpuElemwise{exp,no_inplace}.0)](‘Looping 1000 times took’, 0.26572704315185547, ‘seconds’)(‘Result is’, array([ 1.23178029,  1.61879349,  1.52278066, …,  2.20771813,2.29967761,  1.62323296], dtype=float32))

Used the gpu

本文永久更新链接地址:http://www.linuxidc.com/Linux/2017-10/147614.htm

赞(0) 打赏
转载请注明出处:服务器评测 » Ubuntu 16.04+CUDA8.0+Theano深度学习环境搭建
分享到: 更多 (0)

听说打赏我的人,都进福布斯排行榜啦!

支付宝扫一扫打赏

微信扫一扫打赏