Depth learning algorithm (the first one) ---- TensorFlow 爱之初体验

Welcome attention to micro-channel public number " smart algorithm " - the original link (for better reading experience):

Depth learning algorithm (the first one) ---- TensorFlow 爱之初体验

In front of dozens of series, we learn together the knowledge of machine learning details, please reply "machine learning" in the "smart algorithm" micro-channel public number in the code view and practical learning. From the start of the period, we will learn the depth of knowledge to learn together. The way of learning, we are more exchanges and common progress. In this issue as follows:

  • Introduction and TensorFlow
  • TensorFlow the chopper small scale
  • Linear regression TF combat

End annexed the current codes keywords, keyword reply to download.

 

A. TensorFlow Introduction and Safety

 

Mess depth study of fire, we all know that Google is open source TensorFlow an artificial intelligence learning library. We interpret the tenrflow together in the end SLC: Tensor mean tensor represents N-dimensional array; Flow mean flow is calculated based on the representative of the data flow graph. The N-dimensional digital flow from one end to the process flow diagram of the other end, neural networks, artificial intelligence analysis and processing procedure.

Introduction 1.1 TensorFlow

TensorFlow basic rule is very simple, in general, in two steps:

First, to meet the definition of a computing task, and FIG. There are many nodes, followed by calculation TensorFlow efficient C ++ code optimized. Let's look at a simple example:

The figure represents a simple equation defined computing tasks, the tasks can be calculated by calling TensorFlow library. We know that any one programming language can be calculated so simple equation task, so why should we TensorFlow it?

For this simple equation, we certainly can not TensorFlow calculated under here we come together to learn as an example of why use TensorFlow calculated. One important factor is, TensorFlow FIG supports a computational task, split into a plurality of blocks (below):

Then by computing a plurality of parallel cross CPU or GPU, additionally TensorFlow further support distributed computing. Therefore, we in the case of the huge amount of data large neural network training time, there would be so much pressure.

In the current depth study in the library, there are many open-source learning library, as follows:

我们为什么选择TensorFlow库呢?

我们详细看下TensorFlow有什么优势:

  • 多平台运行,Windows,Linux,macOS,IOS和Android等
  • 提供简单的Python API (TELearn²) 兼容SciKit-Learn机器学习库
  • 提供简单API (TF-slim)去建立训练和评价神经网络模型
  • 包含很多机器学习方法的高性能的C++优化代码
  • 强大的可视化(TensorBoard)可以查看计算过程,比如学习曲线
  • TensorFlow还可以在谷歌云上进行计算图

当然,TensorFlow的优势不只上面所列,还有很多,我们一起学习和发掘。

1.2 TensorFlow安装

安装TensorFlow在Python中和安装其他库类似,可以用pip进行安装,也可以直接在Pycharm或Anaconda中进行安装。当然,机器支持GPU的话,建议安装GPU版本。so easy, 这里不再详述。

 

二. TensorFlow之牛刀小试

 

上节讲到,对于TensorFlow的操作分为两个步骤,首先是创建图,然后计算。在这里我们一起学习创建图和运行的几种方法,管理图和节点的生命周期。

2.1 创建图

以上节描述的图为例,如何创建呢?如下代码,即创建了该方程计算图:

这里需要注意的一点就是,虽然代码看起来好像做了一些计算,特别是最后一行,上面的代码中仅仅是创建了一个图而已,并没有去计算函数的值,甚至连变量初始化都没有做。那么如何计算上面的图呢?

2.2 运行图的常见方法

  • 为了计算上图方程,TensorFlow中需要开启一个session,并用这个session去初始化变量并且做最终计算。如下代码:

上面的方法有一个缺点,就是需要重复代码sess.run()来对变量进行初始化,并且需要手动关闭session,这样显得有些繁琐。

  • 这里我们介绍一种略微方便的一种方法,如下:

在这个with语句中,这个sess被设置为默认的session,也就是说语句x.initializer.run()相当于tf.get_default_session().run(x.initializer),并且f.eval()相当于调用tf.get_default_session().run(f),这种运行图的方法比较易读,并且自动关闭session,不需要我们手动关闭。关于图的管理,下面会介绍。

  • 上面介绍的两种运行图的方法都需要手动初始化每一个变量,但是对于大型的神经网络计算图来说,这无疑是自找麻烦,这里介绍一种自动初始化变量的方法:

在这个方法中,我们通过调用函数tf.global_variables_initializer(),代替了手动逐一初始化。但是这里需要注意的是,这个函数实际上并没有立即对变量进行初始化,而是在运行的时候才进行初始化。

  • 其实在python或者在jupyter 中,一般更喜欢这样运行图:

这种创建session的方法设置自己是默认的session,并且需要手动关闭session。但是这样减少了with语句的应用,使得看起来更为简洁。

从上面的方法可以看出,一个TensorFlow的程序明显的分为两部分,第一部分是创建一个计算图也称为创建阶段,第二部分是计算该图,也称为执行阶段。创建阶段一般是创建一个机器学习需要训练的模型,而执行阶段一般是执行训练过程。下节我们将会看到一实例。

2.3 图的管理

对于我们创建的任何一个节点,都会放到默认的图中,在大多数情况下,这样没问题,如下:

但是,有时候我们可能需要同时管理多个独立的图,这时可以通过新建一个图并临时把该图作为一个默认图(为什么要临时作为默认图),如下:

很多时候,我们在测试的时候,会重复运行一段代码,那么我们会结束一个包含很多节点的图,一般有两种方法,比如在jupyter中,其中一个方法就是重启jupyter kernel,但是常用的另一种方法是通过运行代码tf.reset_default_graph()来结束默认图。(为什么要reset?)

2.4 节点的生命周期

当我们需要计算一个节点的时候,TensorFlow会自动判断该节点所依靠的其他节点,并先计算所以靠的其他节点值。如下:

当我们需要计算y的值的时候,TensorFlow会自动检测到y的值依靠x,而x依靠w,并且会先计算w的值,再计算x的值,最后计算y的值。而当计算z值的时候会重复上面的步骤,并不会去调用计算y的时候计算出来的x的值。也就是说会计算两次w和x的值。

其实在不同的图之间运行,TensorFlow会释放掉所有的节点值(除了某些特殊值和变量值会被保留)。一个变量值的声明周期从它的initializer开始,到session结束。

我们知道上面的计算y和z的值需要两次计算w和x节点,但是,如果想更高效率的计算y和z的值的话,则需要将两个计算放到一个图中,如下:

其实在单线程中,多个session之间不共享任何信息,即使不同session计算同一个图(每一个session都有自己的变量copy)。在分布式的TensorFlow中变量信息存放在服务器中,而不是在session中,所以多session可以共享变量。

 

三. 线性回归TF实战

这里,我们将TensorFlow和用NumPy以及Scikit-Learn做一个比较,进而更加详细的了解TensorFlow。

3.1 TensorFlow线性回归

TensorFlow支持多输入多输出的操作,比方说相加和相差运算是两个输入一个输出,常数和变量是没有输入。这里的输入和输出都是多维数组,也称tensor。就像numpy中的数组,tensor有一个类型和尺寸。

之前的例子中,我们tensor只包含了单一数值,接下来,我们将用TensorFlow来实战一下线性回归这里我们用机器学习三人行系列(公众号回复“机器学习”进行查看)中的房价数据做回归,先上代码在讲解如下:

从上面代码中,我们可以看到,首先获取了房价的数据集,然后在每条数据的前面加1,为什么?,接着创建了两个常亮节点来承载数据和label,之后就是计算线性回归的系数。其实θ的计算是根据下面的公式计算出来的:

上面代码和直接用numpy直接计算θ的主要区别是在于如果我们的电脑上支持GPU计算的话,那么其将自动调用GPU资源运行。

3.2 NumPy线性回归

NumPy的方法,我们直接求取上面的公式如下:

从这结果里面,我们可以看到用TensorFlow和NumPy的方法得到的值是很相近的。

3.3 Scikit-Learn线性回归

关于Scikit-Learn线性回归的线性回归,这里不多说,详情查看机器学习系列文章。如下:

可以看到,三种方法得到的结果十分相近。

 

四. 本期小结

本期我们从TensorFlow的简介特性入手,熟悉了TensorFlow的相关流程,以及如何创建图,运行图的几种常见的方法,最后,我们通过TensorFlow的线性回归与NumPy的线性回归和Scikit-Learn之间的线性回归进行比较,来学习了TensorFlow在线性回归方面的应用。

 

(如需更好的了解相关知识,欢迎加入智能算法社区,在“智能算法”公众号发送“社区”,即可加入算法微信群和QQ群)

本文代码回复关键字:runtf

Guess you like

Origin blog.csdn.net/x454045816/article/details/92128060