From 3b1db0b1f263a2909e1b3cb12718f0b7225a400b Mon Sep 17 00:00:00 2001 From: "Eric Borts (Lambda)" Date: Sat, 29 Mar 2014 18:21:46 -0600 Subject: [PATCH] Added "minimal" test case for costFunctionReg - 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 --- ex2/test_costFunctionReg.m | 6 ++-- ex2/test_costfunction_reg_minimal.m | 51 +++++++++++++++++++++++++++++ ex2/test_ex2.m | 5 ++- 3 files changed, 58 insertions(+), 4 deletions(-) create mode 100644 ex2/test_costfunction_reg_minimal.m diff --git a/ex2/test_costFunctionReg.m b/ex2/test_costFunctionReg.m index e8cd3d7..fb0b670 100644 --- a/ex2/test_costFunctionReg.m +++ b/ex2/test_costFunctionReg.m @@ -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); @@ -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); diff --git a/ex2/test_costfunction_reg_minimal.m b/ex2/test_costfunction_reg_minimal.m new file mode 100644 index 0000000..90ba192 --- /dev/null +++ b/ex2/test_costfunction_reg_minimal.m @@ -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 diff --git a/ex2/test_ex2.m b/ex2/test_ex2.m index 2dac6bf..43b264e 100644 --- a/ex2/test_ex2.m +++ b/ex2/test_ex2.m @@ -8,4 +8,7 @@ %!test test_predict() -%!test test_costFunctionReg() \ No newline at end of file +%!test test_costfunction_reg_minimal() + +%!test test_costFunctionReg() +