stanford_CS231n_learning note_Lec_04 Backpropagation
来源:互联网 发布:好易网络电视安卓版 编辑:程序博客网 时间:2024/05/14 16:23
Lec_4_Back Propagation
Happy Moment:如果这是科目二考试,你会怎么办?
- cs231n Schedule and Syllabus 教学计划与大纲
http://cs231n.stanford.edu/syllabus.html - 网易云课堂
http://study.163.com/course/courseMain.htm?courseId=1003223001 - backprop notes
http://cs231n.github.io/optimization-2/
Review
- score function:
s=f(x;W)=Wx - SVM loss:
Li=∑j≠yimax(0,sj−syi+1) - data loss + regularization:
L=1N∑Ni=1Li+∑kW2k - want
∇WL - Optimization
- Gradient Descent
df(x)dx=lim infh→0f(x+h)−f(x)h - Numerical gradient: slow :(, approximate :(, easy to write :)
- Analytic gradient: fast :), exact :), error-prone :(
- In practice: Derive analytic gradient, check your implementation with numerical gradient
Gradient Descent
While condition is True: 前馈:forward 反馈:backward 更新:update
- 梯度计算想到该图,而非复杂的表达式 重点学习如何在这些计算图中,结合输入和梯度来得到最终的损失函数。
Computational Graph
- Simple expressions and interpretation of the gradient
Interpretation: Keep in mind what the derivatives tell you: They indicate the rate of change of a function with respect to that variable surrounding an infinitesimally small region near a particular point.
The derivative on each variable tells you the sensitivity of the whole expression on its value.
A simple example
-
- want
# set some inputsx = -2; y = 5; z = -4# perform the forward passq = x + y # q becomes 3f = q * z # f becomes -12# perform the backward pass (backpropagation) in reverse order:# first backprop through f = q * zdfdz = q # df/dz = q, so gradient on z becomes 3dfdq = z # df/dq = z, so gradient on q becomes -4# now backprop through q = x + ydfdx = 1.0 * dfdq # dq/dx = 1. And the multiplication here is the chain rule!dfdy = 1.0 * dfdq # dq/dy = 1
Chain rule
- Chain rule 链式规则:
∂f∂x=∂f∂q∂q∂x 局部梯度*计算点处的梯度[local gradient][its gradient] - q是中间变量,f是q与z的乘积
- 红色表示梯度值,绿色表示变量值
- 梯度值>0表示正比于
- 梯度值<0表示负比于
- 反向算法在计算时,链式法则的自变量的取值为输入的值
- 反向算法在计算时,链式法则的自变量的取值为输入的值
- 反向算法在计算时,链式法则的自变量的取值为输入的值
Backprop in practice: Staged computation
- 大的函数也可以直接是做一个整体计算梯度:(1-sigmoid(x))*sigmoid(x)局部梯度
- Three kinds of common derivative expression:
f(x,y)=x+y→∂f∂x=1;∂f∂y=1 f(x,y)=xy→∂f∂x=y;∂f∂y=x f(x,y)=max(x,y)→∂f∂x=1(x≥y);∂f∂y=1(y≥x)
Implementation: forward/backward API
所以当你正真学习这个网络,每一个门都需要记住输入值和其他出现过的中间微分值,所以当我们跑完整个网络时,要把大量的数据值存储下来,因为在反向传播中可能会用到某些变量,所以正向传播时会存储大量数据,反向过程中又会用掉。我们实际上是利用那些中间值进行正确的反向运算。如果你不进行反向传播运算,你可以删除很多东西。假如你用的是嵌入式设备,确实很担心内存问题
通过前馈得到损失,通过反馈得到梯度,通过对梯度的实用来完成权值更新,这就是神经网络训练过程中内部变化:
- 前馈、反馈、更新
- forward–backward-update
Gradients for vectorized code
- Vetorized Operations
# forward passW = np.random.randn(5, 10)X = np.random.randn(10, 3)D = W.dot(X)# now suppose we had the gradient on D from above in the circuitdD = np.random.randn(*D.shape) # same shape as DdW = dD.dot(X.T) #.T gives the transpose of the matrixdX = W.T.dot(dD)
- Question—0: 为什么不需要求Jacobian Matrix?
- Question—1: what is the size of the Jacobian matrix?
- Question—2: What does it look like?
Answer-0:4096*4096的矩阵只有对角线上有值,因为进行的是单个元素的操作。
另外,对角线上不全是1。由于有些小于0的数据要提升到0,所以对角线上有些1倍0替代这里的雅克比矩阵基本上式单位阵,只是有一些1被替换成了0。所以求出完整的雅克比矩阵是不可取的,你也不会真正将矩阵与向量相乘。
Answer-1: 4096x4096:原则上每一个输入值都将影响每一个输出值,但事实上并不是那样。Jacobina有一个特殊的结构。
Answer-2: 最终得到的损失函数只是一个标量。
Summary-1
- neural nets will be very large: no hope of writing down gradient formula by hand for all parameters
- backpropagation = recursive application of the chain rule along a computational graph to compute the gradients of all
inputs/parameters/intermediates- implementations maintain a graph structure, where the nodes implement the forward() / backward() API.
- forward: compute result of an operation and save any intermediates needed for gradient computation in memory
- backward: apply the chain rule to compute the gradient of the loss function with respect to the inputs.
Neural Network
What’s NN for this class?
细胞主体这里执行了一个操作,把所有输入求和再加上偏置,后面轴突上有一个激活方程,数据经过激活方程,得到这个神经元的输出,生物学模型里,历史上的人习惯使用sigmoid函数作为激活方程,理由是结果位于[0,1],可,对于某个输入的处理比率,所以激活函数得到的是一个比率值,如果这个神经元在上个神经元的输出里找到一些它欢的东西,它会把数值上涨很多,具体可以在f的结果里表示出来(比率,概率较大)。
The natural NN and our NN
一个神经元执行函数,向前传递。它会接受输入,输入是一个向量,在细胞体里我么执行加和,只是一个线性相加,我们用这个sigmoid函数当做细胞体上的激发速率,而这个函数的返回值可以传播到其他的神经元上,此处,每一个单个神经元都可以看作是一个小的线性分类器,只是这些神经元都是彼此相连的,它可以在一起工作。
Explain for NN
生物学上神经元要比0.1神经元复杂得多:不同功能,不同树突的神经元,这些突触是复杂的动态系统。这里有一些分线性函数供我们选择,当然历史上研究者们也用过很多其他的函数,此处仅作简单介绍,后面再详细介绍每种激活函数的优缺点。机器学习的历史上,人们一开始使用的是tanh函数,到2012年,这个ReLu函数开始流行,因为这个函数可以是你的神经网络快很多,所以现在大家如果想要一个非线性函数的默认选择,推荐ReLU。Leaky ReLU是最近几年被提出来的Maxout也是一个有趣的函数,最近又提出了ELU,所以大家也可以提出不同的激活函数,使用不同的激活函数描述自己的神经网络,来优化神经网络,关于激活函数的细节我们会在后面的课程中详细讲解。
Activation Functions
分层设置更加高效,与其设置大量的没有明确组织结构的神经元,并让这些神经元独立计算,我们更倾向于吧哲别神经元安排在一个个层中,使得我们可以进行向量化运算,我们可以在矩阵乘法中完成一个隐藏层中所有神经元的计算,这就是我们把它们分层安排的原因,在层中我们可以并行的完成,所有的计算,每一个隐含层中的神经元有相同的输入。换句话说:分层话就是一个计算技巧,屏幕上的是一个三层神经网络。下面的代码就是使用一种激活函数来实现这种层间矩阵乘法的代码。
Neural Networks: Architectures
Demo of a Neurons Network
http://cs.stanford.edu/people/karpathy/convnetjs/demo/classify2d.htm
- 下面用一个demo来展示一下神经网络是怎么工作的:神经网络决定红色绿色的分类边界。如图,使用数据来训练神经网络时,隐含层中的神经元越多,分类效果越好,图像越扭曲,计算函数也越复杂,参见图【NN层数与分类效果】
- 正则化系数
Summary-2
- we arrange neurons into fully-connected layers
- the abstraction of a layer has the nice property that it allows us to use efficient vectorized code (e.g. matrix multiplies)
- neural networks are not really neural
- neural networks: bigger = better (but might have to regularize more strongly)
Q&A:我们把神经元放在神经网络的全连接层中,他们并不是真正的神经元,神经网络越大越大用,同时也需要加入合适的正则化系数,防止神经网络的过拟合,而不是减少神经元。Question:怎么样劝和深度和宽度(层数与每层神经元数)?没有较好的答案。Q:层数与深度哪个重要?A:与数据类型有关:文本-图像-声音…图像与深度关系较大,不同的层使用不同的激活函数,一般是做不到的。大部分情况下,我们都是选择一种激活函数,然后整个神经网络都用这个,实际操作中,我们经常用的手动式ReLU,整个神经网络都用这一个,其实在不同层中换激活函数并没有什么好处,我们一般都不会这么做。
Next Lecture
More than you ever wanted to know about Neural Networks and how to train them.
- stanford_CS231n_learning note_Lec_04 Backpropagation
- Backpropagation
- backpropagation
- Backpropagation
- Backpropagation
- Backpropagation
- backpropagation
- stanford_CS231n_learning note_Lec_01 Introduction
- stanford_CS231n_learning note_Lec_02 Image Classification pipeline
- Backpropagation, Intuitions
- BackPropagation算法
- [Machine_Learning]--Backpropagation
- backpropagation algorithm
- backpropagation,intuitions
- Backpropagation Algorithm
- Backpropagation算法
- Resilient Backpropagation
- stanford_CS231n_learning note_Lec_03 Loss function and optimization
- Android|图形图像之Volley
- SQL Server 根据分隔符依次截取每个子字符串
- JAVA并发手册
- computer E 第一期d1.1.1-Topic
- 常用查找数据结构及算法(Python实现)
- stanford_CS231n_learning note_Lec_04 Backpropagation
- nutch与solr集成备忘录
- android 图形图像之glide
- require.js与sea.js区别
- OKHttp 同步请求
- [LeetCode]446. Arithmetic Slices II
- Android跑马灯效果
- 设备模型七(class)
- linux服务器查看公网IP信息的方法