神经网络成本函数在Andrew Ng的讲座中

时间:2017-06-01 17:11:10

标签: matlab machine-learning neural-network

我的代码有问题,这意味着为我的神经网络提供成本函数。成本函数(J)定义为成本函数。给定样本输入,成本函数返回的负值比预期值小约5倍。我已经在这个问题上工作了几个小时但仍然无法获得所需的价值。

提前致谢。

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

         one=ones(1,10);

         temp=one
         one=transpose(temp);
     sizeTheta1=size(Theta1);
     sizeTheta2=size(Theta2);


for i=1:m

    if y(i)==1
        yVec=[1,0,0,0,0,0,0,0,0,0];
    end
    if y(i)==2
        yVec=[0,1,0,0,0,0,0,0,0,0];
    end
    if y(i)==3
        yVec=[0,0,1,0,0,0,0,0,0,0];
    end
    if y(i)==4
        yVec=[0,0,0,1,0,0,0,0,0,0];
    end
    if y(i)==5
        yVec=[0,0,0,0,1,0,0,0,0,0];
    end
    if y(i)==6
        yVec=[0,0,0,0,0,1,0,0,0,0];
    end
    if y(i)==7
        yVec=[1,0,0,0,0,0,1,0,0,0];
    end
    if y(i)==8
        yVec=[1,0,0,0,0,0,0,1,0,0];
    end
    if y(i)==9
        yVec=[0,0,0,0,0,0,0,0,1,0];
    end
    if y(i)==10
        yVec=[0,0,0,0,0,0,0,0,0,1];
    end
    xVec=transpose(X(i,:));


term1=transpose(-yVec).*(log(sigmoid(Theta2(1:10,1:sizeTheta2(2)-1))*(sigmoid(Theta1(1:25,1:sizeTheta1(2)-1)*xVec))));
term2=(one-transpose(yVec)).*(log(one-(sigmoid(Theta2(1:10,1:sizeTheta2(2)-1)*(sigmoid(Theta1(1:25,1:sizeTheta1(2)-1)*xVec))))));

J=J+(term1-term2);
end 

regTheta1=0;
regTheta2=0;

J=sum(sum(J))*(1/m);

regTheta1=(sum(sum(Theta1.*Theta1)));
regTheta2=(sum(sum(Theta2.*Theta2)));

J=J+((lambda)*(regTheta1+regTheta2))/(2*m);

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

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

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


end

0 个答案:

没有答案