-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfKernelized_k_Means_Clustering.m
85 lines (63 loc) · 2.73 KB
/
fKernelized_k_Means_Clustering.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
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
function [W,C] = fKernelized_k_Means_Clustering(Q,m,gamma,Tmax )
% Function for Kernelized k-mean clustering
% Steps involved: 1) Intialize 2) Learn Dictionary: Obtain W; 3) Obtain
% clusters/labels
%%
% PArameters
% Input
% 1) Q: Pairwise fiber distance/similiarity/dissimilarity matrix:
% Size: n x n
% 2) m: the desired number of fiber bundles
% 3) gamma: RBF kernel parameter
% 4) Tmax: Maximum Number of iterations
%
% Output:
% 1) W: Sparse assignment matix: Size: n x m
% 2) C: Hard/Label/Cluster assignment matrix: Size: 1 x n
%%
% initialize
% Kernel Matrix
K = exp(-gamma*Q.^2) ;
n = size(Q,1) ;
I = eye(n) ;
% initialize A
S = randsample(n,m) ;
A = I(:,S) ;
iter_Error = zeros(Tmax,1) ;
Eold = inf ;
disp('Starting clustering') ;
%%
% dictionary learning
for iter = 1:Tmax
% kernelized k means_clustering
mat_KA = K*A ;
M = A'*mat_KA ;
mat_diag_M = diag(M)';
index = zeros(n,1) ;
W = zeros([m n]) ;
% each column of W: can be computed in parallel
parfor i=1:n
[~,c_i]= min(mat_diag_M - 2*mat_KA(i,:)) ;
index(i) = (i-1)*m + c_i ;
end
W(index) = 1 ;
% Evaluate Reconstruction error
E = trace(K - 2*mat_KA*W + W'*(M*W)) ;
iter_Error(iter) = E ;
if (E > Eold)
disp('Energy function increased???') ;
break ;
elseif ((Eold - E)/E < 0.001)
disp('No change in energy function') ;
break ;
end
Eold = E ;
% Update Dictionary Matrix A using: MOD
temp_inv_mat = W*W' ;
%A = W'/(temp_inv_mat + 1e-10*eye(size(temp_inv_mat))) ;
A = W'/temp_inv_mat ;
end
%%
% Obtain hard/Cluster assignment
[~, C] = max(W,[],1) ;
end