vinayarun

nnCostFunction code

Mar 16th, 2016
59
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function [J grad] = nnCostFunction(nn_params, ...
  2.                                    input_layer_size, ...
  3.                                    hidden_layer_size, ...
  4.                                    num_labels, ...
  5.                                    X, y, lambda)
  6.  
  7. Theta1 = reshape(nn_params(1:hidden_layer_size * (input_layer_size + 1)), ...
  8.                  hidden_layer_size, (input_layer_size + 1));
  9.  
  10. Theta2 = reshape(nn_params((1 + (hidden_layer_size * (input_layer_size + 1))):end), ...
  11.                  num_labels, (hidden_layer_size + 1));
  12.  
  13. m = size(X, 1);
  14.  
  15. J = 0;
  16. Theta1_grad = zeros(size(Theta1));
  17. Theta2_grad = zeros(size(Theta2));
  18.  
  19. X = [ones(m, 1) X];
  20. A = sigmoid(Theta1 * X');
  21. A = [ones(1, size(A, 2)); A];
  22. t = sigmoid(Theta2*A);
  23.  
  24. J = (-1/m)*sum(sum(y'.*(log(t)) + ((1-y').*log(1-t)))) + (lambda/(2*m))*sum(sum(Theta1(:,2:end).^2)) + sum(sum(Theta2(:,2:end).^2));
  25.  
  26. for i = 1:m
  27.     a1 = X(i,:);
  28.     a2 = sigmoid(Theta1 * a1');
  29.     a2 = [ones(1, size(a2, 2)); a2];
  30.     a3 = sigmoid(Theta2*a2);
  31.     delta3 = a3-y(i);
  32.     delta2 = (Theta2'*delta3).*(a2.*(1-a2));
  33.     Theta1_grad = Theta1_grad + [delta2(2:end,:)]*a1;
  34.     Theta2_grad = Theta2_grad + delta3*a2';
  35. end
  36.  
  37. Theta1_grad = (1/m)*Theta1_grad + (lambda/m)*[zeros(size(Theta1,1),1), Theta1(:,2:end)];
  38. Theta2_grad = (1/m)*Theta2_grad + (lambda/m)*[zeros(size(Theta2,1),1), Theta2(:,2:end)];
  39.  
  40. % Unroll gradients
  41. grad = [Theta1_grad(:) ; Theta2_grad(:)];
  42.  
  43. end
RAW Paste Data