赞
踩
神经网络的计算过程由正向传播(forward propagation )来进行前向计算来计算神经网络的输出以及反向传播(back propagation )计算来计算梯度(gradients)或微分(derivatives)。计算图(computation graph)解释了为什么以这种方式来组织。
计算图(computation graph)可以方便地展示神经网络的分层计算过程。
Learn to build a neural network with one hidden layer, using forward propagation and backpropagation.
Learning Objectives
Pros and cons of activation functions
Sigmoid和tanh函数的 缺点之一是 如果Z的值非常大或者非常小 那么关于这个函数导数的梯度或者 斜率会变的很小 当Z很大或者很小的时候 函数的斜率值 会接近零 这会使得梯度下降变的缓慢。
ReLU是目前广泛被人们使用的一个方法 虽然有时候 人们也会使用双曲函数 作为激活函数 ReLU的缺点之一是 当z为负数的时候 其导数为0,但在实际应用中并不是问题。ReLU和Leaky ReLU的共有的优势是 在z的数值空间里面 激活函数的导数或者说 激活函数的斜率 离0比较远 因此在实践当中使用普通的 ReLU激活函数的话 那么神经网络的学习速度通常会比使用 双曲函数tanh或者Sigmoid函数来的更快 主要原因是使学习变慢的斜率趋向0的现象 变少了 激活函数的导数趋向于0会降低学习的速度 我们知道,一半z的数值范围 ReLU的斜率为0 但是在实际使用中 大多数的隐藏单元的z值 将会大于0,因此学习仍然可以很快 。
不同激活函数的优缺点:
如果使用线性激活函数 或者叫恒等激活函数 那么神经网络的输出 仅仅是输入函数的线性变化。
如果使用线性激活函数 或者说 没有使用激活函数 那么无论你的神经网络有多少层 它所做的仅仅是计算线性激活函数 这还不如去除所有隐藏层。线性的隐藏层没有任何用处 因为两个线性函数的组合 仍然是线性函数 除非你在这里引入非线性函数 否则无论神经网络模型包含多少隐藏层 都无法实现更有趣的功能 只有一个地方会使用线性激活函数 当g(z)等于z 就是使用机器学习解决回归问题的时候。
About this Course
This course will teach you the “magic” of getting deep learning to work well. Rather than the deep learning process being a black box, you will understand what drives performance, and be able to more systematically get good results. You will also learn TensorFlow.
After 3 weeks, you will:
This is the second course of the Deep Learning Specialization.
Learning Objectives
Recall that different types of initializations lead to different results
Recognize the importance of initialization in complex neural networks.
Recognize the difference between train/dev/test sets
Diagnose the bias and variance issues in your model
Learn when and how to use regularization methods such as dropout or L2 regularization.
Understand experimental issues in deep learning such as Vanishing or Exploding gradients and learn how to deal with them
Use gradient checking to verify the correctness of your backpropagation implementation
What we want you to remember from this module:
- Regularization will help you reduce overfitting.
- Regularization will drive your weights to lower values.
- L2 regularization and Dropout are two very effective regularization techniques.
Deep Learning models have so much flexibility and capacity that overfitting can be a serious problem, if the training dataset is not big enough. Sure it does well on the training set, but the learned network doesn’t generalize to new examples that it has never seen!
The standard way to avoid overfitting is called L2 regularization. It consists of appropriately modifying your cost function, from:
为什么只对参数w进行正则化呢? 为什么我们不把b的相关项也加进去呢? 实际上也可以这样做 但通常会把它省略掉 因为w往往是一个非常高维的参数矢量 尤其是在发生高方差问题的情况下 可能w有非常多的参数 模型没能很好地拟合所有的参数 而b只是单个数字 几乎所有的参数都集中在w中 而不是b中 即使加上了最后这一项 实际上也不会起到太大的作用 因为b只是大量参数中的一个参数 在实践中通常就不费力气去包含它了 但如果想的话也可以(包含b)
L1 regulazation VS L2 regulazation
L1正则化 即不使用L2范数(Euclid范数(欧几里得范数,常用计算向量长度),即向量元素绝对值的平方和再开方) 而是使用lambda/m乘以这一项的和 这称为参数矢量w的L1范数(即向量元素绝对值之和) 这里有一个数字1的小角标 无论你在分母中使用m还是2m 它只是一个缩放常量 如果你使用L1正则化 w最后会变得稀疏 这意味着w矢量中有很多0 有些人认为这有助于压缩模型 因为有一部分参数是0 只需较少的内存来存储模型 然而在实践中发现 通过L1正则化让模型变得稀疏 带来的收效甚微 所以至少在压缩模型的目标上 它的作用不大
L2-regularization relies on the assumption that a model with small weights is simpler than a model with large weights. Thus, by penalizing the square values of the weights in the cost function you drive all the weights to smaller values. It becomes too costly for the cost to have large weights! This leads to a smoother model in which the output changes more slowly as the input changes.
Dropout is a widely used regularization technique that is specific to deep learning.
It randomly shuts down some neurons in each iteration. Watch these two videos to see what this means!
When you shut some neurons down, you actually modify your model. The idea behind drop-out is that at each iteration, you train a different model that uses only a subset of your neurons. With dropout, your neurons thus become less sensitive to the activation of one other specific neuron, because that other neuron might be shut down at any time.
What you should remember about dropout:
- Dropout is a regularization technique.
- You only use dropout during training. Don’t use dropout (randomly eliminate nodes) during test time.
- Apply dropout both during forward and backward propagation.
- During training time, divide each dropout layer by keep_prob to keep the same expected value for the activations. For example, if keep_prob is 0.5, then we will on average shut down half the nodes, so the output will be scaled by 0.5 since only the remaining half are contributing to the solution. Dividing by 0.5 is equivalent to multiplying by 2. Hence, the output now has the same expected value. You can check that this works even when keep_prob is other values than 0.5.
Learning Objectives
Remember different optimization methods such as (Stochastic) Gradient Descent, Momentum, RMSProp and Adam
Use random minibatches to accelerate the convergence and improve the optimization
Know the benefits of learning rate decay and apply it to your optimization
机器学习的应用是一个高度依赖经验的,不断重复的过程。 你需要训练很多模型才能找到一个确实好用的,所以能够快速的训练模型的确是个优势。令情况更艰难的是在大数据领域中深度学习表现得并不算完美,我们能够训练基于大量数据的神经网络,而用大量数据训练就会很慢,所以你会发现快速的优化算法,好的优化算法的确能大幅提高你和你的团队的效率。
我们之前学过,矢量化(vectorization)可以让你有效地计算所有m个样例而不需要一个具体的for循环就能处理整个训练集。是如果M非常大,速度依然会慢。例如, 如果M是5百万或者5千万或者更大,对你的整个训练集运用梯度下降法,你必须先处理你的整个训练集才能在梯度下降中往前一小步,然后再处理一次整个5百万的训练集才能再往前一小步。所以实际上算法是可以加快的,如果你让梯度下降在处理完整个巨型的5百万训练集之前就开始有所成效。具体来说,你可以这样做,将你的训练集拆分成更小的,微小的训练集,即小批量训练集(mini-batch)。
一次遍历(epoch)是指过一遍训练集,只不过在批量梯度下降法中对训练集的一轮处理只能得到一步梯度逼近,而小批量梯度下降法中对训练集的一轮处理,也就是一次遍历,可以得到5000步梯度逼近。
当你有一个大型训练集时,小批量梯度下降法比梯度下降法要快得多。这几乎是每个从事深度学习的人在处理一个大型数据集时会采用的算法。
A variant is Stochastic Gradient Descent (SGD), which is equivalent to mini-batch gradient descent where each mini-batch has just 1 example. In Stochastic Gradient Descent, you use only 1 training example before updating the gradients. When the training set is large, SGD can be faster. But the parameters will “oscillate” toward the minimum rather than converge smoothly. Here is an illustration of this:
In practice, you’ll often get faster results if you do not use neither the whole training set, nor only one training example, to perform each update. Mini-batch gradient descent uses an intermediate number of examples for each step. With mini-batch gradient descent, you loop over the mini-batches instead of looping over individual training examples.
有几个点需要注意 当beta的值很大的时候 你得到的曲线会更平滑 因为你对更多天数的温度做了平均处理 因此曲线就 波动更小 更加平滑 但另一方面 这个曲线会右移 因为你在一个更大的窗口内计算平均温度 通过在更大的窗口内计算平均 这个指数加权平均的公式 在温度变化时 适应地更加缓慢 这就造成了一些延迟 原因是 当beta=0.98的时候 之前的值具有更大的权重 而当前值的权重就非常小 只有0.02 所以当温度变化的时候 温度上升或者下降 这个指数加权平均 在beta较大时 就会适应得更慢 我们来试试另一个值 让beta的值取另一个极端 比如0.5 那么由右边的公式 这就变成了只对两天进行平均 如果画出来 就会得到黄色的线 由于仅仅平均两天的气温 即只在很小的窗口内计算平均 得到结果中会有更多的噪声 更容易受到异常值的影响 但它可以更快地适应温度变化 使用这个公式就可以实现指数加权平均 在统计学中 它被称为 指数加权滑动平均 我们把它简称为指数加权平均
One of the advantages of this exponentially weighted average formula, is that it takes very little memory. You just need to keep just one row number in computer memory, and you keep on overwriting it with this formula based on the latest values that you got. And it’s really this reason, the efficiency, it just takes up one line of code basically and just storage and memory for a single row number to compute this exponentially weighted average.
动量(Momentum) 或者叫动量梯度下降算法的主要思想是 计算梯度的指数加权平均 然后使用这个梯度来更新权重。
Because mini-batch gradient descent makes a parameter update after seeing just a subset of examples, the direction of the update has some variance, and so the path taken by mini-batch gradient descent will “oscillate” toward convergence. Using momentum can reduce these oscillations.
Momentum takes into account the past gradients to smooth out the update. We will store the ‘direction’ of the previous gradients in the variable
The momentum update rule is, for
where L is the number of layers,
How do you choose
The larger the momentum
Common values for
Tuning the optimal
Momentum takes past gradients into account to smooth out the steps of gradient descent. It can be applied with batch gradient descent, mini-batch gradient descent or stochastic gradient descent.
You have to tune a momentum hyperparameter
你已经学习了如何用动量来加速梯度下降 还有一个叫做RMSprop的算法 全称为均方根传递(Root Mean Square prop),它也可以加速梯度下降 我们来看看它是如何工作的 回忆一下之前的例子 在实现梯度下降时 可能会在垂直方向上出现巨大的振荡 即使它试图在水平方向上前进 为了说明这个例子 我们假设 纵轴代表参数b 横轴代表参数W 当然这里也可以是W1和W2等其他参数 我们使用b和W是为了便于理解 你希望减慢b方向的学习 也就是垂直方向 同时加速或至少不减慢水平方向的学习 这就是RMSprop算法要做的。另一个收效是 你可以使用更大的学习率alpha 学习得更快 而不用担心在垂直方向上发散
在水平方向上 即例子中W的方向上 我们希望学习速率较快 而在垂直方向上 即例子中b的方向上 我们希望降低垂直方向上的振荡 对于S_dW和S_db这两项 我们希望S_dW相对较小 因此这里除以的是一个较小的数 而S_db相对较大 因此这里除以的是一个较大的数 这样就可以减缓垂直方向上的更新 实际上 如果你看一下导数 就会发现垂直方向上的倒数要比水平方向上的更大 所以在b方向上的斜率很大 对于这样的导数 db很大 而dW相对较小 因为函数在垂直方向 即b方向的斜率 要比w方向 也就是比水平方向更陡 所以 db的平方会相对较大 因此S_db会相对较大 相比之下dW会比较小 或者说dW的平方会较小 所以S_dW会较小 结果是 垂直方向上的更新量 会除以一个较大的数 这有助于减弱振荡 而水平方向上的更新量会除以一个较小的数
Adam is one of the most effective optimization algorithms for training neural networks. It combines ideas from RMSProp (described in lecture) and Momentum.
How does Adam work?
The update rule is, for
where:
- t counts the number of steps taken of Adam
- L is the number of layers
-
-
-
在选择超参数的比例的时候,原则上应在不同比例范围内进行均匀随机取值,如 0.001~0.001 、 0.001~0.01 、 0.01~0.1 、 0.1~1 范围内选择。
一般地,如果在
同样,在使用指数加权平均的时候,超参数beta也需要用上面这种方向进行选择。
在计算资源有限的情况下,使用第一种,仅调试一个模型,每天不断优化;
在计算资源充足的情况下,使用第二种,同时并行调试多个模型,选取其中最好的模型。
在深度学习不断兴起的过程中 最重要的创新之一是一种 叫批量归一化 (Batch Normalization) 的算法 它由Sergey Ioffe 和 Christian Szegedy提出 可以让你的超参搜索变得很简单 让你的神经网络变得更加具有鲁棒性 可以让你的神经网络对于超参数的选择上不再那么敏感 而且可以让你更容易地训练非常深的网络。
Implementing Batch Norm
这里的
Batch norm所做的就是 不仅仅在输入层 而且在一些隐藏层上也做归一化 你使用这种归一化方法 对某些隐藏单元的值z做归一化 但是输入层和隐藏层的归一化还有一点不同 就是隐藏层归一化后并不一定是均值0方差1 比如 如果你的激活函数是sigmoid 你就不希望归一化后的值都聚集在这里 可能希望它们有更大的方差 以便于更好的利用s函数非线性的特性 而不是所有的值都在中间这段近似直线的区域上 这就是为什么通过设置
通常的方法就是在我们训练的过程中,对于训练集的Mini-batch,使用指数加权平均,当训练结束的时候,得到指数加权平均后的均值和方差,而这些值直接用于Batch Norm公式的计算,用以对测试样本进行预测。
Softmax回归是一种更普遍的逻辑回归的方法。这种方法能够让你试图预测多分类问题,而不仅仅是二分类问题。
总结一下从
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。