TensorFlow 初使用
安装环境
-
Anaconda 3 环境变量
anaconda3与anaconda3\Scripts
-
conda --version
环境
创建 conda 环境
conda create -n tensorlow python=3.8
Collecting package metadata (current_repodata.json): done
Solving environment: done
==> WARNING: A newer version of conda exists. <==
current version: 4.8.3
latest version: 4.9.2
Please update conda by running
$ conda update -n base -c defaults conda
## Package Plan ##
environment location: XXXXXXX\anaconda3\envs\tensortflow
added / updated specs:
- python=3.8
The following packages will be downloaded:
package | build
---------------------------|-----------------
ca-certificates-2020.10.14 | 0 122 KB
certifi-2020.6.20 | pyhd3eb1b0_3 155 KB
openssl-1.1.1h | he774522_0 4.8 MB
pip-20.2.4 | py39haa95532_0 1.8 MB
python-3.9.0 | h6244533_2 16.4 MB
setuptools-50.3.1 | py39haa95532_1 740 KB
sqlite-3.33.0 | h2a8f88b_0 809 KB
tzdata-2020d | h14c3975_0 110 KB
wheel-0.35.1 | pyhd3eb1b0_0 38 KB
wincertstore-0.2 | py39h2bbff1b_0 15 KB
------------------------------------------------------------
Total: 25.0 MB
The following NEW packages will be INSTALLED:
ca-certificates pkgs/main/win-64::ca-certificates-2020.10.14-0
certifi pkgs/main/noarch::certifi-2020.6.20-pyhd3eb1b0_3
openssl pkgs/main/win-64::openssl-1.1.1h-he774522_0
pip pkgs/main/win-64::pip-20.2.4-py39haa95532_0
python pkgs/main/win-64::python-3.9.0-h6244533_2
setuptools pkgs/main/win-64::setuptools-50.3.1-py39haa95532_1
sqlite pkgs/main/win-64::sqlite-3.33.0-h2a8f88b_0
tzdata pkgs/main/noarch::tzdata-2020d-h14c3975_0
vc pkgs/main/win-64::vc-14.1-h0510ff6_4
vs2015_runtime pkgs/main/win-64::vs2015_runtime-14.16.27012-hf0eaf9b_3
wheel pkgs/main/noarch::wheel-0.35.1-pyhd3eb1b0_0
wincertstore pkgs/main/win-64::wincertstore-0.2-py39h2bbff1b_0
zlib pkgs/main/win-64::zlib-1.2.11-h62dcd97_4
Proceed ([y]/n)? y
Downloading and Extracting Packages
tzdata-2020d | 110 KB | #################################### | 100%
wincertstore-0.2 | 15 KB | #################################### | 100%
setuptools-50.3.1 | 740 KB | #################################### | 100%
certifi-2020.6.20 | 155 KB | #################################### | 100%
pip-20.2.4 | 1.8 MB | ##########################5 | 74%
激活 conda 环境
conda init # 重启CMD.exe
conda info --envs
conda activate tensorflow
(tensortflow) C:\Windows\system32>
根据要在 conda 环境中安装的 TensorFlow 版本
python版本过高导致TensorFlow不能正确安装。此时需要安装低版本的python
conda install python=3.5 #降低pyth版本
(tensorflow)C:>
pip install tensorflow
pip install https://files.pythonhosted.org/packages/61/e1/1e713485caee272e7be779ac10bb0cec36ace3d9f981e067a9105a736791/tensorflow-2.3.1-cp38-cp38-win_amd64.whl
pip install --ignore-installed --upgrade tensorflow
我这里是需要翻墙的 不然下有些包总是显示超时:
Google 使用 wheel 标准分发,以.whl后缀的ZIP格式文件
输入python
输入以下代码:
import tensorflow as tf
message =tf.constant('Welcome to Tensorflow')
with tf.Session() as sess:
print ( sess.run(message).decode() )
tensorflow版本2.0无法兼容版本1.0. tf.disable_eager_execution()
#!/usr/bin/python3
import tensorflow.compat.v1 as tf
tf.disable_eager_execution() #保证sess.run()能够正常运行
message=tf.constant('Welcome')
with tf.Session() as sess:
print(sess.run(message).decode()) #
禁用conda 环境
deactivate #windows
source deactivate #MAC / Ubuntu