系統環境:Win10 64位操作系統,已裝有vs2013.
安裝包:
python-3.7.6【python-3.7.6-amd64.exe】
下載地址:https://www.python.org/downloads/windows/
tensorflow-2.1.0【tensorflow-2.1.0-cp37-cp37m-win_amd64.whl】
下載地址(清華大學開源軟件鏡像站):https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple/tensorflow/
安裝步驟如下:
# python安裝目錄(注:安裝時未設置修改環境變量)
D:\Programs\Python\Python37
# 查看python版本
C:\WINDOWS\system32>d:
D:\>cd D:\Programs\Python\Python37
D:\Programs\Python\Python37>python --version
Python 3.7.6
# 查看pip版本
D:\Programs\Python\Python37>cd Scripts
D:\Programs\Python\Python37\Scripts>pip --version
pip 19.2.3 from d:\programs\python\python37\lib\site-packages\pip (python 3.7)
# 創建虛擬環境
D:\Programs\Python\Python37>python -m venv D:\dev_py\py-3.7.6\tf_env
# 激活虛擬環境
cd D:\dev_py\py-3.7.6\tf_env\Scripts
activate
(tf_env) D:\dev_py\py-3.7.6\tf_env\Scripts>
# 更新pip
(tf_env) D:\dev_py\py-3.7.6\tf_env\Scripts>python -m pip install --upgrade pip
Successfully uninstalled pip-19.2.3
Successfully installed pip-20.0.2
注:
1)安裝tensorflow前,不要先安裝Anaconda;
2)如果安裝中報錯,如“Read timed out.”,下載報錯的依賴包後,手動安裝依賴包,然後重新安裝tensorflow.
# 離線安裝tensorflow[2.1.0]
(tf_env) D:\dev_py\py-3.7.6\tf_env\Scripts>pip install D:\dev_py\py-3.7.6\whl\tensorflow-2.1.0-cp37-cp37m-win_amd64.whl
以下是所有依賴包:
[wheel>=0.26] wheel-0.34.2-py2.py3-none-any.whl
[python_version >= "3"]
[setuptools>=41.0.0]
[requests<3,>=2.21.0] requests-2.23.0-py2.py3-none-any.whl
[protobuf>=3.6.0] protobuf-3.11.3-cp37-cp37m-win_amd64.whl
下載地址:https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple/protobuf/
[google-pasta>=0.1.6] google_pasta-0.1.6-py3-none-any.whl
下載地址: https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple/google-pasta/
[six>=1.12.0] six-1.14.0
[keras-applications>=1.0.8] Keras_Applications-1.0.8-py3-none-any.whl
[opt-einsum>=2.3.2] opt_einsum-3.2.1-py3-none-any.whl
[numpy<2.0,>=1.16.0] numpy-1.18.3-cp37-cp37m-win_amd64.whl
下載地址:https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple/numpy/
[gast==0.2.2] gast-0.2.2.tar.gz
[keras-preprocessing>=1.1.0] Keras_Preprocessing-1.1.0-py2.py3-none-any.whl
[tensorboard<2.2.0,>=2.1.0] tensorboard-2.1.1-py3-none-any.whl
下載地址:https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple/tensorboard/
[markdown>=2.6.8] Markdown-3.2.1-py2.py3-none-any.whl
下載地址:https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple/Markdown/
[google-auth-oauthlib<0.5,>=0.4.1] google_auth_oauthlib-0.4.1-py2.py3-none-any.whl
[grpcio>=1.24.3] grpcio-1.28.1-cp37-cp37m-win_amd64.whl
下載地址:https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple/grpcio/
[werkzeug>=0.11.15] Werkzeug-1.0.1-py2.py3-none-any.whl
[google-auth<2,>=1.6.3] google_auth-1.14.1-py2.py3-none-any.whl
[absl-py>=0.4] absl-py-0.9.0.tar.gz
[requests-oauthlib>=0.7.0] requests_oauthlib-1.3.0-py2.py3-none-any.whl
[cachetools<5.0,>=2.0.0] cachetools-4.1.0-py3-none-any.whl
[pyasn1-modules>=0.2.1] pyasn1_modules-0.2.8-py2.py3-none-any.whl
[rsa<4.1,>=3.1.4] rsa-4.0-py2.py3-none-any.whl
[certifi>=2017.4.17] certifi-2020.4.5.1-py2.py3-none-any.whl
[urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1] urllib3-1.25.9-py2.py3-none-any.whl
[idna<3,>=2.5] idna-2.9-py2.py3-none-any.whl
[chardet<4,>=3.0.2] chardet-3.0.4-py2.py3-none-any.whl
[oauthlib>=3.0.0] oauthlib-3.1.0-py2.py3-none-any.whl
[pyasn1<0.5.0,>=0.4.6] pyasn1-0.4.8-py2.py3-none-any.whl
[tensorflow-estimator<2.2.0,>=2.1.0rc0] tensorflow_estimator-2.1.0-py2.py3-none-any.whl
[wrapt>=1.11.1] wrapt-1.12.1.tar.gz
[scipy==1.4.1] scipy-1.4.1-cp37-cp37m-win_amd64.whl
[h5py] h5py-2.10.0-cp37-cp37m-win_amd64.whl
[astor-0.8.1]
[termcolor-1.1.0]
[tensorflow-2.1.0]
# 打印安裝清單
(tf_env) D:\dev_py\py-3.7.6\tf_env\Scripts>pip list
Package Version
-------------------- ----------
absl-py 0.9.0
astor 0.8.1
cachetools 4.1.0
certifi 2020.4.5.1
chardet 3.0.4
gast 0.2.2
google-auth 1.14.1
google-auth-oauthlib 0.4.1
google-pasta 0.1.6
grpcio 1.28.1
h5py 2.10.0
idna 2.9
Keras-Applications 1.0.8
Keras-Preprocessing 1.1.0
Markdown 3.2.1
numpy 1.18.3
oauthlib 3.1.0
opt-einsum 3.2.1
pip 20.0.2
protobuf 3.11.3
pyasn1 0.4.8
pyasn1-modules 0.2.8
requests 2.23.0
requests-oauthlib 1.3.0
rsa 4.0
scipy 1.4.1
setuptools 41.2.0
six 1.14.0
tensorboard 2.1.1
tensorflow 2.1.0
tensorflow-estimator 2.1.0
termcolor 1.1.0
urllib3 1.25.9
Werkzeug 1.0.1
wheel 0.34.2
wrapt 1.12.1
# 下載"Microsoft C++ Redistributable for Visual Studio 2015, 2017 and 2019"
下載地址:https://support.microsoft.com/help/2977003/the-latest-supported-visual-c-downloads
# 測試 (無GPU環境會告警,可以忽略)
(tf_env) D:\dev_py\py-3.7.6\tf_env\Scripts>python
>>> import tensorflow as tf
2020-04-26 02:19:09.375376: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load dynamic library 'cudart64_101.dll'; dlerror: cudart64_101.dll not found
2020-04-26 02:19:09.382064: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine.
>>> a = tf.constant([1.0,2.0], name="a")
2020-04-26 02:23:25.357210: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load dynamic library 'nvcuda.dll'; dlerror: nvcuda.dll not found
2020-04-26 02:23:25.363372: E tensorflow/stream_executor/cuda/cuda_driver.cc:351] failed call to cuInit: UNKNOWN ERROR (303)
2020-04-26 02:23:25.371887: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:169] retrieving CUDA diagnostic information for host: LAPTOP-QRIA0P3T
2020-04-26 02:23:25.380329: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:176] hostname: LAPTOP-QRIA0P3T
2020-04-26 02:23:25.384576: I tensorflow/core/platform/cpu_feature_guard.cc:142] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
>>> tf.__version__
'2.1.0'
>>> tf.compat.v1.disable_eager_execution()
>>> hello = tf.constant('hello,tensorflow')
>>> sess= tf.compat.v1.Session()
>>> print(sess.run(hello))
b'hello,tensorflow'
>>> a = tf.constant([1.0,2.0], name="a")
>>> b = tf.constant([2.0,3.0], name="b")
>>> result = a + b
>>> sess = tf.compat.v1.Session()
>>> sess.run(result)
array([3., 5.], dtype=float32)
TensorFlow Core v2.1.0 在線文檔: