机器学习小白(三)——TensorFlow安装

1、官方教程

官网https://www.tensorflow.org/,可以直接参考:官方安装教程,就懒得搬运了。不过需要自备梯子,没有的可以看后文。

2、官方教程注意点

(2018.05.24版)
本人是GeForce GTX 960M的渣笔记本显卡,算是刚够到gpu版本的及格线上。目前安装的是1.8.0TensorFlow的gpu版本。不同版本对cuda和cudnn版本要求不一样,坑爹的是兼容性感觉不怎么样,最好是按官方要求的版本。

操作系统上官网是这么写的,需要符合的操作系统

我们已在如下配置的 64 位笔记本电脑/台式机操作系统中构建并测试过 TensorFlow:
MacOS X 10.11 (El Capitan) 或更高版本
Ubuntu 16.04 或更高版本
Windows 7 或更高版本
也许您能在其他笔记本电脑或台式机系统上安装 TensorFlow,但我们只支持上述配置(且只会修复这些配置中的问题)。

可以仅仅安装CPU版本的,也可以安装gpu版本的。如果硬件支持的话,建议装gpu版本的,速度差很多。

您必须从以下 TensorFlow 类型中选择其一来进行安装:
仅支持 CPU 的 TensorFlow。如果您的系统没有 NVIDIA® GPU,就必须安装此版本。请注意,此版本的 TensorFlow 通常更容易安装(用时通常在 5 或 10 分钟内),所以即使您拥有 NVIDIA GPU,我们也建议先安装此版本。预先构建的二进制文件将使用 AVX 指令。
**支持 GPU 的 TensorFlow。**TensorFlow 程序在 GPU 上的运行速度通常要比在 CPU 上快得多。因此,如果您的系统配有满足以下所示先决条件的 NVIDIA® GPU,并且您需要运行性能至关重要的应用,则最终应安装此版本。

安装gpu版本的要麻烦很多,要求如下:

运行支持 GPU 的 TensorFlow 所需满足的要求
如果您要使用本指南描述的其中一种方式安装支持 GPU 的 TensorFlow,就必须在系统上安装以下 NVIDIA 软件:

CUDA® 工具包 9.0。如需了解详情,请参阅 NVIDIA 的文档。请务必按照 NVIDIA 文档中的说明将相关的 CUDA 路径名附加到 %PATH% 环境变量中。
与 CUDA 工具包 9.0 相关联的 NVIDIA 驱动程序。
cuDNN v7.0。如需了解详情,请参阅 NVIDIA 文档。请注意,cuDNN 通常安装在与其他 CUDA DLL 不同的位置。请务必将您安装了 cuDNN DLL 的目录添加到 %PATH% 环境变量中。
CUDA 计算能力为 3.0 或更高的 GPU 卡(用于从源代码构建),以及 CUDA 计算能力为 3.5 或更高的 GPU 卡(用于安装我们的二进制文件)。如需了解支持的 GPU 卡的列表,请参阅 NVIDIA 文档。
如果您的某个软件包不同于上述版本,请改为指定的版本。特别是,cuDNN 版本必须完全匹配:如果无法找到 cuDNN64_7.dll,TensorFlow 就不会加载。要使用不同版本的 cuDNN,您必须从源代码构建。

1、安装时注意从NVIDIA下载的CUDA版本和cuDNN版本,我下了CUDA9.2的,然后运行时提示了错误:

ImportError: Could not find ‘cudart64_90.dll’. TensorFlow requires that this DLL be installed in a directory that is nam
ed in your %PATH% environment variable. Download and install CUDA 9.0 from this URL: https://developer.nvidia.com/cuda-t
oolkit

原因:CUDA版本不是TensorFlow要求的版本,可能过高!!!过低(╯°Д°)╯︵┻━┻
解决办法:下载错误提示里写的版本(默认传统安装)

2、使用时,遇到了提示

Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2

查阅的资料表明:

如果你有一个GPU,并且安装的是gpu版本的

你不应该关心AVX的支持,因为大多数昂贵的操作将被分派到一个GPU设备上(除非明确地设置)。在这种情况下,您可以简单地忽略此警告:

import os
os.environ[‘TF_CPP_MIN_LOG_LEVEL’] = ‘2’

如果安装的是CPU版本(pip install tensorflow)

1.在代码中加入如下代码,忽略警告:

import os
os.environ[‘TF_CPP_MIN_LOG_LEVEL’] = ‘2’
2.编译TensorFlow源码
如果您没有GPU并且希望尽可能多地利用CPU,那么如果您的CPU支持AVX,AVX2和FMA,则应该从针对CPU优化的源构建tensorflow。在这个问题中已经讨论过这个问题,也是这个GitHub问题。 Tensorflow使用称为bazel的ad-hoc构建系统,构建它并不是那么简单,但肯定是可行的。在此之后,不仅警告消失,tensorflow性能也应该改善。

3、如果还有其他错误,可以参考这篇博客,不过我没遇到过
https://blog.csdn.net/mm644706215/article/details/78797782

3、安装教程

3.1 ubuntu和mac

极客学院的
http://wiki.jikexueyuan.com/project/tensorflow-zh/get_started/os_setup.html
或一个中文社区的翻译
http://www.tensorfly.cn/tfdoc/get_started/os_setup.html

3.2 windows

以下是1-4我的安装步骤,这个是gpu版本的


按照Google的文档,cpu版本的只有一步,不需要gpu版本的1-3步骤:
C:> pip3 install –upgrade tensorflow
  • 用Anaconda正常流程如下,Google是这么写的

Anaconda 安装是由社区提供支持的,并没有任何官方支持。
按照以下步骤在 Anaconda 环境中安装 TensorFlow:

按照 Anaconda 下载网站上的说明下载并安装 Anaconda。

调用以下命令创建名为 tensorflow 的 conda 环境:

C:> conda create -n tensorflow pip python=3.5
发出以下命令以激活 conda 环境:

C:> activate tensorflow
(tensorflow)C:> # Your prompt should change

发出相关命令以在 conda 环境中安装 TensorFlow。要安装仅支持 CPU 的 TensorFlow 版本,请输入以下命令:

(tensorflow)C:> pip install –ignore-installed –upgrade tensorflow

要安装 GPU 版本的 TensorFlow,请输入以下命令(在同一行):

(tensorflow)C:> pip install –ignore-installed –upgrade tensorflow-gpu

也可以参考CSDN的一个博客

我也装了anaconda,不过一般都是用Pycharm,anaconda只是装了后常用科学计算的包都有了,所以1-3步骤后,直接用pip安装了tensorflow-gpu,主要是官方教程看到pip就装上了,不想麻烦就先试试,在pycharm里也能用,没有conda create和activate tensorflow的步骤。
不过目测在conda下不能用,因为没有在conda里创建TensorFlow环境,在Anaconda Prompt里用

conda env list

果然并没有TensorFlow环境。

4、测试:Hello, TensorFlow!

import tensorflow as tf
hello = tf.constant('Hello, TensorFlow!')
sess = tf.Session()
print(sess.run(hello))

输出如下所示,嗯一堆没什么用的东西后面是b’Hello, TensorFlow!’,可以算是能用了。

D:\PythonAnaconda\python.exe G:/mission/PythonLearning/MachineLearning/Tensorflow_Hello/hello.py
D:\PythonAnaconda\lib\site-packages\h5py__init__.py:34: FutureWarning: Conversion of the second argument of issubdtype from float to np.floating is deprecated. In future, it will be treated as np.float64 == np.dtype(float).type.
from ._conv import register_converters as _register_converters
2018-05-24 10:39:53.995925: I T:\src\github\tensorflow\tensorflow\core\platform\cpu_feature_guard.cc:140] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
2018-05-24 10:39:55.138147: I T:\src\github\tensorflow\tensorflow\core\common_runtime\gpu\gpu_device.cc:1356] Found device 0 with properties:
name: GeForce GTX 960M major: 5 minor: 0 memoryClockRate(GHz): 1.176
pciBusID: 0000:01:00.0
totalMemory: 2.00GiB freeMemory: 1.65GiB
2018-05-24 10:39:55.138613: I T:\src\github\tensorflow\tensorflow\core\common_runtime\gpu\gpu_device.cc:1435] Adding visible gpu devices: 0
2018-05-24 10:42:20.869521: I T:\src\github\tensorflow\tensorflow\core\common_runtime\gpu\gpu_device.cc:923] Device interconnect StreamExecutor with strength 1 edge matrix:
2018-05-24 10:42:20.869770: I T:\src\github\tensorflow\tensorflow\core\common_runtime\gpu\gpu_device.cc:929] 0
2018-05-24 10:42:20.869930: I T:\src\github\tensorflow\tensorflow\core\common_runtime\gpu\gpu_device.cc:942] 0: N
2018-05-24 10:42:20.870262: I T:\src\github\tensorflow\tensorflow\core\common_runtime\gpu\gpu_device.cc:1053] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 1419 MB memory) -> physical GPU (device: 0, name: GeForce GTX 960M, pci bus id: 0000:01:00.0, compute capability: 5.0)
b’Hello, TensorFlow!’

Process finished with exit code 0

TensorFlow入门

可以参考我的github,一起学习Tensorflow_Hello

猜你喜欢

转载自blog.csdn.net/u012692537/article/details/80432184