成本函数计算,用于神经网络

0

的问题

我是在第5周的安德鲁*伍的机学习课程,课程. 我工作过程分配Matlab中的为这个星期,和我选择使用一个循环实施计算成本J.这里是我的功能。

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. 

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

% add bias to X to create 5000x401 matrix
X = [ones(m, 1) X];
         
% You need to return the following variables correctly 
J = 0;
Theta1_grad = zeros(size(Theta1));
Theta2_grad = zeros(size(Theta2));


% initialize summing terms used in cost expression
sum_i = 0.0;

% loop through each sample to calculate the cost
for i = 1:m

    % logical vector output for 1 example
    y_i = zeros(num_labels, 1);
    class = y(m);
    y_i(class) = 1;
    
    % first layer just equals features in one example 1x401
    a1 = X(i, :);
    
    % compute z2, a 25x1 vector
    z2 = Theta1*a1';
    
    % compute activation of z2
    a2 = sigmoid(z2);
    
    % add bias to a2 to create a 26x1 vector
    a2 = [1; a2];
    
    % compute z3, a 10x1 vector
    z3 = Theta2*a2;
    
    %compute activation of z3. returns output vector of size 10x1
    a3 = sigmoid(z3);
    h = a3;
    
    % loop through each class k to sum cost over each class
    for k = 1:num_labels        
        
        % sum_i returns cost summed over each class
        sum_i = sum_i + ((-1*y_i(k) * log(h(k))) - ((1 - y_i(k)) * log(1 - h(k))));
        
    end
        
end

J = sum_i/m;

我的理解是,一个向量化的实施,这会更容易,但我不明白为什么这种实现是错误的。 当num_labels=10,这一功能输出J=8.47,但预计费用是0.287629. 我计算J从 这个公式. 我误解的计算? 我的理解是,每培训,例的成本为每10个课程是计算后费用对所有10个课程的每个例子都总结在一起。 那是不正确的? 或者我有没有不执行这在我的代码是否正确? 在此先感谢。

2
0

问题是式实现

这种表达 ((-1*y_i(k) * log(h(k))) - ((1 - y_i(k)) * log(1 - h(k)))); 表示的损失的情况下,在二进制分类,因为你只有2个类别所以

  1. y_i is 0 so (1 - yi) = 1
  2. y_i is 1 so (1 - yi) = 0

那么你基本上只考虑目标类概率。

怎么情况下的10片公司的财产作为你提到(y_i)或(1-yi)没有必要为他们中的一个要0和其他要1

你应该正确的损失功能执行这样,你只考虑到的概率的目标类唯一不是所有其他类。

2021-11-22 23:54:56
0

我的问题是与编制索引。 而不是说 class = y(m) 它应该是 class = y(i) 由于 i 是的索引和 m 是5000从数中的行培训的数据。

2021-11-23 03:53:01

其他语言

此页面有其他语言版本

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................