-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathJMVFG.m
1422 lines (1266 loc) · 47 KB
/
JMVFG.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
% This is a demo for the JMVFG algorithm, which is proposed in the paper below. %
% %
% Si-Guo Fang, Dong Huang, Chang-Dong Wang, Yong Tang. %
% Joint Multi-view Unsupervised Feature Selection and Graph Learning. %
% IEEE Transactions on Emerging Topics in Computational Intelligence, 2023. %
% %
% The code has been tested in Matlab R2019b on a PC with Windows 10. %
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [ranking,SS,XX] = JMVFG(X,eta,gamma,beta,c)
%Input: X = cell({X_1,X_2...X_V}) -- Multi-view dataset, each row of X_v represents a sample.
% Parameters: eta > 0, gamma>0, beta>0.
% c -- The number of features after projection, where d_1 == d_2 == ... == d_v == c.
%Output: ranking -- Feature ranking.
% SS -- Similarity graph.
% XX -- Data matrix after feature concatenation and normalization, each row represents a sample.
%% Parameter setting, variable initialization
V = size(X,2); %The number of views
n = size(X{1,1},1); %The number of samples
alpha = 10000;
delta = 1 / V * ones(V,1); %Initialization of delta
%Maximum and minimum normalization
XX = [];%Feature concatenation
for i = 1:V
X{1,i} = ( X{1,i}-repmat(min(X{1,i}),n,1) ) ./repmat( max(X{1,i})-min(X{1,i}),n,1);
X{1,i}( isnan(X{1,i}) ) = 1;
XX = [XX X{1,i}];
end
%Initialize similarity matrix A
A = cell(1,V);
for i = 1:V
sigma = optSigma(X{1,i})^2; %Median of Euclidean distance between all two samples
A{1,i} = constructW(X{1,i}, struct('k',5, 'WeightMode', 'HeatKernel', 't', sigma));
A{1,i} = V * A{1,i} ./ repmat( sum(A{1,i},2 ) , 1 , size(A{1,i},1)); %Row normalization and multiply by V
end
%Initialize S, L
S = zeros(size(A{1,1}));
for i = 1:V
S = S + delta(i) * A{1,i};
end
SS = (S + S') / 2;
P = diag(sum(SS));
L = P - SS;
%Initialize cluster indicator matrix H
options.method = 'k_means';
H = init_H(XX,c,options);
%Initialize W, D and B
W = cell(1,V);
B = cell(1,V);
m = cell(1,V);
D = cell(1,V);
for i=1:V
m{1,i} = size(X{1,i},2); %Feature dimension
X{1,i} = X{1,i}'; %Each column of X_v represents a sample
W{1,i} = eye(m{1,i},c);
B{1,i} = W{1,i}' * X{1,i} * H;
D{1,i} = eye(m{1,i});
end
%% Optimization
MAXITER = 20; %Maximum number of iterations
res = zeros(MAXITER,1); %Objective function value of each iteration
%Calculate the objective function value
Y = cell(1,V);
res_one = 0;
res_two = 0;
res_three = 0;
for i = 1:V
res_one = res_one + norm(W{1,i}' * X{1,i} - B{1,i} * H','fro')^2 + eta * norm_21(W{1,i});
Y{1,i} = W{1,i}' * X{1,i};
res_two = res_two + gamma * trace(Y{1,i} * L * Y{1,i}' );
res_three = res_three + beta * norm( S - delta(i) * A{1,i} ,'fro')^2;
end
res_old = res_one + res_two + res_three;
res(1) = res_old;
for iter = 1:MAXITER
%Update delta
delta = solve_delta(S,A);
%Update W, D
for i = 1:V
temp_W = (X{1,i} * X{1,i}') + gamma * (X{1,i} * L * X{1,i}') + eta * D{1,i};
W{1,i} = temp_W \ X{1,i} * H * B{1,i}';
tempD = 0.5 * (sqrt(sum(W{1,i}.^2,2) + eps)).^(-1);
D{1,i} = diag(tempD);
Y{1,i} = W{1,i}' * X{1,i}; %Update Y for updating S
end
%Update B
for i = 1:V
SVD = W{1,i}'*X{1,i}*H; %This corresponds to the transpose in the paper!
[V_B,~,U_B] = svd(SVD,'econ');
B{1,i} = V_B * U_B';
end
%Update Z
Z = max(H,0);
%Update H
SVD = zeros(size(Z));
for i = 1:V
SVD = SVD + X{1,i}' * W{1,i} * B{1,i} ;
end
SVD = SVD + alpha * Z; %This corresponds to the transpose in the paper!
[V_H,~,U_H] = svd(SVD,'econ');
H = V_H * U_H';
%Update S
S = Update_S(A,Y,V,beta,gamma,delta);
SS = (S + S') / 2;
P = diag(sum(SS));
L = P - SS;
%Calculate new objective function value
res_one = 0;
res_two = 0;
res_three = 0;
for i = 1:V
res_one = res_one + norm(W{1,i}'*X{1,i} - B{1,i}*H','fro')^2 + eta * norm_21(W{1,i});
Y{1,i} = W{1,i}' * X{1,i};
res_two = res_two + gamma * trace(Y{1,i} * L * Y{1,i}');
res_three = res_three + beta * norm(S - delta(i)*A{1,i} ,'fro')^2;
end
res_new = res_one + res_two + res_three;
res(iter + 1) = res_new;
%Judge whether convergence
fprintf('Iter = %d; Objective value = %f\n',iter,res_new)
diff = res_old - res_new;
if (iter > 1 && abs(diff) / (res_old) < 10^-4) || (iter > 1 && abs(diff) < 10^-4)
break
else
res_old = res_new;
end
end
%Calculate feature ranking
WW = [];
for i = 1:V
WW = [WW;W{1,i}];
end
[~,ranking] = sort(sum(WW.*WW,2),'descend');
end
function sigma = optSigma(X)
%input£ºX: row-sample column-feature
%output:sigma
N = size(X,1); %sample number
dist = EuDist2(X,X);
dist = reshape(dist,1,N*N);
sigma = median(dist);
end
function W = constructW(fea,options)
% Usage:
% W = constructW(fea,options)
%
% fea: Rows of vectors of data points. Each row is x_i
% options: Struct value in Matlab. The fields in options that can be set:
%
% NeighborMode - Indicates how to construct the graph. Choices
% are: [Default 'KNN']
% 'KNN' - k = 0
% Complete graph
% k > 0
% Put an edge between two nodes if and
% only if they are among the k nearst
% neighbors of each other. You are
% required to provide the parameter k in
% the options. Default k=5.
% 'Supervised' - k = 0
% Put an edge between two nodes if and
% only if they belong to same class.
% k > 0
% Put an edge between two nodes if
% they belong to same class and they
% are among the k nearst neighbors of
% each other.
% Default: k=0
% You are required to provide the label
% information gnd in the options.
%
% WeightMode - Indicates how to assign weights for each edge
% in the graph. Choices are:
% 'Binary' - 0-1 weighting. Every edge receiveds weight
% of 1.
% 'HeatKernel' - If nodes i and j are connected, put weight
% W_ij = exp(-norm(x_i - x_j)/2t^2). You are
% required to provide the parameter t. [Default One]
% 'Cosine' - If nodes i and j are connected, put weight
% cosine(x_i,x_j).
%
% k - The parameter needed under 'KNN' NeighborMode.
% Default will be 5.
% gnd - The parameter needed under 'Supervised'
% NeighborMode. Colunm vector of the label
% information for each data point.
% bLDA - 0 or 1. Only effective under 'Supervised'
% NeighborMode. If 1, the graph will be constructed
% to make LPP exactly same as LDA. Default will be
% 0.
% t - The parameter needed under 'HeatKernel'
% WeightMode. Default will be 1
% bNormalized - 0 or 1. Only effective under 'Cosine' WeightMode.
% Indicates whether the fea are already be
% normalized to 1. Default will be 0
% bSelfConnected - 0 or 1. Indicates whether W(i,i) == 1. Default 0
% if 'Supervised' NeighborMode & bLDA == 1,
% bSelfConnected will always be 1. Default 0.
% bTrueKNN - 0 or 1. If 1, will construct a truly kNN graph
% (Not symmetric!). Default will be 0. Only valid
% for 'KNN' NeighborMode
%
%
% Examples:
%
% fea = rand(50,15);
% options = [];
% options.NeighborMode = 'KNN';
% options.k = 5;
% options.WeightMode = 'HeatKernel';
% options.t = 1;
% W = constructW(fea,options);
%
%
% fea = rand(50,15);
% gnd = [ones(10,1);ones(15,1)*2;ones(10,1)*3;ones(15,1)*4];
% options = [];
% options.NeighborMode = 'Supervised';
% options.gnd = gnd;
% options.WeightMode = 'HeatKernel';
% options.t = 1;
% W = constructW(fea,options);
%
%
% fea = rand(50,15);
% gnd = [ones(10,1);ones(15,1)*2;ones(10,1)*3;ones(15,1)*4];
% options = [];
% options.NeighborMode = 'Supervised';
% options.gnd = gnd;
% options.bLDA = 1;
% W = constructW(fea,options);
%
%
% For more details about the different ways to construct the W, please
% refer:
% Deng Cai, Xiaofei He and Jiawei Han, "Document Clustering Using
% Locality Preserving Indexing" IEEE TKDE, Dec. 2005.
%
%
% Written by Deng Cai (dengcai2 AT cs.uiuc.edu), April/2004, Feb/2006,
% May/2007
%
bSpeed = 1;
if (~exist('options','var'))
options = [];
end
if isfield(options,'Metric')
warning('This function has been changed and the Metric is no longer be supported');
end
if ~isfield(options,'bNormalized')
options.bNormalized = 0;
end
%=================================================
if ~isfield(options,'NeighborMode')
options.NeighborMode = 'KNN';
end
switch lower(options.NeighborMode)
case {lower('KNN')} %For simplicity, we include the data point itself in the kNN
if ~isfield(options,'k')
options.k = 5;
end
case {lower('Supervised')}
if ~isfield(options,'bLDA')
options.bLDA = 0;
end
if options.bLDA
options.bSelfConnected = 1;
end
if ~isfield(options,'k')
options.k = 0;
end
if ~isfield(options,'gnd')
error('Label(gnd) should be provided under ''Supervised'' NeighborMode!');
end
if ~isempty(fea) && length(options.gnd) ~= size(fea,1)
error('gnd doesn''t match with fea!');
end
otherwise
error('NeighborMode does not exist!');
end
%=================================================
if ~isfield(options,'WeightMode')
options.WeightMode = 'HeatKernel';
end
bBinary = 0;
bCosine = 0;
switch lower(options.WeightMode)
case {lower('Binary')}
bBinary = 1;
case {lower('HeatKernel')}
if ~isfield(options,'t')
nSmp = size(fea,1);
if nSmp > 3000
D = EuDist2(fea(randsample(nSmp,3000),:));
else
D = EuDist2(fea);
end
options.t = mean(mean(D));
end
case {lower('Cosine')}
bCosine = 1;
otherwise
error('WeightMode does not exist!');
end
%=================================================
if ~isfield(options,'bSelfConnected')
options.bSelfConnected = 0;
end
%=================================================
if isfield(options,'gnd')
nSmp = length(options.gnd);
else
nSmp = size(fea,1);
end
maxM = 62500000; %500M
BlockSize = floor(maxM/(nSmp*3));
if strcmpi(options.NeighborMode,'Supervised')
Label = unique(options.gnd);
nLabel = length(Label);
if options.bLDA
G = zeros(nSmp,nSmp);
for idx=1:nLabel
classIdx = options.gnd==Label(idx);
G(classIdx,classIdx) = 1/sum(classIdx);
end
W = sparse(G);
return;
end
switch lower(options.WeightMode)
case {lower('Binary')}
if options.k > 0
G = zeros(nSmp*(options.k+1),3);
idNow = 0;
for i=1:nLabel
classIdx = find(options.gnd==Label(i));
D = EuDist2(fea(classIdx,:),[],0);
[dump idx] = sort(D,2); % sort each row
clear D dump;
idx = idx(:,1:options.k+1);
nSmpClass = length(classIdx)*(options.k+1);
G(idNow+1:nSmpClass+idNow,1) = repmat(classIdx,[options.k+1,1]);
G(idNow+1:nSmpClass+idNow,2) = classIdx(idx(:));
G(idNow+1:nSmpClass+idNow,3) = 1;
idNow = idNow+nSmpClass;
clear idx
end
G = sparse(G(:,1),G(:,2),G(:,3),nSmp,nSmp);
G = max(G,G');
else
G = zeros(nSmp,nSmp);
for i=1:nLabel
classIdx = find(options.gnd==Label(i));
G(classIdx,classIdx) = 1;
end
end
if ~options.bSelfConnected
for i=1:size(G,1)
G(i,i) = 0;
end
end
W = sparse(G);
case {lower('HeatKernel')}
if options.k > 0
G = zeros(nSmp*(options.k+1),3);
idNow = 0;
for i=1:nLabel
classIdx = find(options.gnd==Label(i));
D = EuDist2(fea(classIdx,:),[],0);
[dump idx] = sort(D,2); % sort each row
clear D;
idx = idx(:,1:options.k+1);
dump = dump(:,1:options.k+1);
dump = exp(-dump/(options.t));
nSmpClass = length(classIdx)*(options.k+1);
G(idNow+1:nSmpClass+idNow,1) = repmat(classIdx,[options.k+1,1]);
G(idNow+1:nSmpClass+idNow,2) = classIdx(idx(:));
G(idNow+1:nSmpClass+idNow,3) = dump(:);
idNow = idNow+nSmpClass;
clear dump idx
end
G = sparse(G(:,1),G(:,2),G(:,3),nSmp,nSmp);
else
G = zeros(nSmp,nSmp);
for i=1:nLabel
classIdx = find(options.gnd==Label(i));
D = EuDist2(fea(classIdx,:),[],0);
D = exp(-D/(2*options.t^2));
G(classIdx,classIdx) = D;
end
end
if ~options.bSelfConnected
for i=1:size(G,1)
G(i,i) = 0;
end
end
W = sparse(max(G,G'));
case {lower('Cosine')}
if ~options.bNormalized
fea = NormalizeFea(fea);
end
if options.k > 0
G = zeros(nSmp*(options.k+1),3);
idNow = 0;
for i=1:nLabel
classIdx = find(options.gnd==Label(i));
D = fea(classIdx,:)*fea(classIdx,:)';
[dump idx] = sort(-D,2); % sort each row
clear D;
idx = idx(:,1:options.k+1);
dump = -dump(:,1:options.k+1);
nSmpClass = length(classIdx)*(options.k+1);
G(idNow+1:nSmpClass+idNow,1) = repmat(classIdx,[options.k+1,1]);
G(idNow+1:nSmpClass+idNow,2) = classIdx(idx(:));
G(idNow+1:nSmpClass+idNow,3) = dump(:);
idNow = idNow+nSmpClass;
clear dump idx
end
G = sparse(G(:,1),G(:,2),G(:,3),nSmp,nSmp);
else
G = zeros(nSmp,nSmp);
for i=1:nLabel
classIdx = find(options.gnd==Label(i));
G(classIdx,classIdx) = fea(classIdx,:)*fea(classIdx,:)';
end
end
if ~options.bSelfConnected
for i=1:size(G,1)
G(i,i) = 0;
end
end
W = sparse(max(G,G'));
otherwise
error('WeightMode does not exist!');
end
return;
end
if bCosine && ~options.bNormalized
Normfea = NormalizeFea(fea);
end
if strcmpi(options.NeighborMode,'KNN') && (options.k > 0)
if ~(bCosine && options.bNormalized)
G = zeros(nSmp*(options.k+1),3);
for i = 1:ceil(nSmp/BlockSize)
if i == ceil(nSmp/BlockSize)
smpIdx = (i-1)*BlockSize+1:nSmp;
dist = EuDist2(fea(smpIdx,:),fea,0);
if bSpeed
nSmpNow = length(smpIdx);
dump = zeros(nSmpNow,options.k+1);
idx = dump;
for j = 1:options.k+1
[dump(:,j),idx(:,j)] = min(dist,[],2);
temp = (idx(:,j)-1)*nSmpNow+[1:nSmpNow]';
dist(temp) = 1e100;
end
else
[dump idx] = sort(dist,2); % sort each row
idx = idx(:,1:options.k+1);
dump = dump(:,1:options.k+1);
end
if ~bBinary
if bCosine
dist = Normfea(smpIdx,:)*Normfea';
dist = full(dist);
linidx = [1:size(idx,1)]';
dump = dist(sub2ind(size(dist),linidx(:,ones(1,size(idx,2))),idx));
else
dump = exp(-dump/(2*options.t^2));
end
end
G((i-1)*BlockSize*(options.k+1)+1:nSmp*(options.k+1),1) = repmat(smpIdx',[options.k+1,1]);
G((i-1)*BlockSize*(options.k+1)+1:nSmp*(options.k+1),2) = idx(:);
if ~bBinary
G((i-1)*BlockSize*(options.k+1)+1:nSmp*(options.k+1),3) = dump(:);
else
G((i-1)*BlockSize*(options.k+1)+1:nSmp*(options.k+1),3) = 1;
end
else
smpIdx = (i-1)*BlockSize+1:i*BlockSize;
dist = EuDist2(fea(smpIdx,:),fea,0);
if bSpeed
nSmpNow = length(smpIdx);
dump = zeros(nSmpNow,options.k+1);
idx = dump;
for j = 1:options.k+1
[dump(:,j),idx(:,j)] = min(dist,[],2);
temp = (idx(:,j)-1)*nSmpNow+[1:nSmpNow]';
dist(temp) = 1e100;
end
else
[dump idx] = sort(dist,2); % sort each row
idx = idx(:,1:options.k+1);
dump = dump(:,1:options.k+1);
end
if ~bBinary
if bCosine
dist = Normfea(smpIdx,:)*Normfea';
dist = full(dist);
linidx = [1:size(idx,1)]';
dump = dist(sub2ind(size(dist),linidx(:,ones(1,size(idx,2))),idx));
else
dump = exp(-dump/(2*options.t^2));
end
end
G((i-1)*BlockSize*(options.k+1)+1:i*BlockSize*(options.k+1),1) = repmat(smpIdx',[options.k+1,1]);
G((i-1)*BlockSize*(options.k+1)+1:i*BlockSize*(options.k+1),2) = idx(:);
if ~bBinary
G((i-1)*BlockSize*(options.k+1)+1:i*BlockSize*(options.k+1),3) = dump(:);
else
G((i-1)*BlockSize*(options.k+1)+1:i*BlockSize*(options.k+1),3) = 1;
end
end
end
W = sparse(G(:,1),G(:,2),G(:,3),nSmp,nSmp);
else
G = zeros(nSmp*(options.k+1),3);
for i = 1:ceil(nSmp/BlockSize)
if i == ceil(nSmp/BlockSize)
smpIdx = (i-1)*BlockSize+1:nSmp;
dist = fea(smpIdx,:)*fea';
dist = full(dist);
if bSpeed
nSmpNow = length(smpIdx);
dump = zeros(nSmpNow,options.k+1);
idx = dump;
for j = 1:options.k+1
[dump(:,j),idx(:,j)] = max(dist,[],2);
temp = (idx(:,j)-1)*nSmpNow+[1:nSmpNow]';
dist(temp) = 0;
end
else
[dump idx] = sort(-dist,2); % sort each row
idx = idx(:,1:options.k+1);
dump = -dump(:,1:options.k+1);
end
G((i-1)*BlockSize*(options.k+1)+1:nSmp*(options.k+1),1) = repmat(smpIdx',[options.k+1,1]);
G((i-1)*BlockSize*(options.k+1)+1:nSmp*(options.k+1),2) = idx(:);
G((i-1)*BlockSize*(options.k+1)+1:nSmp*(options.k+1),3) = dump(:);
else
smpIdx = (i-1)*BlockSize+1:i*BlockSize;
dist = fea(smpIdx,:)*fea';
dist = full(dist);
if bSpeed
nSmpNow = length(smpIdx);
dump = zeros(nSmpNow,options.k+1);
idx = dump;
for j = 1:options.k+1
[dump(:,j),idx(:,j)] = max(dist,[],2);
temp = (idx(:,j)-1)*nSmpNow+[1:nSmpNow]';
dist(temp) = 0;
end
else
[dump idx] = sort(-dist,2); % sort each row
idx = idx(:,1:options.k+1);
dump = -dump(:,1:options.k+1);
end
G((i-1)*BlockSize*(options.k+1)+1:i*BlockSize*(options.k+1),1) = repmat(smpIdx',[options.k+1,1]);
G((i-1)*BlockSize*(options.k+1)+1:i*BlockSize*(options.k+1),2) = idx(:);
G((i-1)*BlockSize*(options.k+1)+1:i*BlockSize*(options.k+1),3) = dump(:);
end
end
W = sparse(G(:,1),G(:,2),G(:,3),nSmp,nSmp);
end
if bBinary
W(logical(W)) = 1;
end
if isfield(options,'bSemiSupervised') && options.bSemiSupervised
tmpgnd = options.gnd(options.semiSplit);
Label = unique(tmpgnd);
nLabel = length(Label);
G = zeros(sum(options.semiSplit),sum(options.semiSplit));
for idx=1:nLabel
classIdx = tmpgnd==Label(idx);
G(classIdx,classIdx) = 1;
end
Wsup = sparse(G);
if ~isfield(options,'SameCategoryWeight')
options.SameCategoryWeight = 1;
end
W(options.semiSplit,options.semiSplit) = (Wsup>0)*options.SameCategoryWeight;
end
if ~options.bSelfConnected
W = W - diag(diag(W));
end
if isfield(options,'bTrueKNN') && options.bTrueKNN
else
W = max(W,W');
end
return;
end
% strcmpi(options.NeighborMode,'KNN') & (options.k == 0)
% Complete Graph
switch lower(options.WeightMode)
case {lower('Binary')}
error('Binary weight can not be used for complete graph!');
case {lower('HeatKernel')}
W = EuDist2(fea,[],0);
W = exp(-W/(2*options.t^2));
case {lower('Cosine')}
W = full(Normfea*Normfea');
otherwise
error('WeightMode does not exist!');
end
if ~options.bSelfConnected
for i=1:size(W,1)
W(i,i) = 0;
end
end
W = max(W,W');
end
function H = init_H(XX,c,options)
%XX: each row represents a sample.
%There are two options to initialize H (i.e., k-means or spectral clusterig).
n = size(XX,1);
if (~exist('options','var'))
options = [];
end
if ~isfield(options,'init_method')
options.init_method = 'SC'; %spectral clustering
end
switch lower(options.init_method)
case {lower('SC')}
[~,H] = ADA_ORTH_init(XX,c);
case {lower('k_means')}
labels = litekmeans(XX,c,'MaxIter',20,'Replicates',2);
H = zeros(n,c);
for i = 1:n
H(i,labels(i)) = 1;
end
H = H ./ repmat(sqrt(sum(H)),n,1);
otherwise
error('The init_method does not exist!');
end
end
function [A,H] = ADA_ORTH_init(X,c)
%input: X -- each row represents a sample.
% c -- cluster number.
%Construct the affinity matrix
t = optSigma(X)^2; %Median of Euclidean distance between all two samples
A = constructW(X, struct('k',5, 'WeightMode', 'HeatKernel', 't', t));
diag_ele_arr = sum(A);
diag_ele_arr_t = diag_ele_arr.^(-1/2);
L = eye(size(X,1)) - diag(diag_ele_arr_t)* A *diag(diag_ele_arr_t);
L = (L + L')/2;
[eigvec, eigval] = eig(L);
[~, t1] = sort(diag(eigval), 'ascend');
eigvec = eigvec(:, t1(1:c));
eigvec = bsxfun(@rdivide, eigvec, sqrt(sum(eigvec.^2,2) + eps)); %Normalized eigenvector
%init H
rand('twister',5489);
label = litekmeans(eigvec,c,'Replicates',10);
H = rand(size(X,1),c);
for i = 1:size(X,1)
H(i,label(i)) = 1;
end
H = H + 0.2;
end
function delta = solve_delta(S,A)
% Problem
%
% min sigma(v=1 to V)|| S - deta^v * A^v||^2
% s.t. deta>=0, 1'deta=1
%
View = size(A,2);
p = [];
q = [];
for v = 1:View
p_v = trace(A{1,v} * S');
q_v = trace(A{1,v} * A{1,v}');
p = [p; p_v];
q = [q; q_v];
end
g = [];
for v = 1:View
g_v = p(v) / q(v) + ( 1-sum(p./q) ) / ( q(v)*sum(1./q) );
g = [g; g_v ];
end
gmin = min(g);
ft = 1; %Maximum number of iterations of Newton method
if gmin < 0
f = 1; %Initial function value
miu = 0; %Initial miu
sumq = sum(1./q);
der_temp = ( 1/sumq )./q; %Coefficient of derivative of summation term
while abs(f) > 10^-10 %Until we find the root
max0 = ( miu/sumq )./q - g; %Summation term
posidx = max0>0; %The part whose summation term is greater than 0
der = der_temp(posidx) -1; %Derivative of iteration point
f = sum(max0(posidx)) - miu; %Function value of iteration point
miu = miu - f/der;
ft = ft + 1;
if ft > 1000
break;
end
end
delta_temp = g - ( miu/sumq ) ./ q ;
delta = max(delta_temp,0);
else
delta = max(g,0);
end
end
function S = Update_S(A,Y,V,beta,gamma,delta)
n = size(A{1,1},1);
G = cell(1,V);
for i = 1:V
G{1,i} = L2_distance_1(Y{1,i},Y{1,i});
end
S = zeros(n);
for i = 1:n
gi = zeros(1,n);
deltaAi = zeros(1,n);
for i1 = 1:V
gi = gi + G{1,i1}(i,:);
deltaAi = deltaAi + 2 * delta(i1) * A{1,i1}(i,:);
end
gi_temp = gamma / (2 * beta) * gi;
r = ( deltaAi - gi_temp ) / (2 * V);
S(i,:) = EProjSimplex_new(r,1);
end
end
function [x ] = EProjSimplex_new(v, k)%v:f+x disntance de fushu
% the reason to do this is that, which point should have similarity to
% current point or how many points is not sure,so we should interation to decide which point is
% near to current point and the number. this is done by using threshold
%
%% Problem
%
% min 1/2 || x - v||^2
% s.t. x>=0, 1'x=1
%
if nargin < 2
k = 1;
end
ft=1;
n = length(v);%15
v0 = v-mean(v) + k/n;%v0: 1*15 v-mean(v)+1/15
%vmax = max(v0);
vmin = min(v0);
if vmin < 0
f = 1;
lambda_m = 0;
while abs(f) > 10^-10%f is residue,it is the sum of similarity of chosen point, %if it is closed enough to 0,we think the chosen points is good enough
v1 = v0 - lambda_m;
posidx = v1>0;
npos = sum(posidx);
g = -npos;
f = sum(v1(posidx)) - k;%k=1
%lambda_m is used to control sum(v1(posidx)), if it is bigger than 1,then f is positive,
%then,lambda_m will raise to let less point be neighboor to current point; if it is litter than 1, then f is neg,
%lambda_m will decrease to let more point be neighboor to current point.
lambda_m = lambda_m - f/g;
ft=ft+1;
if ft > 1000
x = max(v1,0);
break;
end
end
x = max(v1,0);
else
x = v0;
end
end
function D = EuDist2(fea_a,fea_b,bSqrt)
%EUDIST2 Efficiently Compute the Euclidean Distance Matrix by Exploring the
%Matlab matrix operations.
%
% D = EuDist(fea_a,fea_b)
% fea_a: nSample_a * nFeature
% fea_b: nSample_b * nFeature
% D: nSample_a * nSample_a
% or nSample_a * nSample_b
%
% Examples:
%
% a = rand(500,10);
% b = rand(1000,10);
%
% A = EuDist2(a); % A: 500*500
% D = EuDist2(a,b); % D: 500*1000
%
% version 2.1 --November/2011
% version 2.0 --May/2009
% version 1.0 --November/2005
%
% Written by Deng Cai (dengcai AT gmail.com)
if ~exist('bSqrt','var')
bSqrt = 1;
end
if (~exist('fea_b','var')) || isempty(fea_b)
aa = sum(fea_a.*fea_a,2);
ab = fea_a*fea_a';
if issparse(aa)
aa = full(aa);
end
D = bsxfun(@plus,aa,aa') - 2*ab;
D(D<0) = 0;
if bSqrt
D = sqrt(D);
end
D = max(D,D');
else
aa = sum(fea_a.*fea_a,2);
bb = sum(fea_b.*fea_b,2);
ab = fea_a*fea_b';
if issparse(aa)
aa = full(aa);
bb = full(bb);
end
D = bsxfun(@plus,aa,bb') - 2*ab;
D(D<0) = 0;
if bSqrt
D = sqrt(D);
end
end
end
% compute squared Euclidean distance
% ||A-B||^2 = ||A||^2 + ||B||^2 - 2*A'*B
function d = L2_distance_1(a,b)%x,x
% a,b: two matrices. each column is a data
% d: distance matrix of a and b
if (size(a,1) == 1)
a = [a; zeros(1,size(a,2))];
b = [b; zeros(1,size(b,2))];
end
aa=sum(a.*a); bb=sum(b.*b); ab=a'*b;
d = repmat(aa',[1 size(bb,2)]) + repmat(bb,[size(aa,2) 1]) - 2*ab;
d = real(d);
d = max(d,0);
% % force 0 on the diagonal?
% if (df==1)
% d = d.*(1-eye(size(d)));
% end
end
function result = norm_21(data)
B = data.*data;
c = sum(B,2);
D = sqrt(c);
result = sum(D);
end
function [label, center, bCon, sumD, D] = litekmeans(X, k, varargin)
%LITEKMEANS K-means clustering, accelerated by matlab matrix operations.
%
% label = LITEKMEANS(X, K) partitions the points in the N-by-P data matrix
% X into K clusters. This partition minimizes the sum, over all
% clusters, of the within-cluster sums of point-to-cluster-centroid
% distances. Rows of X correspond to points, columns correspond to
% variables. KMEANS returns an N-by-1 vector label containing the
% cluster indices of each point.
%
% [label, center] = LITEKMEANS(X, K) returns the K cluster centroid
% locations in the K-by-P matrix center.
%
% [label, center, bCon] = LITEKMEANS(X, K) returns the bool value bCon to
% indicate whether the iteration is converged.
%
% [label, center, bCon, SUMD] = LITEKMEANS(X, K) returns the
% within-cluster sums of point-to-centroid distances in the 1-by-K vector
% sumD.
%
% [label, center, bCon, SUMD, D] = LITEKMEANS(X, K) returns
% distances from each point to every centroid in the N-by-K matrix D.
%
% [ ... ] = LITEKMEANS(..., 'PARAM1',val1, 'PARAM2',val2, ...) specifies
% optional parameter name/value pairs to control the iterative algorithm
% used by KMEANS. Parameters are:
%
% 'Distance' - Distance measure, in P-dimensional space, that KMEANS
% should minimize with respect to. Choices are:
% {'sqEuclidean'} - Squared Euclidean distance (the default)
% 'cosine' - One minus the cosine of the included angle
% between points (treated as vectors). Each
% row of X SHOULD be normalized to unit. If
% the intial center matrix is provided, it
% SHOULD also be normalized.
%
% 'Start' - Method used to choose initial cluster centroid positions,