吴恩达 MachineLearning Week5

吴恩达 MachineLearning Week5

概述

本周主要讲解了反向传播算法 (backpropagation algorithm),用于计算神经网络中代价方程 (cost function) 对变量 theta 的偏导数值。本周主要内容即实现该算法。

nnCostFunction.m

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 = 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);
         
% You need to return the following variables correctly 
J = 0;
Theta1_grad = zeros(size(Theta1));
Theta2_grad = zeros(size(Theta2));

% ====================== YOUR CODE HERE ======================

% 记得加上 bias unit
X = [ones(m, 1) X]; 

% 将 y 值转成 1 * 10 的矩阵
Y = zeros(m , num_labels);
for i = 1:m
    Y(i , y(i)) = 1;
end

% Forwardpropagation

a2 = sigmoid(X * Theta1');

a2 = [ones(size(a2 , 1) , 1) a2]; 

a3 = sigmoid(a2 * Theta2') ;

J = sum( sum( -Y .* log(a3) - (1 - Y) .* log(1 - a3) ) ) / m;

Theta1_withoutBias = Theta1(: , 2:end);
Theta2_withoutBias = Theta2(: , 2:end);

% regularized cost function
J = J + lambda * (sum(sum(Theta1_withoutBias .^ 2)) + sum(sum(Theta2_withoutBias .^ 2))) / (2 * m);

% Backpropagation

d1 = zeros(size(Theta1));
d2 = zeros(size(Theta2)) ;

theta1_wtbias = Theta1;
theta1_wtbias(: , 1) = 0;

theta2_wtbias = Theta2;
theta2_wtbias(: , 1) = 0;

for t = 1:m
    yt = Y(t , :);
    a3t = a3(t , :);
    a2t = a2(t , :);
    a1t = X(t , :);
    delta3 = a3t - yt;
    delta2 = delta3 * Theta2 .* (a2t .* (1 - a2t));
    delta2 = delta2(2:end);
    d2 = d2 + delta3' * a2t;
    d1 = d1 + delta2' * a1t; 
end
% regularized theta
Theta1_grad = Theta1_grad + d1 / m + lambda * theta1_wtbias / m; 
Theta2_grad = Theta2_grad + d2 / m + lambda * theta2_wtbias / m;


% -------------------------------------------------------------

% =========================================================================

% Unroll gradients
grad = [Theta1_grad(:) ; Theta2_grad(:)];


end

转载于:https://www.cnblogs.com/amoy-zhp/p/8682073.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值