From 395c5676dc6a0e6cda868ad96f053d2a769e3090 Mon Sep 17 00:00:00 2001 From: neingeist Date: Sat, 1 Nov 2014 20:42:58 +0100 Subject: [PATCH] Add regularization to the cost function --- ex4/nnCostFunction.m | 25 ++++++++----------------- 1 file changed, 8 insertions(+), 17 deletions(-) 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; % -------------------------------------------------------------