在Windows下Anaconda安装tensorflow
关于 TensorFlow
TensorFlow是一个采用数据流图(data flow graphs),用于数值计算的开源软件库。节点(Nodes)在图中表示数学操作,图中的线(edges)则表示在节点间相互联系的多维数据数组,即张量(tensor)。它灵活的架构让你可以在多种平台上展开计算,例如台式计算机中的一个或多个CPU(或GPU),服务器,移动设备等等。TensorFlow 最初由Google大脑小组(隶属于Google机器智能研究机构)的研究员和工程师们开发出来,用于机器学习和深度神经网络方面的研究,但这个系统的通用性使其也可广泛用于其他计算领域。
环境
$ conda --version
conda 4.7.12
$ python --version
Python 3.7.4
tensorflow不支持Python 3.7,所以准备一个Python 3.6的环境
$ conda create -n tensorflow python=3.6
tensorflow即为Python 3.6的环境名
启动tensorflow环境
$ conda activate tensorflow
关闭tensorflow环境
(tensorflow) $ conda deactivate
$
安装tensorflow
安装前一定要启动tensorflow环境
(tensorflow) $ pip install tensorflow
下载速度实在太慢,只能去找国内的镜像资源:
定向到清华镜像资源
(tensorflow) $ conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
(tensorflow) $ conda config --set show_channel_urls yes
安装cpu版本(若要安装gpu版本则将文件改为gpu,我没试过!)
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple/ https://mirrors.tuna.tsinghua.edu.cn/tensorflow/windows/cpu/tensorflow-1.2.0-cp36-cp36m-win_amd64.whl
测试Tensorflow
import tensorflow as tf
导入的时候会有一些警告信息,原因是numpy版本高了
安装低版本
pip3 install numpy == 1.16
pip install numpy ==1.16
虽然10多M但还是下得很慢很慢,又得找镜像,挨个试。
e.g. pip install -i http://mirrors.aliyun.com/pypi/simple/ numpy 这是临时指定镜像地址
清华:https://pypi.tuna.tsinghua.edu.cn/simple
阿里云:http://mirrors.aliyun.com/pypi/simple/
中国科技大学 https://pypi.mirrors.ustc.edu.cn/simple/
华中理工大学:http://pypi.hustunique.com/
山东理工大学:http://pypi.sdutlinux.org/
豆瓣:http://pypi.douban.com/simple/
note:新版ubuntu要求使用https源,要注意。
又出现了一个问题
ERROR: tensorboard 1.9.0 has requirement markdown>=2.6.8, but you’ll have markdo
wn 2.2.0 which is incompatible.
原因: setuptools 版本太低,不兼容。
升级 pip install --upgrade setuptools
升级后再安装numpy就不会报错了
屏蔽Tensorflow输出的调试和警告信息
import os
import tensorflow as tf
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
在tensorflow0.12以上版本中有效,其中:
0 = all messages are logged (default behavior)
1 = INFO messages are not printed
2 = INFO and WARNING messages are not printed
3 = INFO, WARNING, and ERROR messages are not printed
查看tensorflow是gpu版本还是cpu版本
import os
from tensorflow.python.client import device_lib
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "99"
print(device_lib.list_local_devices())
以上所有都是百度来的,安装过程中会遇到各种问题,所有我将它记录下来。