Ubuntu20.04環(huán)境安裝tensorflow2的方法步驟
一、TensorFlow介紹
1、簡(jiǎn)介
TensorFlow是一個(gè)基于數(shù)據(jù)流編程(dataflow programming)的符號(hào)數(shù)學(xué)系統(tǒng),被廣泛應(yīng)用于各類(lèi)機(jī)器學(xué)習(xí)(machine learning)算法的編程實(shí)現(xiàn),其前身是谷歌的神經(jīng)網(wǎng)絡(luò)算法庫(kù)DistBelief 。Tensorflow擁有多層級(jí)結(jié)構(gòu),可部署于各類(lèi)服務(wù)器、PC終端和網(wǎng)頁(yè)并支持GPU和TPU高性能數(shù)值計(jì)算,被廣泛應(yīng)用于谷歌內(nèi)部的產(chǎn)品開(kāi)發(fā)和各領(lǐng)域的科學(xué)研究 。TensorFlow由谷歌人工智能團(tuán)隊(duì)谷歌大腦(Google Brain)開(kāi)發(fā)和維護(hù),擁有包括TensorFlow Hub、TensorFlow Lite、TensorFlow Research Cloud在內(nèi)的多個(gè)項(xiàng)目以及各類(lèi)應(yīng)用程序接口(Application Programming Interface, API)。自2015年11月9日起,TensorFlow依據(jù)阿帕奇授權(quán)協(xié)議(Apache 2.0 open source license)開(kāi)放源代碼 。
2、TensorFlow 支持的系統(tǒng)
以下 64 位系統(tǒng)上測(cè)試過(guò) TensorFlow 并且這些系統(tǒng)支持 TensorFlow:
- Python 3.5–3.8
- Ubuntu 16.04 或更高版本
- Windows 7 或更高版本(含 C++ 可再發(fā)行軟件包)
- macOS 10.12.6 (Sierra) 或更高版本(不支持 GPU)
- Raspbian 9.0 或更高版本
二、安裝步驟
1、檢查確認(rèn)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、創(chuàng)建項(xiàng)目目錄
bdsc@ubuntuvm:~$ mkdir mytensorflow bdsc@ubuntuvm:~$ cd mytensorflow/
4、創(chuàng)建虛擬環(huán)境
bdsc@ubuntuvm:~/mytensorflow$ python3 -m venv tenf
5、激活虛擬環(huán)境
bdsc@ubuntuvm:~/mytensorflow$ source tenf/bin/activate (tenf) bdsc@ubuntuvm:~/mytensorflow$
6、升級(jí)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、驗(yàn)證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是否報(bào)錯(cuò)
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 “l(fā)icense” 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時(shí)報(bào)錯(cuò)無(wú)法加載動(dòng)態(tài)庫(kù)libcudart.so.11.0
解決方法:
在import tensorflow as tf語(yǔ)句前加上如下語(yǔ)句
import os os.environ[‘TF_CPP_MIN_LOG_LEVEL'] = ‘2' import tensorflow as tf
2、報(bào)錯(cuò)session屬性找不到
原因:
報(bào)錯(cuò)AttributeError: module ‘tensorflow' has no attribute ‘Session'。這其實(shí)不是安裝錯(cuò)誤,是因?yàn)樵谛碌腡ensorflow 2.0版本中已經(jīng)移除了Session這一模塊。
解決方法:
改換運(yùn)行代碼,tf.compat.v1.Session()
到此這篇關(guān)于Ubuntu20.04環(huán)境安裝tensorflow2的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)Ubuntu20.04安裝tensorflow2內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 解決Tensorflow安裝成功,但在導(dǎo)入時(shí)報(bào)錯(cuò)的問(wèn)題
- Python3.7安裝keras和TensorFlow的教程圖解
- Windows10下Tensorflow2.0 安裝及環(huán)境配置教程(圖文)
- 詳解TensorFlow在windows上安裝與簡(jiǎn)單示例
- windows環(huán)境下tensorflow安裝過(guò)程詳解
- TensorFlow2.1.0最新版本安裝詳細(xì)教程
- python3.6.3安裝圖文教程 TensorFlow安裝配置方法
- Windows上安裝tensorflow 詳細(xì)教程(圖文詳解)
- Win7下Python與Tensorflow-CPU版開(kāi)發(fā)環(huán)境的安裝與配置過(guò)程
相關(guān)文章
Tensorflow限制CPU個(gè)數(shù)實(shí)例
今天小編就為大家分享一篇Tensorflow限制CPU個(gè)數(shù)實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-02-02Python生成隨機(jī)驗(yàn)證碼代碼實(shí)例解析
這篇文章主要介紹了Python生成隨機(jī)驗(yàn)證碼代碼實(shí)例解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06Python?pandas刪除指定行/列數(shù)據(jù)的方法實(shí)例
這篇文章主要給大家介紹了關(guān)于Python?pandas刪除指定行/列數(shù)據(jù)的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2022-01-01詳解Python的Django框架中Manager方法的使用
這篇文章主要介紹了Python的Django框架中Manager方法的使用,包括修改初始Manager QuerySets和增加額外的Manager方法等操作,需要的朋友可以參考下2015-07-07Python實(shí)現(xiàn)的堆排序算法原理與用法實(shí)例分析
這篇文章主要介紹了Python實(shí)現(xiàn)的堆排序算法,簡(jiǎn)單描述了堆排序的原理,并結(jié)合實(shí)例形式分析了Python實(shí)現(xiàn)堆排序的相關(guān)操作技巧,代碼中備有較為詳細(xì)的注釋便于理解,需要的朋友可以參考下2017-11-11