Skip to content

Instantly share code, notes, and snippets.

@zhangce
Last active November 1, 2016 07:12
Show Gist options
  • Save zhangce/494784bf342d5d8040c90c463e9accc6 to your computer and use it in GitHub Desktop.
Save zhangce/494784bf342d5d8040c90c463e9accc6 to your computer and use it in GitHub Desktop.
function [m n_fetchfull] = SVMq(X, y, m, lr, num_range)
n_examples = size(X, 1);
n_features = size(X, 2);
num_ranges = {};
num_ranges_array = [];
for j=1:n_features
num_ranges{j} = num_range * (floor(log(abs(m(j))+1)) + 1);
num_ranges_array = [num_ranges_array num_ranges{j}];
end
num_ranges_array
X1 = quantize_minmaxfull(X, num_ranges);
n_fetchfull = 0;
for i=1:n_examples
label = y(i);
dot = X1(i, :) * m'; % Quantized Version
weight = abs(1-dot* label);
prob = weight/(weight + 1);
if prob < 0.1
prob = 0.1;
end
if rand() <= prob % Importance Sampling --
% Give those near boundary a
% smaller probability to be
% choosen
upper = dot + sum(m(find(m>0)) ./ num_ranges_array(find(m>0))); % num_ranges{j}; .* MAXS(find(m>0)) ./ num_ranges_array(find(m>0)));
lower = dot + sum(m(find(m<0)) ./ num_ranges_array(find(m<0))); % .* MAXS(find(m<0)) ./ num_ranges_array(find(m<0)));
if (1-upper*label) * (1-lower*label) < 0
% If we are not sure about
% whether quantization is
% safe
n_fetchfull = n_fetchfull + 1;
dot = X(i, :) * m';
% Fetch original data
end
if 1 - dot * label > 0
m = m + lr * label * X1(i, :) / prob;
end
end
end
n_fetchfull
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment