1. 安装Anaconda
安装步骤很简单,此文章略过。
2. 安装Tensorflow
安装包下载地址:
https://pypi.org/project/tensorflow/1.4.0/
这里假定anaconda是已经安装好的情况下,进行如下步骤进行安装tensorflow:
(1)打开已安装好的Anaconda Prompt:
(2)利用Anaconda创建一个python3.6.5的环境,环境名称为tensorflow ,输入下面命令:
(base) C:\Users\Administrator>conda create -n tensorflow python=3.6.5
Proceed ([y]/n)? y
(3)在Anaconda Prompt中启动tensorflow环境:
(base) C:\Users\Administrator>activate tensorflow
注:当不使用tensorflow时,关闭tensorflow环境,命令为:deactivate
(4)安装cpu版本的TensorFlow
pip install tensorflow-1.14.0-cp36-cp36m-win_amd64.whl
执行如下:
(tensorflow) G:\深度学习>pip install tensorflow-1.14.0-cp36-cp36m-win_amd64.whl
Processing g:\深度学习\tensorflow-1.14.0-cp36-cp36m-win_amd64.whl
Collecting protobuf>=3.6.1 (from tensorflow==1.14.0)
Using cached https://files.pythonhosted.org/packages/09/36/285541664581d34d869c44921971a8e6cd1059600610edd133e2f0e8823c/protobuf-3.9.1-cp36-cp36m-win_amd64.whl
Collecting termcolor>=1.1.0 (from tensorflow==1.14.0)
Collecting grpcio>=1.8.6 (from tensorflow==1.14.0)
Using cached https://files.pythonhosted.org/packages/9b/03/b18e4402007f11b42aefebba56443572faf255b9216e40e3d128c774a48d/grpcio-1.23.0-cp36-cp36m-win_amd64.whl
Collecting tensorflow-estimator<1.15.0rc0,>=1.14.0rc0 (from tensorflow==1.14.0)
Using cached https://files.pythonhosted.org/packages/3c/d5/21860a5b11caf0678fbc8319341b0ae21a07156911132e0e71bffed0510d/tensorflow_estimator-1.14.0-py2.py3-none-any.whl
Collecting wrapt>=1.11.1 (from tensorflow==1.14.0)
Collecting gast>=0.2.0 (from tensorflow==1.14.0)
Collecting tensorboard<1.15.0,>=1.14.0 (from tensorflow==1.14.0)
Using cached https://files.pythonhosted.org/packages/91/2d/2ed263449a078cd9c8a9ba50ebd50123adf1f8cfbea1492f9084169b89d9/tensorboard-1.14.0-py3-none-any.whl
Collecting six>=1.10.0 (from tensorflow==1.14.0)
Downloading https://files.pythonhosted.org/packages/73/fb/00a976f728d0d1fecfe898238ce23f502a721c0ac0ecfedb80e0d88c64e9/six-1.12.0-py2.py3-none-any.whl
Requirement already satisfied: wheel>=0.26 in e:\anaconda3\envs\tensorflow\lib\site-packages (from tensorflow==1.14.0) (0.33.4)
Collecting keras-preprocessing>=1.0.5 (from tensorflow==1.14.0)
Using cached https://files.pythonhosted.org/packages/28/6a/8c1f62c37212d9fc441a7e26736df51ce6f0e38455816445471f10da4f0a/Keras_Preprocessing-1.1.0-py2.py3-none-any.whl
Collecting google-pasta>=0.1.6 (from tensorflow==1.14.0)
Using cached https://files.pythonhosted.org/packages/d0/33/376510eb8d6246f3c30545f416b2263eee461e40940c2a4413c711bdf62d/google_pasta-0.1.7-py3-none-any.whl
Collecting absl-py>=0.7.0 (from tensorflow==1.14.0)
Collecting keras-applications>=1.0.6 (from tensorflow==1.14.0)
Using cached https://files.pythonhosted.org/packages/71/e3/19762fdfc62877ae9102edf6342d71b28fbfd9dea3d2f96a882ce099b03f/Keras_Applications-1.0.8-py3-none-any.whl
Collecting numpy<2.0,>=1.14.5 (from tensorflow==1.14.0)
Using cached https://files.pythonhosted.org/packages/87/f4/682e88f2b5c1d49a8011fadee57eb3c13f55f156536597a625109261314d/numpy-1.17.1-cp36-cp36m-win_amd64.whl
Collecting astor>=0.6.0 (from tensorflow==1.14.0)
Using cached https://files.pythonhosted.org/packages/d1/4f/950dfae467b384fc96bc6469de25d832534f6b4441033c39f914efd13418/astor-0.8.0-py2.py3-none-any.whl
Requirement already satisfied: setuptools in e:\anaconda3\envs\tensorflow\lib\site-packages (from protobuf>=3.6.1->tensorflow==1.14.0) (41.0.1)
Collecting werkzeug>=0.11.15 (from tensorboard<1.15.0,>=1.14.0->tensorflow==1.14.0)
Downloading https://files.pythonhosted.org/packages/b7/61/c0a1adf9ad80db012ed7191af98fa05faa95fa09eceb71bb6fa8b66e6a43/Werkzeug-0.15.6-py2.py3-none-any.whl (328kB)
|████████████████████████████████| 337kB 285kB/s
Collecting markdown>=2.6.8 (from tensorboard<1.15.0,>=1.14.0->tensorflow==1.14.0)
Using cached https://files.pythonhosted.org/packages/c0/4e/fd492e91abdc2d2fcb70ef453064d980688762079397f779758e055f6575/Markdown-3.1.1-py2.py3-none-any.whl
Collecting h5py (from keras-applications>=1.0.6->tensorflow==1.14.0)
Downloading https://files.pythonhosted.org/packages/01/1e/115c4403544a91001d9c618748b2e8786db45544e36b8a6cf3c525e9b57f/h5py-2.9.0-cp36-cp36m-win_amd64.whl (2.4MB)
|████████████████████████████████| 2.4MB ...
Installing collected packages: six, protobuf, termcolor, grpcio, tensorflow-estimator, wrapt, gast, absl-py, numpy, werkzeug, markdown, tensorboard, keras-preprocessing, google-pasta, h5py, keras-applications, astor, tensorflow
Successfully installed absl-py-0.8.0 astor-0.8.0 gast-0.2.2 google-pasta-0.1.7 grpcio-1.23.0 h5py-2.9.0 keras-applications-1.0.8 keras-preprocessing-1.1.0 markdown-3.1.1 numpy-1.17.1 protobuf-3.9.1 six-1.12.0 tensorboard-1.14.0 tensorflow-1.14.0 tensorflow-estimator-1.14.0 termcolor-1.1.0 werkzeug-0.15.6 wrapt-1.11.2
(5)测试tensorflow
import tensorflow as tf
hello = tf.constant('Hello, TensorFlow!')
sess = tf.Session()
print(sess.run(hello))
===》
b'Hello,Tensorflow'
3.常见报错问题
a).FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
_np_qint8 = np.dtype([("qint8", np.int8, 1)])
(tensorflow) G:\深度学习>python
原因:
numpy是一个基础的数组计算包,Tensorflow、 tensorboard、Keras-Preprocessing、h5py、pandas等一系列常用的第三方库都需要numpy的支持。这里提示numpy.core._multiarray_umath failed to import和numpy.core.umath failed to import ,得知无法正常import tensorflow 的原因是numpy库的引用出了问题。应该是numpy版本和当前Tensorflow版本兼容性问题。
解决方法:
(tensorflow) G:\深度学习>pip uninstall numpy
Uninstalling numpy-1.14.5:
Would remove:
e:\anaconda3\envs\tensorflow\lib\site-packages\numpy-1.14.5.dist-info\*
e:\anaconda3\envs\tensorflow\lib\site-packages\numpy\*
e:\anaconda3\envs\tensorflow\scripts\f2py.py
Proceed (y/n)? y
Successfully uninstalled numpy-1.14.5
(tensorflow) G:\深度学习>pip install numpy==1.15.0
Collecting numpy==1.15.0
Downloading https://files.pythonhosted.org/packages/53/d1/2499797c88de95ea3239ad7f6e6a47895fe51aad1aa2a116f50ec9e0ee74/numpy-1.15.0-cp36-none-win_amd64.whl (13.5MB)
|████████████████████████████████| 13.5MB 1.1MB/s
Installing collected packages: numpy
Successfully installed numpy-1.15.0
(tensorflow) G:\深度学习>pip uninstall numpy
Uninstalling numpy-1.15.0:
Would remove:
e:\anaconda3\envs\tensorflow\lib\site-packages\numpy-1.15.0.dist-info\*
e:\anaconda3\envs\tensorflow\lib\site-packages\numpy\*
e:\anaconda3\envs\tensorflow\scripts\conv-template.exe
e:\anaconda3\envs\tensorflow\scripts\f2py.exe
e:\anaconda3\envs\tensorflow\scripts\f2py.py
e:\anaconda3\envs\tensorflow\scripts\from-template.exe
Proceed (y/n)? y
Successfully uninstalled numpy-1.15.0
(tensorflow) G:\深度学习>python
Python 3.6.5 |Anaconda, Inc.| (default, Mar 29 2018, 13:32:41) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
ModuleNotFoundError: No module named 'numpy.core._multiarray_umath'
ImportError: numpy.core.multiarray failed to import
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "<frozen importlib._bootstrap>", line 968, in _find_and_load
SystemError: <class '_frozen_importlib._ModuleLockManager'> returned a result with an error set
ImportError: numpy.core._multiarray_umath failed to import
ImportError: numpy.core.umath failed to import
2019-09-06 21:48:49.194538: F tensorflow/python/lib/core/bfloat16.cc:675] Check failed: PyBfloat16_Type.tp_base != nullptr
(tensorflow) G:\深度学习>pip install numpy===1.16.5
Collecting numpy===1.16.5
WARNING: Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ReadTimeoutError("HTTPSConnectionPool(host='pypi.org', port=443): Read timed out. (read timeout=15)",)': /simple/numpy/
Downloading https://files.pythonhosted.org/packages/4c/7b/42bee615e731b54021d6e530573a3c6e7cbf16dd54a9ef7c9887d3324c14/numpy-1.16.5-cp36-cp36m-win_amd64.whl (11.9MB)
|████████████████████████████████| 11.9MB 59kB/s
Installing collected packages: numpy
Successfully installed numpy-1.16.5
b).2019-09-06 22:20:50.661911: I tensorflow/core/platform/cpu_feature_guard.cc:142] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
原因:
CPU 支持AVX AVX2 (可以加速CPU计算),但你安装的 TensorFlow 版本不支持
解决方法:
i. 初学者可以先忽略,直接执行如下命令:
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
说明:os.environ["TF_CPP_MIN_LOG_LEVEL"] = '1' # 默认,显示所有信息
os.environ["TF_CPP_MIN_LOG_LEVEL"] = '2' # 只显示 warning 和 Error
os.environ["TF_CPP_MIN_LOG_LEVEL"] = '3' # 只显示 Error
ii. 如果需要对CPU进行优化,可以访问下面的github,重新编译tensorflow源码以兼容AVX,参考如下链接:
https://github.com/lakshayg/tensorflow-build