간단한 BP 신경 네트워크 구현

4263 단어 신경 네트워크
간단한 BP신경망cosfunction;(coursera machine learning programming assignment)
function [J grad] = nnCostFunction(nn_params, ...
                                   input_layer_size, ...
                                   hidden_layer_size, ...
                                   num_labels, ...
                                   X, y, lambda)
%nn_params  unroll                                
%nn_params
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));

%%  
sigmoid = @(z)(1./(1+exp(-z)));
a1 = [ones(m,1) X];
z2 = a1 *Theta1';
a2 = sigmoid(z2);
a2 = [ones(size(a2,1),1) a2];
z3 = a2 * Theta2';
a3 = sigmoid(z3);
y_classes = zeros(m,num_labels);

%%  y , y=5, y=[0 0 0 0 1 0 0 0 0];
for i = 1 : m
    y_classes(i,y(i)) = 1;
end
% 
J = sum(sum(-y_classes.*log(a3) - (1-y_classes).*log(1-a3))/m);

% ;
Theta1_no_bias = Theta1;
Theta1_no_bias(:,1) = 0;
Theta2_no_bias = Theta2;
Theta2_no_bias(:,1) = 0;
%  
J= J + (sum(sum(Theta1_no_bias.*Theta1_no_bias)) + sum(sum(Theta2_no_bias.*Theta2_no_bias)))*lambda/2/m;

% , 。
delta3 = (a3 - y_classes);
delta2 = delta3 * Theta2(:,2:end).*sigmoidGradient(z2);
Theta1(:,1) = 0;
Theta2(:,1) = 0;
Theta1_grad = delta2' * a1/m + lambda /m *Theta1;
Theta2_grad = delta3' * a2/m + lambda /m *Theta2;
% Unroll gradients
grad = [Theta1_grad(:) ; Theta2_grad(:)];


end

좋은 웹페이지 즐겨찾기