5 Neural Networks (part two)

content:

  5 Neural Networks (part two)

    5.1 cost function

    5.2 Back Propagation

    5.3 神经网络总结

接上一篇4. Neural Networks (part one). 本文将先定义神经网络的代价函数,然后介绍逆向传播(Back Propagation: BP)算法,它能有效求解代价函数对连接权重的偏导,最后对训练神经网络的过程进行总结。

5.1 cost function

(注:正则化相关内容参见3.Bayesian statistics and Regularization)

5.2 Back Propagation

(详细推导过程参见反向传播算法)。

图5-1 BP算法步骤

在实现反向传播算法时,有如下几个需要注意的地方。

  1. 需要对所有的连接权重(包括偏移单元)初始化为接近0但不全等于0的随机数。如果所有参数都用相同的值作为初始值,那么所有隐藏层单元最终会得到与输入值有关的、相同的函数(也就是说,所有神经元的激活值都会取相同的值,对于任何输入x 都会有:  )。随机初始化的目的是使对称失效。具体地,我们可以如图5-2一样随机初始化。(matlab实现见后文代码1)
  2. 如果实现的BP算法计算出的梯度(偏导数)是错误的,那么用该模型来预测新的值肯定是不科学的。所以,我们应该在应用之前就判断BP算法是否正确。具体的,可以通过数值的方法(如图5-3所示的)计算出较精确的偏导,然后再和BP算法计算出来的进行比较,若两者相差在正常的误差范围内,则BP算法计算出的应该是比较正确的,否则说明算法实现有误。注意在检查完后,在真正训练模型时不应该再运行数值计算偏导的方法,否则将会运行很慢。(matlab实现见后文代码2)
  3. 用matlab实现时要注意matlab的函数参数不能为矩阵,而连接权重为矩阵,所以在传递初始化连接权重前先将其向量化,再用reshape函数恢复。(见后文代码3)

图5-2 随机初始化连接权重

图5-3 数值方法求代价函数偏导的近似值

5.3 神经网络总结

第一步,设计神经网络结构。

第二步,实现正向传播(FP)和反向传播算法,这一步包括如下的子步骤。

第三步,用数值方法检查求偏导的正确性

第四步,用梯度下降法或更先进的优化算法求使得代价函数最小的连接权重

在第四步中,由于代价函数是非凸(non-convex)函数,所以在优化过程中可能陷入局部最优值,但不一定比全局最优差很多(如图5-4),在实际应用中通常不是大问题。也会有一些启发式的算法(如模拟退火算法,遗传算法等)来帮助跳出局部最优。

图5-4 陷入局部最优(不一定比全局最优差很多)

代码1:随机初始化连接权重

function W = randInitializeWeights(L_in, L_out)
%RANDINITIALIZEWEIGHTS Randomly initialize the weights of a layer with L_in
%incoming connections and L_out outgoing connections
%   W = RANDINITIALIZEWEIGHTS(L_in, L_out) randomly initializes the weights
%   of a layer with L_in incoming connections and L_out outgoing
%   connections.
%
%   Note that W should be set to a matrix of size(L_out, 1 + L_in) as
%   the column row of W handles the "bias" terms
%W = zeros(L_out, 1 + L_in);% Instructions: Initialize W randomly so that we break the symmetry while
%               training the neural network.
%
% Note: The first row of W corresponds to the parameters for the bias units
%epsilon_init = sqrt(6) / (sqrt(L_out+L_in));
W = rand(L_out, 1 + L_in) * 2 * epsilon_init - epsilon_init;end

View Code

代码2:用数值方法求代价函数对连接权重偏导的近似值

function numgrad = computeNumericalGradient(J, theta)
%COMPUTENUMERICALGRADIENT Computes the gradient using "finite differences"
%and gives us a numerical estimate of the gradient.
%   numgrad = COMPUTENUMERICALGRADIENT(J, theta) computes the numerical
%   gradient of the function J around theta. Calling y = J(theta) should
%   return the function value at theta.% Notes: The following code implements numerical gradient checking, and
%        returns the numerical gradient.It sets numgrad(i) to (a numerical
%        approximation of) the partial derivative of J with respect to the
%        i-th input argument, evaluated at theta. (i.e., numgrad(i) should
%        be the (approximately) the partial derivative of J with respect
%        to theta(i).)
%                numgrad = zeros(size(theta));
perturb = zeros(size(theta));
e = 1e-4;
for p = 1:numel(theta)% Set perturbation vectorperturb(p) = e;% Compute Numerical Gradientnumgrad(p) = ( J(theta + perturb) - J(theta - perturb)) / (2*e);perturb(p) = 0;
end
end

View Code

代码3:应用FP和BP算法实现计算隐藏层为1层的神经网络的代价函数以及其对连接权重的偏导数

function [J grad] = nnCostFunction(nn_params, ...input_layer_size, ...hidden_layer_size, ...num_labels, ...X, y, lambda)
%NNCOSTFUNCTION Implements the neural network cost function for a two layer
%neural network which performs classification
%   [J grad] = NNCOSTFUNCTON(nn_params, hidden_layer_size, num_labels, ...
%   X, y, lambda) computes the cost and gradient of the neural network. The
%   parameters for the neural network are "unrolled" into the vector
%   nn_params and need to be converted back into the weight matrices.
%
%   The returned parameter grad should be a "unrolled" vector of the
%   partial derivatives of the neural network.
%% Reshape nn_params back into the parameters Theta1 and Theta2, the weight matrices
% for our 2 layer neural network:Theta1: 1->2; Theta2: 2->3
Theta1 = reshape(nn_params(1:hidden_layer_size * (input_layer_size + 1)), ...hidden_layer_size, (input_layer_size + 1));Theta2 = reshape(nn_params((1 + (hidden_layer_size * (input_layer_size + 1))):end), ...num_labels, (hidden_layer_size + 1));% Setup some useful variables
m = size(X, 1);
J = 0;
Theta1_grad = zeros(size(Theta1));
Theta2_grad = zeros(size(Theta2));%         Note: The vector y passed into the function is a vector of labels
%               containing values from 1..K. You need to map this vector into a
%               binary vector of 1's and 0's to be used with the neural network
%               cost function.for i = 1:m% compute activation by Forward Propagationa1 = [1; X(i,:)'];z2 = Theta1 * a1;a2 = [1; sigmoid(z2)];z3 = Theta2 * a2;h = sigmoid(z3);yy = zeros(num_labels,1);yy(y(i)) = 1;              % 训练集的真实值yyJ = J + sum(-yy .* log(h) - (1-yy) .* log(1-h));% Back Propagation delta3 = h - yy;delta2 = (Theta2(:,2:end)' * delta3) .* sigmoidGradient(z2); %注意要除去偏移单元的连接权重Theta2_grad = Theta2_grad + delta3 * a2';   Theta1_grad = Theta1_grad + delta2 * a1';
endJ = J / m + lambda * (sum(sum(Theta1(:,2:end) .^ 2)) + sum(sum(Theta2(:,2:end) .^ 2))) / (2*m);Theta2_grad = Theta2_grad / m;
Theta2_grad(:,2:end) = Theta2_grad(:,2:end) + lambda * Theta2(:,2:end) / m; % regularized nnTheta1_grad = Theta1_grad / m;
Theta1_grad(:,2:end) = Theta1_grad(:,2:end) + lambda * Theta1(:,2:end) / m; % regularized nn% Unroll gradients
grad = [Theta1_grad(:) ; Theta2_grad(:)];end

Stanford机器学习笔记-5.神经网络Neural Networks (part two)相关推荐

  1. Stanford机器学习笔记-4. 神经网络Neural Networks (part one)

    4. Neural Networks (part one) Content: 4. Neural Networks (part one) 4.1 Non-linear Classification. ...

  2. 吴恩达机器学习笔记week8——神经网络 Neutral network

    吴恩达机器学习笔记week8--神经网络 Neutral network 8-1.非线性假设 Non-linear hypotheses 8-2.神经元与大脑 Neurons and the brai ...

  3. Stanford机器学习---第五讲. 神经网络的学习 Neural Networks learning

    原文见http://blog.csdn.net/abcjennifer/article/details/7758797,加入了一些自己的理解 本栏目(Machine learning)包含单參数的线性 ...

  4. cs224w(图机器学习)2021冬季课程学习笔记9 Graph Neural Networks 2: Design Space

    诸神缄默不语-个人CSDN博文目录 cs224w(图机器学习)2021冬季课程学习笔记集合 文章目录 1. A General Perspective on Graph Neural Networks ...

  5. 【论文笔记】Progressive Neural Networks 渐进式神经网络

    Progressive NN Progressive NN是第一篇我看到的deepmind做这个问题的.思路就是说我不能忘记第一个任务的网络,同时又能使用第一个任务的网络来做第二个任务. 为了不忘记之 ...

  6. 【深度学习笔记】cs231n neural networks 3

    cs231n neural networks 3笔记 Gradient checks 1. Use the centered formula 2. Use relative error for the ...

  7. 机器学习笔记(五)神经网络

    5.神经网络 5.1神经元模型 神经网络是由具有适应性的简单单元组成的广泛并行互连的网络,它的组织能够模拟生物神经系统对真实世界物体所作出的交互反应.对这句话的理解,简单提要下,主角是简单单元(输入单 ...

  8. 初识:神经网络(Neural Networks)

    浅谈Neural Networks 神经网络介绍 神经网络原理 感知机 激活函数 Sigmoid 函数 双曲正切函数(tanh) 后序 神经网络介绍 人工神经网络(Artificial Neural ...

  9. 吴恩达机器学习笔记 —— 10 神经网络参数的反向传播算法

    http://www.cnblogs.com/xing901022/p/9350271.html 本篇讲述了神经网络的误差反向传播以及训练一个神经网络模型的流程 更多内容参考 机器学习&深度学 ...

最新文章

  1. 电脑开机显示Invalidsystemdisk
  2. python编写性别比例_Python分析微信好友性别比例和省份城市分布比例的方法示例【基于itchat模块】...
  3. 图神经网络三剑客:GCN、GAT与GraphSAGE
  4. js中window.onload 与 jquery中$(document.ready()) 测试
  5. python下载网页歌词_python3个人学习笔记-批量下载分析歌词2
  6. linux如何杀死进程最快,如何在Linux系统中杀掉内存消耗最大的进程?
  7. python周期执行-用Python执行周期性动作
  8. ResourceExhaustedError 解决方案
  9. 【新书推荐】【2020】卫星通信(第三版)
  10. 不能将下载行为传输到IDM(亲测有效)
  11. 新人服务器上快速简单搭建cs
  12. 基于SpringBoot 在线答题系统 含小程序!
  13. 联想小新pro16和联想小新pro14 2022款哪个好
  14. APP开发从需求到产品—APP产品经理成长日记
  15. lo linux 环回端口,本地环回接口lo The Loopback Network Interface lo--用Enki学Linux系列(2)...
  16. GAN相关模型和论文
  17. 3、Nginx系列之: location和alias的区别
  18. Python实现世界人口地图
  19. IAR一键更新项目文件树及include路径 IAR项目版本降级
  20. 小微企业信用评级方法

热门文章

  1. 编译Tomcat9源码【转】
  2. 携程基于云的软呼叫中心及客服平台架构实践
  3. Java 8 Stream API详解--转
  4. WebSocket 实战--转
  5. 对request.getSession(false)的理解(附程序员常疏忽的一个漏洞)--转
  6. 孙宏斌谈贾跃亭哽咽:“我一定把乐视做成一个好公司”
  7. 【深度学习看手相】台湾学生获奖 AI 项目是科学还是伪科学? 搜狐科技 08-06 12:44 1新智元报道 来源: medium,facebook 编译:胡祥杰 张易 【新智元导读】本周日带来一个有
  8. 文本深度表示模型Word2Vec 简介 Word2vec 是 Google 在 2013 年年中开源的一款将词表征为实数值向量的高效工具, 其利用深度学习的思想,可以通过训练,把对文本内容的处理简
  9. python模块xlwt怎么用不了_python中使用 xlwt 操作excel的常见方法与问题
  10. Java 8 - 收集器Collectors