脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - Ubuntu20.04环境安装tensorflow2的方法步骤

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

2021-08-31 00:19浪子吴天 Python

这篇文章主要介绍了Ubuntu20.04环境安装tensorflow2的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

一、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版本

?
1
2
root@ubuntuvm:~# python3 -v
python 3.8.5

2、安装venv模块

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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、创建项目目录

?
1
2
bdsc@ubuntuvm:~$ mkdir mytensorflow
bdsc@ubuntuvm:~$ cd mytensorflow/

4、创建虚拟环境

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

5、激活虚拟环境

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

6、升级pip

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
(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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
(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、查看安装模块及版本

?
1
2
3
4
5
6
7
8
9
10
11
(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

?
1
2
3
4
5
6
7
8
9
10
11
12
(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语句前加上如下语句

?
1
2
3
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内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/carefree2005/article/details/113183136

延伸 · 阅读

精彩推荐