Skip to content

Instantly share code, notes, and snippets.

Created April 24, 2015 12:09
Show Gist options
  • Save anonymous/a247990550102a92eaf6 to your computer and use it in GitHub Desktop.
Save anonymous/a247990550102a92eaf6 to your computer and use it in GitHub Desktop.
clear all;
close all;
clc;
load data_logreg.mat;
isOctave = exist('OCTAVE_VERSION', 'builtin') ~= 0;
eta = 1;
options.degrees = [1 2 5 15];
options.methods = {'gd' 'gdad' 'builtin'};
options.iterations.gd = [10 100 1000];
options.iterations.gdad = [1000];
options.iterations.builtin = [1000];
options.fun.gd = @(eta, theta, iter) ...
(@(cost_func) gradient_descent(cost_func, eta, theta, iter));
options.fun.gdad = @(eta, theta, iter) ...
(@(cost_func) gradient_descent_adaptive(cost_func, eta, theta, iter));
if(isOctave)
options.fun.builtin = @(eta, theta, iter) ...
(@(cost_func) fminunc(cost_func, theta, ...
optimset('GradObj', 'on', 'TolFun', 1e-8, 'TolX', 1e-8, 'MaxIter', iter)));
else
options.fun.builtin = @(eta, theta, iter) ...
(@(cost_func) fminunc(cost_func, theta, ...
optimset('DerivativeCheck', 'off', ...
'GradObj', 'on', 'TolFun', 1e-8, 'TolX', 1e-8, 'MaxIter', iter)));
end
options.formats = {'png'};
for meth = options.methods
for deg = options.degrees
for iter = options.iterations.(meth{1})
[X_train, num_params] = ...
logreg_poly_design_matrix(data.x1_train, data.x2_train, deg);
theta = zeros(num_params,1);
cost_func = @(theta) logreg_cost_function(theta, X_train, data.y_train);
f = options.fun.(meth{1});
g = f(eta, theta, iter);
[theta, Jval] = g(cost_func);
X_test = logreg_poly_design_matrix(data.x1_test, data.x2_test, deg);
Jtest = logreg_cost_function(theta, X_test, data.y_test);
fprintf('meth: %7s, deg: %2d, iter: %4d, Jval: %.5f, Jtest: %.5f\n', ...
meth{1}, deg, iter, Jval, Jtest);
if(isOctave)
continue
end
h = figure('Visible','off','PaperUnits','inches','PaperPosition',[0 0 20 15]);
logreg_plot(data, deg, theta, Jval, Jtest);
for file = options.formats
print(sprintf('-d%s', file{1}),'-r100', ...
sprintf('plot-%s-%02d-%04d.%s', meth{1}, deg, iter, file{1}));
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment