标签:
content:
5 Neural Networks (part two)
5.1 cost function
5.2 Back Propagation
5.3 神经网络总结
接上一篇4. Neural Networks (part one). 本文将先定义神经网络的代价函数,然后介绍逆向传播(Back Propagation: BP)算法,它能有效求解代价函数对连接权重的偏导,最后对训练神经网络的过程进行总结。
(注:正则化相关内容参见3.Bayesian statistics and Regularization)
(详细推导过程参见反向传播算法)。
图5-1 BP算法步骤
在实现反向传播算法时,有如下几个需要注意的地方。
图5-2 随机初始化连接权重
图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
代码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 vector perturb(p) = e; % Compute Numerical Gradient numgrad(p) = ( J(theta + perturb) - J(theta - perturb)) / (2*e); perturb(p) = 0; end end
代码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 Propagation a1 = [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; % 训练集的真实值yy J = 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‘; end J = 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 nn Theta1_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)
标签:
原文地址:http://www.cnblogs.com/llhthinker/p/5356174.html