安装TensorFlow-gpu
Posted youpeng
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了安装TensorFlow-gpu相关的知识,希望对你有一定的参考价值。
安装TensorFlow-gpu
本文介绍的是安装CUDA9.0和TensorFlow1.8,当然,如果你想安装更高版本的,可以仿照本文思路来安装,只是版本不同,思路是一样的。
可以从下面这个网址查看TensorFlow与CUDA的版本对应情况
https://tensorflow.google.cn/install/source
一、安装CUDA
最新版本的CUDA Tookit(https://developer.nvidia.com/accelerated-computing-toolkit)
1.从‘>CUDA9.0下载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: accept
Install NVIDIA Accelerated Graphics Driver for Linux-x86_64 410.48?
(y)es/(n)o/(q)uit: n
Install the CUDA 9.0 Toolkit?
(y)es/(n)o/(q)uit: y
Enter 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: y
Install the CUDA 9.0 Samples?
(y)es/(n)o/(q)uit:
Install the CUDA 9.0 Samples?
(y)es/(n)o/(q)uit: y
Enter 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 Selected
Toolkit: Installed in /usr/local/cuda-9.0
Samples: Installed in /home/jason
Please 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 root
To uninstall the CUDA Toolkit, run the uninstall script in /usr/local/cuda-9.0/bin
Please 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, replacing <CudaInstaller> with the name of this run file:
sudo <CudaInstaller>.run -silent -driver
Logfile 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/lib64
export CUDA_HOME=/usr/local/cuda-9.0/bin
export PATH=$PATH:$LD_LIBRARY_PATH:$CUDA_HOME
4.检查是否安装成功,命令nvcc -V
运行测试用例,当然得你在第1步同意下载smaples才行,其实,通过上一步已经基本确定CUDA安装成功了
cd ~/NVIDIA_CUDA-9.0_Samples/1_Utilities/bandwidthTest
make
./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 = PASS
NOTE: The CUDA Samples are not meant for performance measurements. Results may vary when GPU Boost is enabled.
返回Result = PASS 表示安装成功
二、安装TensorFlow
官方推荐是用Virtualenv安装,不过这里我们仅使用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 archive下载对应版本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 tf
hello = tf.constant('hello,tensorflow')
sess = tf.Session() # 输完这句,也会输出一些东西,你可以看看有没有GPU字样来确定是否通过GPU运行的TensorFlow
print(sess.run(hello))
成功会输出b‘hello,tensorflow‘
以上是关于安装TensorFlow-gpu的主要内容,如果未能解决你的问题,请参考以下文章