Ubuntu20.04环境安装tensorflow2的方法步骤


Posted in Python onJanuary 29, 2021

一、TensorFlow介绍

1、简介

  TensorFlow是一个基于数据流编程(dataflow programming)的符号数学系统,被广泛应用于各类机器学习(machine learning)算法的编程实现,其前身是谷歌的神经网络算法库DistBelief 。Tensorflow拥有多层级结构,可部署于各类服务器、PC终端和网页并支持GPU和TPU高性能数值计算,被广泛应用于谷歌内部的产品开发和各领域的科学研究 。TensorFlow由谷歌人工智能团队谷歌大脑(Google Brain)开发和维护,拥有包括TensorFlow Hub、TensorFlow Lite、TensorFlow Research Cloud在内的多个项目以及各类应用程序接口(Application Programming Interface, API)。自2015年11月9日起,TensorFlow依据阿帕奇授权协议(Apache 2.0 open source license)开放源代码 。

2、TensorFlow 支持的系统

  以下 64 位系统上测试过 TensorFlow 并且这些系统支持 TensorFlow:

  • Python 3.5?3.8
  • Ubuntu 16.04 或更高版本
  • Windows 7 或更高版本(含 C++ 可再发行软件包)
  • macOS 10.12.6 (Sierra) 或更高版本(不支持 GPU)
  • Raspbian 9.0 或更高版本

二、安装步骤

1、检查确认python版本

root@ubuntuvm:~# python3 -V
Python 3.8.5

2、安装venv模块

root@ubuntuvm:/home# apt install python3-venv python3-dev
Reading package lists… Done
Building dependency tree
Reading state information… Done
The following additional packages will be installed:
libc-dev-bin libc6 libc6-dbg libc6-dev libcrypt-dev libexpat1-dev libpython3-dev libpython3.8-dev linux-libc-dev manpages-dev python-pip-whl
python3-distutils python3.8-dev python3.8-venv zlib1g zlib1g-dev
Suggested packages:
glibc-doc
The following NEW packages will be installed:
libc-dev-bin libc6-dev libcrypt-dev libexpat1-dev libpython3-dev libpython3.8-dev linux-libc-dev manpages-dev python-pip-whl python3-dev
python3-distutils python3-venv python3.8-dev python3.8-venv zlib1g-dev
The following packages will be upgraded:
libc6 libc6-dbg zlib1g
3 upgraded, 15 newly installed, 0 to remove and 233 not upgraded.
Need to get 21.2 MB of archives.
After this operation, 56.1 MB of additional disk space will be used.
Do you want to continue? [Y/n] y

3、创建项目目录

bdsc@ubuntuvm:~$ mkdir mytensorflow
bdsc@ubuntuvm:~$ cd mytensorflow/

4、创建虚拟环境

bdsc@ubuntuvm:~/mytensorflow$ python3 -m venv tenf

5、激活虚拟环境

bdsc@ubuntuvm:~/mytensorflow$ source tenf/bin/activate
(tenf) bdsc@ubuntuvm:~/mytensorflow$

6、升级pip

(tenf) bdsc@ubuntuvm:~/mytensorflow$ pip3 -V
pip 20.0.2 from /home/bdsc/mytensorflow/tenf/lib/python3.8/site-packages/pip (python 3.8)
(tenf) bdsc@ubuntuvm:~/mytensorflow$ pip install --upgrade pip
Collecting pip
Downloading pip-21.0-py3-none-any.whl (1.5 MB)
|????????????????????????????????| 1.5 MB 1.0 MB/s
Installing collected packages: pip
Attempting uninstall: pip
Found existing installation: pip 20.0.2
Uninstalling pip-20.0.2:
Successfully uninstalled pip-20.0.2
Successfully installed pip-21.0
(tenf) bdsc@ubuntuvm:~/mytensorflow$ pip3 -V
pip 21.0 from /home/bdsc/mytensorflow/tenf/lib/python3.8/site-packages/pip (python 3.8)

7、安装tensorflow

(tenf) bdsc@ubuntuvm:~/mytensorflow$ pip3 install tensorflow
Collecting tensorflow
Downloading tensorflow-2.4.1-cp38-cp38-manylinux2010_x86_64.whl (394.4 MB)
|????????????????????????????????| 394.4 MB 4.9 kB/s

Collecting tensorflow-estimator<2.5.0,>=2.4.0
Downloading tensorflow_estimator-2.4.0-py2.py3-none-any.whl (462 kB)
|????????????????????????????????| 462 kB 24 kB/s
Collecting astunparse~=1.6.3
Downloading astunparse-1.6.3-py2.py3-none-any.whl (12 kB)
Collecting protobuf>=3.9.2
Downloading protobuf-3.14.0-cp38-cp38-manylinux1_x86_64.whl (1.0 MB)
|????????????????????????????????| 1.0 MB 35 kB/s
Collecting gast==0.3.3
Downloading gast-0.3.3-py2.py3-none-any.whl (9.7 kB)
Collecting flatbuffers~=1.12.0
Downloading flatbuffers-1.12-py2.py3-none-any.whl (15 kB)
Collecting absl-py~=0.10
Downloading absl_py-0.11.0-py3-none-any.whl (127 kB)
|????????????????????????????????| 127 kB 52 kB/s
Collecting wheel~=0.35
Downloading wheel-0.36.2-py2.py3-none-any.whl (35 kB)
Collecting termcolor~=1.1.0
Downloading termcolor-1.1.0.tar.gz (3.9 kB)
Collecting wrapt~=1.12.1
Downloading wrapt-1.12.1.tar.gz (27 kB)
Collecting grpcio~=1.32.0
Downloading grpcio-1.32.0-cp38-cp38-manylinux2014_x86_64.whl (3.8 MB)
|????????????????????????????????| 3.8 MB 35 kB/s
Collecting six~=1.15.0
Downloading six-1.15.0-py2.py3-none-any.whl (10 kB)
Collecting typing-extensions~=3.7.4
Downloading typing_extensions-3.7.4.3-py3-none-any.whl (22 kB)
Collecting tensorboard~=2.4
Downloading tensorboard-2.4.1-py3-none-any.whl (10.6 MB)
|????????????????????????????????| 10.6 MB 46 kB/s
Collecting keras-preprocessing~=1.1.2
Downloading Keras_Preprocessing-1.1.2-py2.py3-none-any.whl (42 kB)
|????????????????????????????????| 42 kB 43 kB/s
Collecting h5py~=2.10.0
Downloading h5py-2.10.0-cp38-cp38-manylinux1_x86_64.whl (2.9 MB)
|????????????????????????????????| 2.9 MB 46 kB/s
Collecting opt-einsum~=3.3.0
Downloading opt_einsum-3.3.0-py3-none-any.whl (65 kB)
|????????????????????????????????| 65 kB 45 kB/s
Collecting google-pasta~=0.2
Downloading google_pasta-0.2.0-py3-none-any.whl (57 kB)
|????????????????????????????????| 57 kB 26 kB/s
Collecting numpy~=1.19.2
Downloading numpy-1.19.5-cp38-cp38-manylinux2010_x86_64.whl (14.9 MB)
|????????????????????????????????| 14.9 MB 65 kB/s
Collecting markdown>=2.6.8
Downloading Markdown-3.3.3-py3-none-any.whl (96 kB)
|????????????????????????????????| 96 kB 66 kB/s
Collecting google-auth-oauthlib<0.5,>=0.4.1
Downloading google_auth_oauthlib-0.4.2-py2.py3-none-any.whl (18 kB)
Collecting werkzeug>=0.11.15
Downloading Werkzeug-1.0.1-py2.py3-none-any.whl (298 kB)
|????????????????????????????????| 298 kB 44 kB/s
Requirement already satisfied: setuptools>=41.0.0 in ./tenf/lib/python3.8/site-packages (from tensorboard~=2.4->tensorflow) (44.0.0)
Collecting tensorboard-plugin-wit>=1.6.0
Downloading tensorboard_plugin_wit-1.8.0-py3-none-any.whl (781 kB)
|????????????????????????????????| 781 kB 43 kB/s
Collecting requests<3,>=2.21.0
Downloading requests-2.25.1-py2.py3-none-any.whl (61 kB)
|????????????????????????????????| 61 kB 57 kB/s
Collecting google-auth<2,>=1.6.3
Downloading google_auth-1.24.0-py2.py3-none-any.whl (114 kB)
|????????????????????????????????| 114 kB 37 kB/s
Collecting rsa<5,>=3.1.4
Downloading rsa-4.7-py3-none-any.whl (34 kB)
Collecting pyasn1-modules>=0.2.1
Downloading pyasn1_modules-0.2.8-py2.py3-none-any.whl (155 kB)
|????????????????????????????????| 155 kB 34 kB/s
Collecting cachetools<5.0,>=2.0.0
Downloading cachetools-4.2.1-py3-none-any.whl (12 kB)
Collecting requests-oauthlib>=0.7.0
Downloading requests_oauthlib-1.3.0-py2.py3-none-any.whl (23 kB)
Collecting pyasn1<0.5.0,>=0.4.6
Downloading pyasn1-0.4.8-py2.py3-none-any.whl (77 kB)
|????????????????????????????????| 77 kB 42 kB/s
Collecting chardet<5,>=3.0.2
Downloading chardet-4.0.0-py2.py3-none-any.whl (178 kB)
|????????????????????????????????| 178 kB 47 kB/s
Collecting idna<3,>=2.5
Downloading idna-2.10-py2.py3-none-any.whl (58 kB)
|????????????????????????????????| 58 kB 31 kB/s
Collecting urllib3<1.27,>=1.21.1
Downloading urllib3-1.26.2-py2.py3-none-any.whl (136 kB)
|????????????????????????????????| 136 kB 33 kB/s
Collecting certifi>=2017.4.17
Downloading certifi-2020.12.5-py2.py3-none-any.whl (147 kB)
|????????????????????????????????| 147 kB 26 kB/s
Collecting oauthlib>=3.0.0
Downloading oauthlib-3.1.0-py2.py3-none-any.whl (147 kB)
|????????????????????????????????| 147 kB 36 kB/s
Using legacy ‘setup.py install' for termcolor, since package ‘wheel' is not installed.
Using legacy ‘setup.py install' for wrapt, since package ‘wheel' is not installed.
Installing collected packages: urllib3, pyasn1, idna, chardet, certifi, six, rsa, requests, pyasn1-modules, oauthlib, cachetools, requests-oauthlib, google-auth, wheel, werkzeug, tensorboard-plugin-wit, protobuf, numpy, markdown, grpcio, google-auth-oauthlib, absl-py, wrapt, typing-extensions, termcolor, tensorflow-estimator, tensorboard, opt-einsum, keras-preprocessing, h5py, google-pasta, gast, flatbuffers, astunparse, tensorflow
Running setup.py install for wrapt … done
Running setup.py install for termcolor … done
Successfully installed absl-py-0.11.0 astunparse-1.6.3 cachetools-4.2.1 certifi-2020.12.5 chardet-4.0.0 flatbuffers-1.12 gast-0.3.3 google-auth-1.24.0 google-auth-oauthlib-0.4.2 google-pasta-0.2.0 grpcio-1.32.0 h5py-2.10.0 idna-2.10 keras-preprocessing-1.1.2 markdown-3.3.3 numpy-1.19.5 oauthlib-3.1.0 opt-einsum-3.3.0 protobuf-3.14.0 pyasn1-0.4.8 pyasn1-modules-0.2.8 requests-2.25.1 requests-oauthlib-1.3.0 rsa-4.7 six-1.15.0 tensorboard-2.4.1 tensorboard-plugin-wit-1.8.0 tensorflow-2.4.1 tensorflow-estimator-2.4.0 termcolor-1.1.0 typing-extensions-3.7.4.3 urllib3-1.26.2 werkzeug-1.0.1 wheel-0.36.2 wrapt-1.12.1

8、验证tensoflow2是否安装成功

1、查看安装模块及版本

(tenf) bdsc@ubuntuvm:~/mytensorflow$ pip3 show tensorflow
Name: tensorflow
Version: 2.4.1
Summary: TensorFlow is an open source machine learning framework for everyone.
Home-page: https://www.tensorflow.org/
Author: Google Inc.
Author-email: packages@tensorflow.org
License: Apache 2.0
Location: /home/bdsc/mytensorflow/tenf/lib/python3.8/site-packages
Requires: typing-extensions, google-pasta, protobuf, wrapt, gast, tensorboard, opt-einsum, flatbuffers, numpy, wheel, astunparse, keras-preprocessing, six, termcolor, tensorflow-estimator, grpcio, absl-py, h5py
Required-by:

2、查看import tensorflow是否报错

Ubuntu20.04环境安装tensorflow2的方法步骤

3、使用TensorFlow打印hello,Tensorflow

(tenf) bdsc@ubuntuvm:~/mytensorflow$ python3
Python 3.8.5 (default, Jul 28 2020, 12:59:40)
[GCC 9.3.0] on linux
Type “help”, “copyright”, “credits” or “license” for more information.
>>> import os
>>> os.environ[‘TF_CPP_MIN_LOG_LEVEL'] = ‘2'
>>> import tensorflow as tf
>>> tf.compat.v1.disable_eager_execution()
>>> hello = tf.constant(‘Hello, Tensorflow!')
>>> sess = tf.compat.v1.Session()
>>> print(sess.run(hello))
b'Hello, Tensorflow!'

三、FAQ

1、import tensorflow时报错无法加载动态库libcudart.so.11.0

Ubuntu20.04环境安装tensorflow2的方法步骤

解决方法:
在import tensorflow as tf语句前加上如下语句

import os
os.environ[‘TF_CPP_MIN_LOG_LEVEL'] = ‘2'
import tensorflow as tf

Ubuntu20.04环境安装tensorflow2的方法步骤

2、报错session属性找不到

Ubuntu20.04环境安装tensorflow2的方法步骤

原因:
报错AttributeError: module ‘tensorflow' has no attribute ‘Session'。这其实不是安装错误,是因为在新的Tensorflow 2.0版本中已经移除了Session这一模块。
解决方法:
改换运行代码,tf.compat.v1.Session()

到此这篇关于Ubuntu20.04环境安装tensorflow2的实现的文章就介绍到这了,更多相关Ubuntu20.04安装tensorflow2内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Python 相关文章推荐
Python使用matplotlib绘制动画的方法
May 20 Python
详解Python map函数及Python map()函数的用法
Nov 16 Python
Python中__slots__属性介绍与基本使用方法
Sep 05 Python
使用Python制作自动推送微信消息提醒的备忘录功能
Sep 06 Python
PyQt5固定窗口大小的方法
Jun 18 Python
python实现图像检索的三种(直方图/OpenCV/哈希法)
Aug 08 Python
Python3操作Excel文件(读写)的简单实例
Sep 02 Python
Python 转换RGB颜色值的示例代码
Oct 13 Python
Django连接数据库并实现读写分离过程解析
Nov 13 Python
python 截取XML中bndbox的坐标中的图像,另存为jpg的实例
Mar 10 Python
Python 如何实现数据库表结构同步
Sep 29 Python
Pygame Draw绘图函数的具体使用
Nov 17 Python
python3定位并识别图片验证码实现自动登录功能
Jan 29 #Python
python中numpy数组与list相互转换实例方法
Jan 29 #Python
详解Python中的GIL(全局解释器锁)详解及解决GIL的几种方案
Jan 29 #Python
Python爬虫获取op.gg英雄联盟英雄对位胜率的源码
Jan 29 #Python
python中spy++的使用超详细教程
Jan 29 #Python
Python Selenium破解滑块验证码最新版(GEETEST95%以上通过率)
Jan 29 #Python
详解pycharm的python包opencv(cv2)无代码提示问题的解决
Jan 29 #Python
You might like
PHP5新特性: 更加面向对象化的PHP
2006/11/18 PHP
IStream与TStream之间的相互转换
2008/08/01 PHP
ThinkPHP框架里隐藏index.php
2016/04/12 PHP
深入理解 PHP7 中全新的 zval 容器和引用计数机制
2018/10/15 PHP
Laravel jwt 多表(多用户端)验证隔离的实现
2019/12/18 PHP
JavaScript DOM学习第八章 表单错误提示
2010/02/19 Javascript
javascript的变量、传值、传址、参数之间关系
2015/07/26 Javascript
JS实现的另类手风琴效果网页内容切换代码
2015/09/08 Javascript
js删除局部变量的实现方法
2016/06/25 Javascript
xcode中获取js文件的路径方法(推荐)
2016/11/05 Javascript
jQuery实现页面滚动时智能浮动定位
2017/01/08 Javascript
jQuery实现全选、反选和不选功能
2017/08/16 jQuery
js中位运算的运用实例分析
2018/12/11 Javascript
Vue3.0 响应式系统源码逐行分析讲解
2019/10/14 Javascript
Python中的迭代器漫谈
2015/02/03 Python
Python下实现的RSA加密/解密及签名/验证功能示例
2017/07/17 Python
Python读取图片为16进制表示简单代码
2018/01/19 Python
Atom的python插件和常用插件说明
2018/07/08 Python
利用Python实现微信找房机器人实例教程
2019/03/10 Python
Python实现12306火车票抢票系统
2019/07/04 Python
matplotlib 生成的图像中无法显示中文字符的解决方法
2020/06/10 Python
零基础学Python之前需要学c语言吗
2020/07/21 Python
Python如何将装饰器定义为类
2020/07/30 Python
HTML5 虚拟键盘出现挡住输入框的解决办法
2017/02/14 HTML / CSS
BLACKMORES澳洲官网:澳大利亚排名第一的保健品牌
2018/09/27 全球购物
新闻专业大学生找工作的自我评价
2013/10/30 职场文书
硕士研究生自我鉴定范文
2013/12/27 职场文书
优秀村官事迹材料
2014/01/10 职场文书
九年级历史教学反思
2014/01/27 职场文书
勤奋学习演讲稿
2014/05/10 职场文书
销售辞职信范文
2015/03/02 职场文书
2015年大学班长个人工作总结
2015/04/24 职场文书
一般纳税人申请报告
2015/05/18 职场文书
信息简报范文
2015/07/21 职场文书
关于践行三严三实的心得体会
2016/01/05 职场文书
2016廉洁从政心得体会
2016/01/19 职场文书