Ubuntu 16.04 LTS Tensorflow-CPU/Cuda9.0 + Cudnn7.0.5 + Tensorflow-GPU

本文详细介绍了如何在Ubuntu16.04系统上安装TensorFlow-GPU版本,包括验证NVIDIA GPU、安装CUDA Toolkit 9.0、cuDNN v7.0以及设置Virtualenv环境等步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

概述

  • 此教程为Ubuntu 16.04 下 Tensorflow-GPU或者CPU版本的安装
  • 安装前,请务必执行1.1.1的操作,验证您的显卡是NIVIDIA的并且支持GPU计算。如果不支持GPU运算,则只能安装Tensorflow-CPU版本,直接跳过1、2、3大标题,从4. Virtualenv + Tensorflow1.5处开始,并选择安装CPU版本
  • 注意!!安装CUDA和cuDNN时,都不要安装最新版本,一切按照本篇中的版本进行安装。

1. NVIDIA CUDA Toolkit 9.0

1.1 Pre-installation Actions

1.1.1 Verify the system has a CUDA-capable GPU.

type the following at the command line to get the information of your graphics card

$ lspci | grep -i nvidia

If your graphics card is from NVIDIA and it is listed in here, your GPU is CUDA-capable.

1.1.2 Verify the system is running a supported version of Linux.

The CUDA Development Tools are only supported on some specific distributions of Linux. These are listed in the CUDA Toolkit release notes.

To determine which distribution and release number you’re running, type the following at the command line:

$ uname -m && cat /etc/*release

You should see output similar to the following, modified for your particular system:

x86_64
Red Hat Enterprise Linux Workstation release 6.0 (Santiago)

The x86_64 line indicates you are running on a 64-bit system. The remainder gives information about your distribution.

1.1.3 Verify the system has gcc installed.

To verify the version of gcc installed on your system, type the following on the command line:

$ gcc --version

If an error message displays, you need to install the development tools from your Linux distribution or obtain a version of gcc and its accompanying toolchain from the Web.

1.1.4 Verify the system has the correct kernel headers and development packages installed.

The kernel headers and development packages for the currently running kernel can be installed with:

$ sudo apt-get install linux-headers-$(uname -r)



1.2 Install

1.2.1 Download the CUDA Toolkit 9.0

CUDA Toolkit 9.0 Downloads
这里写图片描述
这里写图片描述

1.2.2 Install the CUDA Toolkit 9.0

$ cd ~/Downloads
$ sudo dpkg -i cuda-repo-ubuntu1604_9.0.176-1_amd64.deb
$ sudo apt-key adv --fetch-keys http://developer.download.nvidia.com/compute/cuda/repos/ubuntu1604/x86_64/7fa2af80.pub
$ sudo apt-get update
$ sudo apt-get install cuda-9-0


1.3 Post-installation Actions

$ export PATH=/usr/local/cuda-9.0/bin${PATH:+:${PATH}}
$ export LD_LIBRARY_PATH=/usr/local/cuda-9.0/lib64\
                         ${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}
  • 重启以更新环境变量


2. cuDNN v7.0

2.1 Download

To download the cuDNN , click here
这里写图片描述
The download of cuDNN requires membership of the NVIDIA Developer Program,so you need to create your account first and then Login.
这里写图片描述
Do not choose the latest version
这里写图片描述
Choose the v7.0.5 for CUDA 9.0
这里写图片描述
Download Runtime 、 Developer 、Code Samples
这里写图片描述

2.2 Install

$ cd ~/Downloads 
$ sudo dpkg -i libcudnn7_7.0.5.15-1+cuda9.0_amd64.deb
$ sudo dpkg -i libcudnn7-dev_7.0.5.15-1+cuda9.0_amd64.deb
$ sudo dpkg -i libcudnn7-doc_7.0.5.15-1+cuda9.0_amd64.deb


2.3 Verifying

To verify that cuDNN is installed and is running properly, compile the mnistCUDNN

  • Copy the cuDNN sample to a writable path.
$ cp -r /usr/src/cudnn_samples_v7/ $HOME
  • Go to the writable path.
$ cd $HOME/cudnn_samples_v7/mnistCUDNN
  • Compile the mnistCUDNN sample.
$ make clean && make
  • Run the mnistCUDNN sample.
$ ./mnistCUDNN

If cuDNN is properly installed and running on your Linux system, you will see a
message similar to the following:

Test passed!


3. Virtualenv + Tensorflow1.5

3.1 Install the Virtualenv

  • 发出下列其中一条命令来安装 pip 和 Virtualenv:
$ sudo apt-get install python3-pip python3-dev python-virtualenv
  • 发出下列其中一条命令来创建 Virtualenv 环境:
$ sudo virtualenv --system-site-packages -p python3 ~/tensorflow 

targetDirectory 用于指定 Virtualenv 树的顶层目录。我们的说明中假定 targetDirectory 为 ~/tensorflow,但您可以选择任何目录。

3.2 source the Virtualenv

  • 为了避免每次激活输入长命令,我们用tensorflow来代替”source ~/tensorflow/bin/activate”。
  • 打开~/.bashrc file文件
$ sudo gedit ~/.bashrc
  • 文件最后一行追加一行,然后保存并关闭文件
alias tensorflow="source ~/tensorflow/bin/activate"
  • 无需重启的更新环境变量的方法
$ source ~/.bashrc
  • 发出下列其中一条命令来激活 Virtualenv 环境:
$ tensorflow
  • 执行上述 source 命令后,您的提示符应该会变成如下内容则表示成功
(tensorflow)$ 


3.3 Install the Tensorflow

  • 发出下列其中一条命令以在处于活动状态的 Virtualenv 环境中安装

  • TensorFlow-CPU版本(无需安装CUDA和CUDNN)

(tensorflow)$ sudo pip3 install --upgrade tensorflow
  • TensorFlow-GPU版本(需要安装CUDA和CUDNN)
(tensorflow)$ sudo pip3 install --upgrade tensorflow-gpu


4.3 Verifying the Tensorflow

$ tensorflow
(tensorflow)$ Python3
>> import tensorflow as tf
>> hello = tf.constant('Hello, TensorFlow!')
>> sess = tf.Session()
>> print(sess.run(hello))

若如果系统输出以下内容,就说明您可以开始编写 TensorFlow 程序了:

Hello, TensorFlow!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值