-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Tests (trivial and nontrivial) for costFunction and gradient
- Loading branch information
1 parent
1fc21db
commit 7a69a14
Showing
4 changed files
with
50 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
function test_costfunction () | ||
% an extremely simple cost function calculation with | ||
% one negative and one positive class | ||
X = [1 1; 2 2]; % minimal 2D feature matrix | ||
y = [0 1]'; % at least one negative and one positive | ||
theta = [0 0]'; % simplify calculations by using 0 for theta | ||
|
||
[J, grad] = costFunction(theta, X, y); | ||
|
||
assert(J, 0.69315, 1e-5); | ||
assert(grad, [-1/4 -1/4]'); | ||
|
||
% NOTE: I don't think it's important for the cost calculation to | ||
% set the first column to ones (FOR THIS TEST), but I may be incorrect | ||
% in this assumption. | ||
|
||
endfunction |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
function test_costfunction_nontrivial () | ||
% simple two-entry matrix with n+1 features including | ||
% the first column as ones-vector | ||
X = [ 1 2 3; % non-trivial matrix | ||
1 4 5 ]; | ||
y = [0 1]'; % one positive, one negative | ||
theta = [.1 .2 .3]'; % non-trivial theta | ||
|
||
[J, grad] = costFunction(theta, X, y); | ||
|
||
% non-trivial cost | ||
% ERROR if you get this result... you have something backwards | ||
% assert(J, 1.3536, 1e-4 ); | ||
|
||
% the correct result | ||
assert(J, 0.85363, 1e-4 ); | ||
|
||
% non-grivial gradient | ||
grad_exp = [ 0.35951 0.63584 0.99534 ]'; | ||
assert(grad, grad_exp, 1e-4); | ||
endfunction |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters