安装TensorFlow-gpu
本文介绍的是安装CUDA9.0和TensorFlow1.8,当然,如果你想安装更高版本的,可以仿照本文思路来安装,只是版本不同,思路是一样的。
可以从下面这个网址查看TensorFlow与CUDA的版本对应情况
一、安装CUDA
最新版本的CUDA Tookit()
1.从下载runfile(local)格式的包
2.安装 CUDA
chmod +x cuda_9.0.176_384.81_linux.run sudo sh sh ./cuda_9.0.176_384.81_linux.run
询问是否需要添加驱动时,选择no
Do you accept the previously read EULA?accept/decline/quit: acceptInstall NVIDIA Accelerated Graphics Driver for Linux-x86_64 410.48?(y)es/(n)o/(q)uit: nInstall the CUDA 9.0 Toolkit?(y)es/(n)o/(q)uit: yEnter Toolkit Location [ default is /usr/local/cuda-9.0 ]: Do you want to install a symbolic link at /usr/local/cuda?(y)es/(n)o/(q)uit: yInstall the CUDA 9.0 Samples?(y)es/(n)o/(q)uit: Install the CUDA 9.0 Samples?(y)es/(n)o/(q)uit: yEnter CUDA Samples Location [ default is /home/jason ]:
安装完成后
Installing the CUDA Toolkit in /usr/local/cuda-9.0 ... Installing the CUDA Samples in /home/jason ...Copying samples to /home/jason/NVIDIA_CUDA-9.0_Samples now...Finished copying samples.============ Summary ============Driver: Not SelectedToolkit: Installed in /usr/local/cuda-9.0Samples: Installed in /home/jasonPlease make sure that - PATH includes /usr/local/cuda-9.0/bin - LD_LIBRARY_PATH includes /usr/local/cuda-9.0/lib64, or, add /usr/local/cuda-9.0/lib64 to /etc/ld.so.conf and run ldconfig as rootTo uninstall the CUDA Toolkit, run the uninstall script in /usr/local/cuda-9.0/binPlease see CUDA_Installation_Guide_Linux.pdf in /usr/local/cuda-9.0/doc/pdf for detailed information on setting up CUDA.***WARNING: Incomplete installation! This installation did not install the CUDA Driver. A driver of version at least 384.00 is required for CUDA 9.0 functionality to work.To install the driver using this installer, run the following command, replacingwith the name of this run file: sudo .run -silent -driverLogfile is /tmp/cuda_install_2813.log
3.将CUDA的安装目录添加到path
cd ~sudo gedit .bashrc
在最后面添加,对于不同的版本只要改改cuda的版本就行了
export LD_LIBRARY_PATH=/usr/local/cuda-9.0/lib64:/usr/local/cuda/extras/CPUTI/lib64export CUDA_HOME=/usr/local/cuda-9.0/binexport PATH=$PATH:$LD_LIBRARY_PATH:$CUDA_HOME
4.检查是否安装成功,命令nvcc -V
运行测试用例,当然得你在第1步同意下载smaples才行,其实,通过上一步已经基本确定CUDA安装成功了
cd ~/NVIDIA_CUDA-9.0_Samples/1_Utilities/bandwidthTestmake./bandwidthTest
[CUDA Bandwidth Test] - Starting...Running on... Device 0: GeForce MX150 Quick Mode Host to Device Bandwidth, 1 Device(s) PINNED Memory Transfers Transfer Size (Bytes) Bandwidth(MB/s) 33554432 3035.4 Device to Host Bandwidth, 1 Device(s) PINNED Memory Transfers Transfer Size (Bytes) Bandwidth(MB/s) 33554432 2786.0 Device to Device Bandwidth, 1 Device(s) PINNED Memory Transfers Transfer Size (Bytes) Bandwidth(MB/s) 33554432 33879.5 Result = PASSNOTE: The CUDA Samples are not meant for performance measurements. Results may vary when GPU Boost is enabled.
返回Result = PASS 表示安装成功
二、安装TensorFlow
官方推荐是用安装,不过这里我们仅使用pip进行安装。
我用的是pip3,当然那你也可以用普通的pip,建议用pip3,如果你系统默认Python版本是3的话,pip好像是对应Python2的
先说一下,直接下载当前最新TensorFlow版本的命令pip3 install --upgrade tensorflow-gpu
但考虑到兼容性,还是自己指定一个相对第一点的版本安装吧
需要FQ的方法:pip3 install tensorflow-gpu==1.8
不需要FQ的方法:pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple/ --upgrade tensorflow-gpu
等待结束就安装完成了。
更加详细的安装方法:
三、安装cuDNN
从下载对应版本cuDNN,注意一定要和CUDA相对应,下载cuDNN Library for Linux
解压
sudo tar -zxvf cudnn-9.0-linux-x64-v7.5.1.10.tgz
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*
至此,cuDNN安装完成
四、测试
打开终端,进入Python环境,输入一下代码进行测试
import tensorflow as tfhello = tf.constant('hello,tensorflow')sess = tf.Session() # 输完这句,也会输出一些东西,你可以看看有没有GPU字样来确定是否通过GPU运行的TensorFlowprint(sess.run(hello))
成功会输出b'hello,tensorflow'
卸载TensorFlow和CUDA以及cuDNN
卸载TensorFlow 卸载CUDA以及cuDNN