-
Notifications
You must be signed in to change notification settings - Fork 12
/
example_derivativeCheck.m
52 lines (39 loc) · 1.36 KB
/
example_derivativeCheck.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
clear all
nInst = 250;
nVars = 10;
X = randn(nInst,nVars);
w = randn(nVars,1);
y = sign(X*w + randn(nInst,1));
wTest = randn(nVars,1);
fprintf('Testing gradient using forward-differencing...\n');
order = 1;
derivativeCheck(@LogisticLoss,wTest,order,1,X,y);
fprintf('Testing gradient using central-differencing...\n');
derivativeCheck(@LogisticLoss,wTest,order,2,X,y);
fprintf('Testing gradient using complex-step derivative...\n');
derivativeCheck(@LogisticLoss,wTest,order,3,X,y);
fprintf('\n\n\n');
pause
fprintf('Testing Hessian using forward-differencing\n');
order = 2;
derivativeCheck(@LogisticLoss,wTest,order,1,X,y);
fprintf('Testing Hessian using central-differencing\n');
order = 2;
derivativeCheck(@LogisticLoss,wTest,order,2,X,y);
fprintf('Testing Hessian using complex-step derivative\n');
order = 2;
derivativeCheck(@LogisticLoss,wTest,order,3,X,y);
fprintf('\n\n\n');
pause
fprintf('Testing gradient using fastDerivativeCheck...\n');
order = 1;
fastDerivativeCheck(@LogisticLoss,wTest,order,1,X,y);
fastDerivativeCheck(@LogisticLoss,wTest,order,2,X,y);
fastDerivativeCheck(@LogisticLoss,wTest,order,3,X,y);
fprintf('\n\n\n');
pause
fprintf('Testing Hessian using fastDerivativeCheck...\n');
order = 2;
fastDerivativeCheck(@LogisticLoss,wTest,order,1,X,y);
fastDerivativeCheck(@LogisticLoss,wTest,order,2,X,y);
fastDerivativeCheck(@LogisticLoss,wTest,order,3,X,y);