-
Notifications
You must be signed in to change notification settings - Fork 58
/
sparseFiltering.m
43 lines (36 loc) · 1.21 KB
/
sparseFiltering.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
function [optW] = sparseFiltering(N, X)
% N = # features, X = input data (examples in column)
optW = randn(N, size(X, 1));
optW = minFunc(@SparseFilteringObj, optW(:), ...
struct('MaxIter', 200, 'Corr', 20), X, N);
optW = reshape(optW, [N, size(X, 1)]);
end
function [Obj, DeltaW] = SparseFilteringObj (W, X, N)
% Reshape W into matrix form
W = reshape(W, [N, size(X,1)]);
% Feed Forward
F = W*X;
Fs = sqrt(F.^2 + 1e-8);
[NFs, L2Fs] = l2row(Fs);
[Fhat, L2Fn] = l2row(NFs');
% Compute Objective Function
Obj = sum(sum(Fhat, 2), 1);
% Backprop through each feedforward step
DeltaW = l2rowg(NFs', Fhat, L2Fn, ones(size(Fhat)));
DeltaW = l2rowg(Fs, NFs, L2Fs, DeltaW');
DeltaW = (DeltaW .* (F./Fs)) * X';
DeltaW = DeltaW(:);
end
%
% The following functions are now in common/
%
% function [Y,N] = l2row(X) % L2 Normalize X by rows
% % We also use this to normalize by column with l2row(X')
% N = sqrt(sum(X.^2,2) + 1e-8);
% Y = bsxfun(@rdivide,X,N);
% end
%
% function [G] = l2grad(X,Y,N,D) % Backpropagate through Normalization
% G = bsxfun(@rdivide, D, N) - bsxfun(@times, Y, sum(D.*X, 2)./(N.^2));
% end
%