diff --git a/ex4/nnCostFunction.m b/ex4/nnCostFunction.m index 0cb283f..fd42d44 100644 --- a/ex4/nnCostFunction.m +++ b/ex4/nnCostFunction.m @@ -80,23 +80,14 @@ assert(size(J) == [1 1]); % and Theta2_grad from Part 2. % - - - - - - - - - - - - - - - - - +% Note: Theta1/2 are matrixes here, we want all their rows, but skip their +% first column (not regularizing the bias term). +regularization_term = lambda/(2*m) * ... + (sum(sum(Theta1(:,2:end).^2)) ... + + sum(sum(Theta2(:,2:end).^2))); +assert(size(regularization_term) == [1 1]); + +J += regularization_term; % -------------------------------------------------------------