-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathsearch_opt_threshold.m
49 lines (40 loc) · 1.25 KB
/
search_opt_threshold.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
% Find the optimal threshold of a weight matrix.
% The edges for small values are turned "off."
%
% We search over threshold values using an efficient fibonacci search.
%
% Pick the value of thresholding
% that optimizes the model score.
function M = search_opt_threshold(M)
% Get the unique weight values
U_cc = logical(triu(M.SK,1));
wvec = vec(M.WK(U_cc));
wvec = unique(wvec);
wvec = wvec(:);
wvec(wvec<.001)=[];
% Possible values to threshold matrix at
tvals = [3*eps; wvec + 3*eps];
fobj = @(threshold)threshold_w(threshold,M);
% Special case when wvec is empty
if isempty(wvec)
[score,M] = fobj(inf);
return
end
% Search for the best threshold
[tbest,tbest_fobj,M] = min_log_search(tvals,fobj);
end
% Threshold the weight matrix
% and report the score
function [score,M] = threshold_w(threshold,M)
% Remove the weights that are below threshold
oldSK = M.SK;
M.SK = sparsity_pattern(M.WK,threshold);
M.SK = M.SK & oldSK;
M.WK(~M.SK)=0;
% Re-optimize the weights
M = search_learn_parameters(M);
% Score the model
lp = search_score(M);
score = -lp;
% fprintf(1,'Threshold=%s score=%s\n',num2str(threshold,3),num2str(score,6));
end