实施神经网络以计算其成本

时间:2018-02-25 22:59:10

标签: matlab neural-network

Cost function

我正在尝试在Matlab中编写上面的表达式。不幸的是,我似乎得到了10.441460而不是0.287629的成本,所以我超过了36倍!

至于每个符号:

m is the number of training examples.   [a scalar number]

K is the number of output nodes.   [a scalar number]

y is the vector of training outputs   [an m by 1 vector]


y^{(i)}_{k} is the ith training output (target) for the kth output node.   
[a scalar number]
x^{(i)} is the ith training input.   [a column vector for all the input 
nodes]
h_{\theta}(x^{(i)})_{k} is the value of the hypothesis at output k, with 
weights theta, and training input i.   [a scalar number]

note: h_{\theta}(x^{(i)}) will be a column vector with K rows.

我尝试使用成本函数:

Theta1 = [ones(1,size(Theta1,2));Theta1];
X = [ones(m,1) , X];     %Add a column of 1's to X
R=zeros(m,1);
for i = 1:m
    a = y(i) == [10 1:9];
     R(i) = -(a*(log(sigmoid(Theta2*(sigmoid(Theta1*X(i,:)'))))) + (1-a)*(log(1-sigmoid(Theta2*(sigmoid(Theta1*X(i,:)'))))))/m;
end
J = sum(R);

这可能对参考有用:

  function [J grad] = nnCostFunction(nn_params, ...
                                   input_layer_size, ...
                                   hidden_layer_size, ...
                                   num_labels, ...
                                   X, y, lambda)

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);


% ====================== 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
%

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

end

0 个答案:

没有答案