-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathreg_sparse_coding.m
More file actions
95 lines (82 loc) · 2.35 KB
/
Copy pathreg_sparse_coding.m
File metadata and controls
95 lines (82 loc) · 2.35 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
function [B, S, stat] = reg_sparse_coding(X, num_bases, Sigma, beta, gamma, num_iters, batch_size, initB, initS, fname_save)
%
% Regularized sparse coding
%
% Inputs
% X -data samples, column wise
% num_bases -number of bases
% Sigma -smoothing matrix for regularization
% beta -smoothing regularization
% gamma -sparsity regularization
% num_iters -number of iterations
% batch_size -batch size
% initB -initial dictionary
% initS -initial coefficient
% fname_save -file name to save dictionary
%
% Outputs
% B -learned dictionary
% S -sparse codes
% stat -statistics about the training
%
% Written by Jianchao Yang @ IFP UIUC, Sep. 2009.
pars = struct;
pars.patch_size = size(X,1);
pars.num_patches = size(X,2);
pars.num_bases = num_bases;
pars.num_trials = num_iters;
pars.beta = beta;
pars.gamma = gamma;
pars.VAR_basis = 1; % maximum L2 norm of each dictionary atom
if ~isa(X, 'double'),
X = cast(X, 'double');
end
if isempty(Sigma),
Sigma = eye(pars.num_bases);
end
if exist('batch_size', 'var') && ~isempty(batch_size)
pars.batch_size = batch_size;
else
pars.batch_size = size(X, 2);
end
if exist('fname_save', 'var') && ~isempty(fname_save)
pars.filename = fname_save;
else
pars.filename = sprintf('Results/reg_sc_b%d_%s', num_bases, datestr(now, 30));
end
pars
% initialize basis
if ~exist('initB') || isempty(initB)
B = rand(pars.patch_size, pars.num_bases)-0.5;
B = B - repmat(mean(B,1), size(B,1),1);
B = B*diag(1./sqrt(sum(B.*B)));
else
disp('Using initial B...');
B = initB;
end
[L M]=size(B);
t=0;
% statistics variable
% optimization loop
while t < pars.num_trials
t=t+1;
% Take a random permutation of the samples
indperm = randperm(size(X,2));
for batch=1:(size(X,2)/pars.batch_size),
% This is data to use for this step
batch_idx = indperm((1:pars.batch_size)+pars.batch_size*(batch-1));
Xb = X(:,batch_idx);
% learn coefficients (conjugate gradient)
S = initS(:,batch_idx);
% update basis
B = l2ls_learn_basis_dual(Xb, S, pars.VAR_basis);
end
end
return
function retval = assert(expr)
retval = true;
if ~expr
error('Assertion failed');
retval = false;
end
return