吴恩达 MachineLearning 第五周课后练习代码
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了吴恩达 MachineLearning 第五周课后练习代码,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2712字,纯文字阅读大概需要4分钟。
内容图文
![吴恩达 MachineLearning 第五周课后练习代码](/upload/InfoBanner/zyjiaocheng/1243/010580003cb240e58232715e54cbf673.jpg)
吴恩达 MachineLearning 第五周课后练习代码
概述
本周主要讲解了反向传播算法 (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
内容总结
以上是互联网集市为您收集整理的吴恩达 MachineLearning 第五周课后练习代码全部内容,希望文章能够帮你解决吴恩达 MachineLearning 第五周课后练习代码所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。