温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Ubuntu20.04中怎么安装TensorFlow

发布时间:2021-07-13 14:10:23 来源:亿速云 阅读:492 作者:Leah 栏目:大数据

这期内容当中小编将会给大家带来有关Ubuntu20.04中怎么安装TensorFlow,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。

1. pip安装TensorFlow

这里使用的是当前日期最新版的 Ubuntu20.04 作为操作系统平台。20.04默认安装了Python3.8版本,但是没有安装pip,所以可以使用apt先安装pip:

$ sudo apt install python3-pip -y

查看pip的版本,如果不是最新版本顺便将pip升级到最新版本:

$ pip3  -V
$ pip3 install --upgrade pip

安装pip的方式很多,例如可以把pip下载到本地使用Python命令安装等。安装TensorFlow2需要使用高于19.0的pip版本

安装之前需要把pip更新到最新版:

$ pip3 install --upgrade pip

安装 virtualenv 来帮助我们创建虚拟环境,这里使用的是国内阿里云的镜像源,下载速度会快很多:

$ pip3 install -U virtualenv -i https://mirrors.aliyun.com/pypi/simple

创建一个新的 Python 虚拟环境,创建一个 ./venv 目录来存放它:

$ virtualenv ./venv/

如果我们想把系统中已有的库也加入到虚拟环境中,可以加上--system-site-packages。指定Python版本适用-p参数,如:-p python3

激活该虚拟环境:

$ source venv/bin/activate

使用 pip 安装支持 GPU 的 TensorFlow 软件包,可以选择稳定版或预览版。安装稳定版的TensorFlow:

(venv) thanlon@thanlon:~$ pip install tensorflow -i https://mirrors.aliyun.com/pypi/simple/

如果安装只支持CPU版本:pip install tensorflow-cpu;同理安装只支持GPU版本:pip install tensorflow-cpu。指定版本:pip install tensorflow-gpu==2.2.0

安装预览版使用下面的命令:

(venv) thanlon@thanlon:~$ pip install tf-nightly -i https://mirrors.aliyun.com/pypi/simple/

安装之前一定要 保证/tmp空闲足够充足,大概需要3G~4G,当然也可以通过命令临时更改 /tmp 容量:

(venv) thanlon@thanlon:~$ sudo mount -t tmpfs -o size=4G /tmp(venv) thanlon@thanlon:~$ df -h
/dev/sda10      4.0G     0  4.0G    0% /tmp

查看已安装的所有包:

(venv) thanlon@thanlon:~$ pip list

测试不指定 CPU 和 GPU 版本的 TensorFlow 是否不区分 CPU 和 GPU,是否会支持GPU。测试发现确实支持GPU:
Ubuntu20.04中怎么安装TensorFlow

直接安装的TensorFlow,并没有指定GPU版本,却得到GPU的支持。实际上新版本的TensorFlow不指定GPU版本同样支持GPU。对于1.15及更早版本,CPU和GPU软件包是分开的。pip install tensorflow==1.15是CPU版本,pip install tensorflow-gpu==1.15是GPU版本。

2. Anaconda安装TensorFlow

Anaconda是一个开源的Python发行版本,其中包含了很多流行的用于科学计算、数据分析的Python包。可以从官网下载,官网地址:https://www.anaconda.com/products/individual
Ubuntu20.04中怎么安装TensorFlow
也可以到清华镜像站下载,官网链接:https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/
Ubuntu20.04中怎么安装TensorFlow
我们这里直接从清华镜像站复制了Anaconda最新版本的链接,使用 wget 或者 axel 先下载到本地:

$ axel https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/Anaconda3-5.3.1-Linux-x86_64.sh

执行脚本安装Anaconda,安装过程中根据提示结合自己需要做例如选择安装路径、是否安装Visual Stadio Code的一些操作:

$ ./Anaconda3-5.3.1-Linux-x86_64.sh

Anaconda安装成功之后接下来就可以安装TensorFlow了,Anaconda默认使用国外的镜像源,在我们安装TensorFlow之前先更换为国内的镜像源,如更换为 中科大镜像源,第一个是最重要且主要的镜像源地址,第二个添不添加都可以:

thanlon@thanlon:~$ conda config --add channels https://mirrors.ustc.edu.cn/anaconda/pkgs/main
thanlon@thanlon:~$ conda config --add channels https://mirrors.ustc.edu.cn/anaconda/pkgs/free

更新conda:

thanlon@thanlon:~$ conda update -n base -c defaults conda

conda比pip更加强大,可以安装非Python库。所以,非常推荐使用Anaconda安装TensorFlow!

Ubuntu20.04中怎么安装TensorFlow
创建并进入虚拟环境,离开虚拟环境使用 conda deactivate 命令:

thanlon@thanlon:~$ conda create -n venv python=3.8
thanlon@thanlon:~$ conda activate venv(venv) thanlon@thanlon:~$ conda deactivate

安装 TensorFlow GPU 最新版本,当前最新版是 tensorflow-gpu-2.2.0,已知版本的情况下可以指定版本:

(venv) thanlon@thanlon:~$ conda install tensorflow-gpu

为了检查安装的 TensorFlow 是否是 GPU 版本,需要安装 jupyter notebook 写程序测试:

(venv) thanlon@thanlon:~$ conda install jupyter notebook(venv) thanlon@thanlon:~$ jupyter notebook

测试程序如下,说明我们已经成功安装支持 GPU 的TensorFlow:
Ubuntu20.04中怎么安装TensorFlow
至此,Anaconda安装TensorFlow已完成!

3. TensorFlow Docker容器的构建

首先安装Docker,Dockeer官方安装文档:https://docs.docker.com/engine/install/ubuntu/
Ubuntu20.04中怎么安装TensorFlow
安装方式有三种,我这里选择存储库安装。卸载旧版本的Docker,如果之前安装过:

$ sudo apt-get remove docker docker-engine docker.io containerd runc

设置Docker存储库:

$ sudo apt-get install \
    apt-transport-https \
    ca-certificates \curl \
    gnupg-agent \
    software-properties-common

添加Docker的官方GPG密钥:

$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

设置稳定的存储:

$ sudo add-apt-repository \   "deb [arch=amd64] https://download.docker.com/linux/ubuntu \   $(lsb_release -cs) \
   stable"

更新apt程序包索引,并安装最新版本的Docker Engine和容器:

$ sudo apt-get update && sudo apt-get install docker-ce docker-ce-cli containerd.io

运行hello-world 映像来验证是否正确安装了Docker Engine:

$ sudo docker run -it hello-world

查看Docker是否启动:

$ systemctl status docker.service

至此Docker安装成功,下面开始安装TensorFlow。首先下载 TensorFlow Docker 镜像,TensorFlow 提供了很多ensorFlow Docker 镜像。官方 TensorFlow Docker 映像位于 tensorflow/tensorflow Docker Hub 代码库中:
Ubuntu20.04中怎么安装TensorFlow

References:https://tensorflow.google.cn/install/docker#gpu_support

更换下载源为国内的镜像源 Docker官方中国区:https://registry.docker-cn.com;阿里云:https://pee6w651.mirror.aliyuncs.com

$ sudo vim /etc/docker/daemon.json
$ systemctl restart docker.service 
$ cat /etc/docker/daemon.json{
   
   
   "registry-mirrors": ["https://pee6w651.mirror.aliyuncs.com"]}

这里就拉去一个支持 GPU 和 Jupyter 的版本:

$ docker pull tensorflow/tensorflow:latest-gpu-jupyter  # latest release w/ GPU support and Jupyter

Ubuntu20.04中怎么安装TensorFlow
成功安装:

thanlon@thanlon:~$ sudo docker images
REPOSITORY              TAG                  IMAGE ID            CREATED             SIZE
tensorflow/tensorflow   latest-gpu-jupyter   8d78dd1e1b64        8 weeks ago         3.99GB

Ubuntu20.04中怎么安装TensorFlow
查看本地已有的容器:

thanlon@thanlon:~$ sudo docker images
REPOSITORY              TAG                  IMAGE ID            CREATED             SIZE
tensorflow/tensorflow   latest-gpu-jupyter   8d78dd1e1b64        8 weeks ago         3.99GB
tensorflow/tensorflow   latest-gpu           f5ba7a196d56        8 weeks ago         3.84GB

接下来运行 TAG 是 latest-gpu 的容器,进入容器后可以查看内置的 Python 版本以及安装好的 TensorFlow:

thanlon@thanlon:~$ sudo docker run -it tensorflow/tensorflow:latest-gpu
root@c4fceafad48c:/# python -VPython 3.6.9
root@c4fceafad48c:/# pip list...
tensorflow-gpu         2.2.0...

可以另外开一个Terminal,查看正在运行的镜像:

thanlon@thanlon:~$ sudo docker psCONTAINER ID        IMAGE                              COMMAND             CREATED             STATUS              PORTS               NAMES
49b31ca53c49        tensorflow/tensorflow:latest-gpu   "/bin/bash"         17 seconds ago      Up 16 seconds                           gallant_shirley

运行 TAG 是 latest-gpu-jupyter 的容器:

thanlon@thanlon:~$ sudo docker run -p 8888:8888 tensorflow/tensorflow:latest-gpu-jupyter

浏览器访问下面的链接就可以使用 jupyter notebook:
Ubuntu20.04中怎么安装TensorFlow
Ubuntu20.04中怎么安装TensorFlow

上述就是小编为大家分享的Ubuntu20.04中怎么安装TensorFlow了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI