首页 > 解决方案 > 我的 Octave 函数将所有用于和反向传播的答案返回为 0.0000 --- coursera ML 第 5 周作业

问题描述

我目前正在学习 Andrew Nguyen 的 coursera 机器学习课程,现在是第 5 周。对于前向和反向传播的神经网络分配,我的函数不断返回“ans = 0.000”

输出:

Feedforward Using Neural Network ...
Cost at parameters (loaded from ex4weights): 0.000000
(this value should be about 0.287629)

Checking Cost Function (w/ Regularization) ...
Cost at parameters (loaded from ex4weights): 0.000000
(this value should be about 0.383770)

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 ======================
% Instructions: You should complete the code by working through the
%               following parts.
%
% Part 1: Feedforward the neural network and return the cost in the
%         variable J. After implementing Part 1, you can verify that your
%         cost function computation is correct by verifying the cost
%         computed in ex4.m
%

y_matrix = eye(num_labels)(y,:);

X = [ones(m,1), X];

a1 = X;

z2 = a1 * Theta1';
a2 = sigmoid(z2);
a2 = a2 = [ones(m,1), a2];

z3 = a2 * Theta2';
a3 = sigmoid(z3);


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

% Part 2: Implement the backpropagation algorithm to compute the gradients
%         Theta1_grad and Theta2_grad. You should return the partial derivatives of
%         the cost function with respect to Theta1 and Theta2 in Theta1_grad and
%         Theta2_grad, respectively. After implementing Part 2, you can check
%         that your implementation is correct by running checkNNGradients
%
%         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.
%
%         Hint: We recommend implementing backpropagation using a for-loop
%               over the training examples if you are implementing it for the 
%               first time.

A1 = X; % 5000 x 401
  
  Z2 = A1 * Theta1';  % m x hidden_layer_size == 5000 x 25
  A2 = sigmoid(Z2); % m x hidden_layer_size == 5000 x 25
  A2 = [ones(size(A2,1),1), A2]; % Adding 1 as first column in z = (Adding bias unit) % m x (hidden_layer_size + 1) == 5000 x 26
  
  Z3 = A2 * Theta2';  % m x num_labels == 5000 x 10
  A3 = sigmoid(Z3); % m x num_labels == 5000 x 10
  
  
  
  y_matrix = eye(num_labels)(y,:);
  
  DELTA3 = A3 - y_matrix; % 5000 x 10
  DELTA2 = (DELTA3 * Theta2') .*sigmoidGradient(Z2); % 5000 x 26
  DELTA2 = DELTA2(:,2:end); % 5000 x 25 %Removing delta2 for bias node
  
  Theta1_grad = (1/m) * (DELTA2 * A1); % 25 x 401
  Theta2_grad = (1/m) * (DELTA3 * A2); % 10 x 26


%
% Part 3: Implement regularization with the cost function and gradients.
%
%         Hint: You can implement this around the code for
%               backpropagation. That is, you can compute the gradients for
%               the regularization separately and then add them to Theta1_grad
%               and Theta2_grad from Part 2.
%

 reg_term = (lambda/(2*m)) * (sum(sum(Theta1(:,2:end).^2)) + sum(sum(Theta2(:,2:end).^2))); %scalar
  
  %Costfunction With regularization
  J = J + reg_term; %scalar
  
  %Calculating gradients for the regularization
  Theta1_grad_reg_term = (lambda/m) * [zeros(size(Theta1, 1), 1) Theta1(:,2:end)]; % 25 x 401
  Theta2_grad_reg_term = (lambda/m) * [zeros(size(Theta2, 1), 1) Theta2(:,2:end)]; % 10 x 26
  
  %Adding regularization term to earlier calculated Theta_grad
  Theta1_grad = Theta1_grad + Theta1_grad_reg_term;
  Theta2_grad = Theta2_grad + Theta2_grad_reg_term;
  
% -------------------------------------------------------------

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

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


end

标签: neural-networkoctavebackpropagation

解决方案


%%%%%%%%%%%%%%%%% PART-1 %%%%%%%%%%%%%%%%

y_matrix = eye(num_labels)(y,:);

X = [ones(m,1), X];

a1 = X;

z2 = a1 * Theta1';

a2 = sigmoid(z2);

a2 = [一个(m,1),a2];

%在这一行你应该写 a2=[ones(size(a2,1),1),a2];

z3 = a2 * Theta2';

a3 = sigmoid(z3);

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

%在这一行中,而不是 (-1/m) 使用 (1/m) 也正确检查括号。

%%%%%%%%%%%% 第二部分 %%%%%%%%%%%%%%

y_matrix = eye(num_labels)(y,:);

DELTA3 = A3 - y_matrix;% 5000 x 10

DELTA2 = (DELTA3 * Theta2') .*sigmoidGradient(Z2);% 5000 x 26

% In this Line (DELTA3 * Theta2') 表示(5000 x 10 * 26 x 10) 这是错误的!同样 sigmoidGradient(Z2) 实际上是 5000 x 25 所以要使其具有相同的尺寸,我们可以将其重写为

%DELTA2= (DELTA3 * Theta2).*[ones(size(Z2,1),1) sigmoidGradient(Z2)]; % 5000 x 26

DELTA2 = DELTA2(:,2:end); % 5000 x 25 %移除偏置节点的 delta2

% 再次正确检查每个向量的尺寸。


推荐阅读