Depth learning algorithm (No. 3) ---- TensorFlow start from DNN

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

Depth study of three lines (No. 3) ---- TensorFlow start from DNN

We have learned some basics TensorFlow, which will start with DNN period we started to learn the knowledge TensorFlow area. The way of learning, we are more exchanges and common progress. In this issue as follows:

  • From biology to artificial neural networks
  • Training Multilayer Perceptron
  • DNN training

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

 

A. From biology to artificial neural networks

Birds inspire us to fly, burdock plants inspired by Velcro, and nature inspired many other inventions. So, the architecture of the brain, is the key idea inspired artificial neural network (ANN) is. Artificial neural network is a core depth of learning.

1.1 from biological to artificial neurons

Before discussing artificial neurons, let's take a quick look at the biological neurons, as shown below. It is a strange looks cells, primarily in the cortex of animals (e.g., your brain), and from the cell nucleus and most of the cells containing complex components and many branches called dendrites extending portion composition, there is a very long extension, called axons. The length of the axon may be several times longer than the cell body, or up to several times. Split near the axon terminals thereof into a plurality of branches called dendrites end, and the tip of these branches are tiny structures called synaptic terminals (or simply synapses), which is connected to the dendrites (or directly to the cell body) of other neurons. Biological neuron receives a short electric pulse signals from other neurons through these synapses. When a sufficient number of other neurons from neuron receives a signal within a few milliseconds, it will trigger its own signal.

Action tf.assign () function is to create a new node to a value assigned to the variable, here corresponding to perform the following iteration:

Thus, a single biological neural seems to behave quite simple, but they are organized in a vast network of billions of neurons, each neuron is usually connected with thousands of other neurons when the situation is different. Biological neural network (BNN) architecture is still the subject of active research, some part of the structure of the brain has been printed out, it seems that neurons generally continuous levels of the organization, as shown below.

II. Training Multilayer Perceptron

Warren McCulloch and Walter Pitts presents a very simple biological neuron model, which came to be known artificial neurons: it has one or more binary (on / off) input and a binary output. When more than a certain number of valid input, of artificial neurons will simply activates its output. McCulloch and Pitts showed that, with such a simplified model, we can create an artificial neural network, compute any logical proposition you want. For example, assume neuronal activation least two input neurons valid, you can build some of the various logical operations performed on ANN, as shown in FIG.

Perceptron is one of Frank Rosenblatt in 1957 invention of the simplest ANN architecture. It is based on a slightly different artificial neurons (see below), is called linear threshold units (LTU): the digital input and output is now (instead of binary on / off values), each input are connected to a weight. Computing a weighted LTU and its input (z = w1 x1 + w2 x2 + ⋯ + wn xn = wT · x), and then apply the step function and output: hw (x) = step (z) = step ( wT · x).

Used in the perceptron is the most commonly used Heaviside step function (see the following equation). Sometimes used in place of the sign function.

Only a single layer of a single layer perceptron LTU, each neuron is connected to all inputs, usually add an extra bias characteristic (x0 = 1). Having two inputs and three outputs of the perceptron as shown in FIG. This perception may be divided into three different instances at the same binary class, which makes it more than one output sorter.

A MLP consists of a (direct) input layer, one or more layers LTU, called hidden layer, the last layer LTU called output layer (shown below). Other than the output layer, each layer comprising a bias neuron connected to the lower layer and completely. When the ANN with two or more hidden layers, it is referred to as depth Neural Network (DNN).

多年来,研究人员一直在努力寻找一种培训MLP的方法,但没有成功。但在1986年,D. E. Rumelhart 等人。发表了一篇突破性文章,介绍反向传播训练算法。对于每个训练实例,算法将其输入到网络并计算每个神经元的输出(这是正向传递,就像进行预测时一样)。 然后它计算网络的输出误差(即期望的输出和网络的实际输出之间的差异),并且它计算最后一个隐藏层中的每个神经元对每个输出神经元的误差有多大贡献。然后继续测量这些误差贡献中多少来自前一个隐藏层中的每个神经元 - 等等直到算法到达输入层。

为了使这个算法正常工作,作者对MLP的架构做了一个关键的改变:他们用logistic函数σ(z)= 1 /(1 + exp(-z))代替了阶跃函数。 这是很重要的,因为阶跃函数只包含平坦段,所以没有梯度可用(梯度下降不能在平坦表面上移动),而logistic函数在每个地方都有一个定义明确的非零导数,允许渐变下降 每一步都有进步。 反向传播算法可以与其他激活函数一起使用,而不是逻辑函数。 其他两种流行的激活功能是:

1.双曲正切函数tanh(z)=2σ(2z) - 1

它是S形的,连续的,可微分的,但是它的输出值范围从-1到1(而不是在逻辑函数中为0到1),这往往会使每一层的输出更大或训练开始时标准化程度较低(即以0为中心)。

2.ReLU功能

ReLU(z)= max(0,z)。 它是连续的,但不幸的是它在z = 0时不可微分(斜率突然变化,这可能导致梯度下降反弹)。实际上它运行得非常好,并且具有快速计算的优点。

这些流行的激活函数及其衍生物如图下图所示。

MLP通常用于分类,每个输出对应于不同的二进制类(例如,垃圾邮件/火腿,紧急/不紧急等等)。当这些类是排他性的(例如,数字图像分类的类0到9)时,输出层通常通过用共享的softmax函数代替单独的激活函数(见下图)。 softmax函数在机器学习系列中介绍过。每个神经元的输出对应于相应类的估计概率。 请注意,信号仅在一个方向上(从输入到输出)流动,所以此架构是前馈神经网络(FNN)的一个示例。

使用TensorFlow训练MLP的最简单方法是使用高级API TF.Learn,它与Scikit-Learn的API非常相似。 DNNClassifier类使得使用任意数量的隐藏层训练深层神经网络和softmax输出层来输出估计类别概率变得十分简单。 例如,下面的代码训练一个DNN用于分类两个隐藏层(一个具有300个神经元,另一个具有100个神经元)以及一个具有10个神经元的softmax输出层:

如果您在MNIST数据集上运行此代码(在对其进行缩放后(例如,通过使用ScikitLearn的StandardScaler),您可能会得到一个在测试集上的准确率超过98.2%的模型! 这比我们在机器学习系列文章的模型中训练的最佳模型要好:

代码过长,详细代码请移步公众号“智能算法”回复文末关键字下载。

 

三. 训练DNN

这里我们将实现Minibatch渐变下降以在MNIST数据集上进行训练。 第一步是构建阶段,构建TensorFlow图。 第二步是执行阶段,您可以在其中实际运行图来训练模型。

3.1 构建阶段

首先,我们需要导入tensorflow库。 然后,我们必须指定输入和输出的数量,并设置每层中隐藏的神经元的数量:

接下来,我们可以使用占位符节点来表示训练数据和目标。 X的形状只是部分定义的。 我们知道它将是一个二维张量(即矩阵),沿第一维的实例和沿第二维的特征,并且我们知道特征的数量将是28 x 28(每像素一个特征) ,但我们还不知道每个培训批次将包含多少个实例。 所以X的形状是(None,n_inputs)。 同样,我们知道y将是每个实例有一个入口的一维张量,但是在这一点上我们也不知道训练批量的大小,因此形状是(无)。

现在我们来创建实际的神经网络。 占位符X将充当输入层; 在执行阶段,它将一次替换为一个训练批次(请注意,训练批次中的所有实例都将由神经网络同时处理)。 现在你需要创建两个隐藏层和输出层。 这两个隐藏层几乎是相同的:它们的区别仅在于它们所连接的输入以及它们包含的神经元的数量。 输出层也非常相似,但它使用softmax激活功能而不是ReLU激活功能。 因此,让我们创建一个我们将用来一次创建一个图层的neuron_layer()函数。 它将需要参数来指定输入,神经元的数量,激活函数和图层的名称:

让我们一行一行地看看这段代码:

1.首先我们使用图层的名称创建一个名称范围:它将包含该神经元图层的所有计算节点。 这是可选的,但如果TensorBoard中的节点组织良好,该图形在TensorBoard中看起来会更好。

2.接下来,我们通过查询输入矩阵的形状并获得第二维的大小(第一维是实例)来获得输入的数量。

3.接下来的三行创建一个W变量,它将保存权重矩阵。 它将是一个二维张量,其中包含每个输入和每个神经元之间的所有连接权重; 因此,它的形状将是(n_inputs,n_neurons)。 它将被随机初始化,使用标准偏差为2 / ninputs的截断法线(高斯)分布。 使用这个特定的标准偏差有助于算法更快地收敛。 为所有隐藏层随机初始化连接权重非常重要,以避免梯度下降算法无法打破的任何对称性。

4.下一行为偏差创建一个b变量,初始化为0(在这种情况下不存在对称性问题),每个神经元具有一个偏置参数。

5.然后我们创建一个子图来计算z = X·W + b。 这种向量化的实现将有效地计算输入的加权和加上层中每个神经元的偏置项,对于批处理中的所有实例,只需一次。

6.最后,如果激活参数设置为“relu”,则代码返回relu(z)(即,max(0,z)),否则它只返回z。

好的,现在你有一个很好的功能来创建一个神经元层。 让我们用它来创建深度神经网络! 第一个隐藏层将X作为输入。 第二个将第一个隐藏层的输出作为输入。 最后,输出层将第二个隐藏层的输出作为输入。

请注意,为了清晰起见,我们再次使用名称范围。 还要注意,在通过softmax激活函数之前,logits是神经网络的输出:出于优化原因,我们稍后将处理softmax计算。

正如你所期望的那样,TensorFlow具有许多方便的功能来创建标准的神经网络图层,所以通常不需要像我们刚才那样定义自己的神经元层()函数。 例如,TensorFlow的dense()函数创建一个完全连接的层,其中所有输入连接到该层中的所有神经元。 只需导入该函数并用以下代码替换dnn构造部分:

现在我们已经准备好了神经网络模型,我们需要定义我们将用来训练它的代价函数。我们将使用交叉熵,交叉熵会惩罚估计目标类别的概率较低的模型。 TensorFlow提供了几个函数来计算交叉熵。 我们将使用sparse_softmax_cross_entropy_with_logits()。 这将给我们一个包含每个实例的交叉熵的一维张量。然后,我们可以使用TensorFlow的reduce_mean()函数来计算所有实例的平均交叉熵。

我们有神经网络模型,我们有代价函数,现在我们需要定义一个GradientDescentOptimizer来调整模型参数以最小化代价函数:

构造阶段的最后一个重要步骤是指定如何评估模型。我们可以使用in_top_k()函数。 这将返回一个布尔值为1D的张量,所以我们需要将这些布尔值转换为浮点数,然后计算平均值。 这会给我们网络的整体准确性。

我们需要创建一个节点来初始化所有变量,并且我们还将创建一个Saver以将我们训练好的模型参数保存到磁盘:

3.2 构建阶段

这部分更短,更简单。 首先,我们加载MNIST。 我们可以使用ScikitLearn,但TensorFlow提供了自己的帮助程序,它可以提取数据,对数据进行缩放(0到1之间),对其进行混洗,并提供一个简单的函数来一次加载一个小批量。 所以让我们用它来执行:

此代码打开TensorFlow会话,并运行初始化所有变量的init节点。 然后它运行主要的训练循环:在每个时代,代码迭代对应于训练集大小的许多小批量。 每个小批量都通过next_batch()方法获取,然后代码简单地运行训练操作,为其提供当前的最小批量输入数据和目标。 接下来,在每个迭代结束时,代码将在最后一个小批量和完整训练集上评估模型,并打印出结果。 最后,模型参数保存到磁盘。

3.3 使用神经网络

现在神经网络已经过训练,您可以使用它来进行预测。 要做到这一点,你可以重复使用相同的构造阶段,但像这样改变执行阶段:

首先代码从磁盘加载模型参数。 然后它加载一些你想分类的新图像。然后代码评估logits节点。 如果你想知道所有估计的类概率,你需要将softmax()函数应用于logits,但是如果你只是想预测一个类,你可以简单地选择具有最高logit值的类(使用 argmax()函数执行这个技巧)。

五. 本期小结

至此,我们了解了神经网络的由来,并且训练了多层感知机以及DNN神经网络。

 

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

 

Guess you like

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