Skip to content

Commit

Permalink
Added "minimal" test case for costFunctionReg
Browse files Browse the repository at this point in the history
- These tests use only 0s and 1s for the matrices, mainly, and allow you
  to find if you've got the very basics of the algorithm working. For a
  more detailed test of costFunctionReg see test_costFunctionReg.m from
  community TA Collin Beckingham which uses magic(3) and non-trival values
  for theta and y
  • Loading branch information
cod3monk3y committed Mar 30, 2014
1 parent d0399da commit 3b1db0b
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 4 deletions.
6 changes: 3 additions & 3 deletions ex2/test_costFunctionReg.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ function test_costFunctionReg ()
lambda = 3;

J_exp = 50.971;
grad_exp = [-6.1081 -7.1932 -12.3540];
grad_exp = [-6.1081 -7.1932 -12.3540]'; % vectors are typically vertical

[J, grad] = costFunctionReg(theta, X, y, lambda);

Expand All @@ -19,10 +19,10 @@ function test_costFunctionReg ()
% From TA Colin Beckingham, at https://class.coursera.org/ml-005/forum/thread?thread_id=943#post-4666
theta = [2; 1; -9];
X = magic(3);
y = [1; -0.2; 3];
y = [1; -0.2; 3];
lambda = 0.1;
J_exp_2 = 11.338;
grad_exp_2 = [-6.1081 -8.1598 -3.6540];
grad_exp_2 = [-6.1081 -8.1598 -3.6540]'; % vectors are typically vertical

[J, grad] = costFunctionReg(theta, X, y, lambda);

Expand Down
51 changes: 51 additions & 0 deletions ex2/test_costfunction_reg_minimal.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
function test_costfunction_reg_minimal ()
% basically the same test as test_costfunction_minimal, but with
% varying values for the lambda, and theta cannot be 0 (there would be
% nothing to penalize), and the x0 (ones) vector has been added

X = [1 1 1;
1 2 2]; % minimal 2D feature matrix
y = [0 1]'; % at least one negative and one positive

theta = [0 1 1]'; % need non-zero values for theta

%
% lambda = 0 --> no regularization
%
[J0, grad0] = costFunctionReg(theta, X, y, 0.0);
[J1, grad1] = costFunctionReg(theta, X, y, 1.0);
[J2, grad2] = costFunctionReg(theta, X, y, 2.0);

J = [J0 J1 J2] ;
assert(J, [1.0725 1.5725 2.0725], 1e-4);

grad_exp = [
0.43141 0.43141 0.43141;
0.42241 0.92241 1.42241;
0.42241 0.92241 1.42241; ];

grad = [grad0 grad1 grad2];

assert(grad, grad_exp, 1e-4);

% non-zero theta 0, validates that code isn't
% including theta 0 in the regularization term
theta = [1 1 1]';

[J0, grad0] = costFunctionReg(theta, X, y, 0.0);
[J1, grad1] = costFunctionReg(theta, X, y, 1.0);
[J2, grad2] = costFunctionReg(theta, X, y, 2.0);

J = [J0 J1 J2];
assert(J, [1.5277 2.0277 2.5277], 1e-4);

grad_exp = [
0.47294 0.47294 0.47294;
0.46959 0.96959 1.46959;
0.46959 0.96959 1.46959 ];

grad = [grad0 grad1 grad2];

assert(grad, grad_exp, 1e-4);

endfunction
5 changes: 4 additions & 1 deletion ex2/test_ex2.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,7 @@

%!test test_predict()

%!test test_costFunctionReg()
%!test test_costfunction_reg_minimal()

%!test test_costFunctionReg()

0 comments on commit 3b1db0b

Please sign in to comment.