-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
796 lines (660 loc) · 26.8 KB
/
utils.py
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
import numpy as np
import matplotlib.pyplot as plt
import time
from munkres import Munkres
import itertools
# pytorch imports
import torch
import torch.nn as nn
# scikit-learn imports
from sklearn.neighbors import NearestNeighbors
from sklearn.cluster import KMeans
import sklearn.metrics
from sklearn.metrics.cluster import normalized_mutual_info_score
def createAffinitySSL(data, labels, ms, ms_normal, sigmaFlag, labeled_index, classNum, mu1, mu2):
'''
Computes SSL Affinity matrix
inputs:
data: array of data featrues
labels: array of data labels
ms: neighbors number per node
ms_normal: neighbor for the kernel std.
sigmaFlag: flag for the kernel variance calculation
labeled_index: labeled set index array
classNum: number of classes
mu1: unsupervised affinity parameter
mu2: labeled affinity parameter
returns:
y: the affinity matrix
'''
class_labeles = np.arange(0, classNum, 1)
n, m = data.shape
W = createAffinity(data, ms, ms_normal, sigmaFlag)
W = np.array(W)
W_max = np.max(W)
W_for_labeld = np.zeros((n, n))
W_for_labeld[:, labeled_index] = W[:, labeled_index]
W_for_labeld[labeled_index, :] = 0
W_for_labeld_T = W_for_labeld.T
W_for_labeld = 0.5 * (W_for_labeld_T + W_for_labeld)
lables_labled = np.ones(n) * (-1)
vals = labels[list(labeled_index)]
lables_labled[list(labeled_index)] = list(vals)
for i in range(classNum):
curr_group = class_labeles[i]
group_indx = np.where(lables_labled == curr_group)
group_indx = group_indx[0]
pairs = list(itertools.product(group_indx, repeat=2))
pairs_num = len(pairs)
for j in range(pairs_num):
W_for_labeld[pairs[j]] = W_max
W_all = mu1 * W + mu2 * W_for_labeld
for i in range(classNum - 1):
first_group = class_labeles[i]
first_group_indx = np.where(lables_labled == first_group)
first_group_indx = first_group_indx[0]
for j in range(i + 1, classNum):
sec_group = class_labeles[j]
sec_group_indx = np.where(lables_labled == sec_group)
sec_group_indx = sec_group_indx[0]
pairs = list(itertools.product(first_group_indx, sec_group_indx, repeat=1))
pairs_num = len(pairs)
for k in range(pairs_num):
pair_k_flip = np.flip(pairs[k])
W_all[pairs[k]] = 0
W_all[pair_k_flip[0], pair_k_flip[1]] = 0
W_all = (W_all + W_all.T) / 2
diag_indx = np.arange(0, n, 1)
W_all[diag_indx, diag_indx] = np.max(W_all)
W_all = torch.Tensor(W_all)
return W_all
def createAffinityWNLL(data, ms, ms_normal, sigmaFlag, labeled_index):
'''
Computes WNLL Affinity matrix
inputs:
data: array of data featrues
ms: neighbors number per node
ms_normal: neighbor for the kernel std.
sigmaFlag: flag for the kernel variance calculation
labeled_index: labeled set index array
returns:
y: the affinity matrix
'''
n, m = data.shape
S_size = len(labeled_index)
mu1 = 2
if S_size == 0:
mu2 = 0
else:
mu2 = (n / S_size)-1
W = createAffinity(data, ms, ms_normal, sigmaFlag)
W = np.array(W)
W_for_labeld = np.zeros((n, n))
W_for_labeld[:, labeled_index] = W[:, labeled_index]
W_for_labeld[labeled_index, :] = 0
W_for_labeld_T = W_for_labeld.T
W_for_labeld = 0.5 * (W_for_labeld_T + W_for_labeld)
W_all = mu1 * W + mu2 * W_for_labeld
W_all = torch.Tensor(W_all)
return W_all
def createAffinityMaxOnly(data, labels, ms, ms_normal, sigmaFlag, labeled_index, classNum, mu1, mu2):
'''
Computes contrastive affinity (positive) matrix
inputs:
data: array of data featrues
labels: array of data labels
ms: neighbors number per node
ms_normal: neighbor for the kernel std.
sigmaFlag: flag for the kernel variance calculation
labeled_index: labeled set index array
classNum: number of classes
mu1: unsupervised affinity parameter
mu2: labeled affinity parameter
returns:
y: the affinity matrix
'''
class_labeles = np.arange(0, classNum, 1)
n, m = data.shape
W = createAffinity(data, ms, ms_normal, sigmaFlag)
W = np.array(W)
W_max = np.max(W)
W_for_labeld = np.zeros((n, n))
lables_labled = np.ones(n) * (-1)
vals = labels[list(labeled_index)]
lables_labled[list(labeled_index)] = list(vals)
for i in range(classNum):
curr_group = class_labeles[i]
group_indx = np.where(lables_labled == curr_group)
group_indx = group_indx[0]
pairs = list(itertools.product(group_indx, repeat=2))
pairs_num = len(pairs)
for j in range(pairs_num):
W_for_labeld[pairs[j]] = W_max
W_all = mu1 * W + mu2 * W_for_labeld
W_all = (W_all + W_all.T) / 2
diag_indx = np.arange(0, n, 1)
W_all[diag_indx, diag_indx] = np.max(W_all)
W_all = torch.Tensor(W_all)
return W_all
def createAffinityDisconnectOnly(data, labels, ms, ms_normal, sigmaFlag, labeled_index, classNum, mu1, mu2):
'''
Computes contrastive affinity (negative) matrix
inputs:
data: array of data featrues
labels: array of data labels
ms: neighbors number per node
ms_normal: neighbor for the kernel std.
sigmaFlag: flag for the kernel variance calculation
labeled_index: labeled set index array
classNum: number of classes
mu1: unsupervised affinity parameter
mu2: labeled affinity parameter
returns:
y: the affinity matrix
'''
class_labeles = np.arange(0, classNum, 1)
n, m = data.shape
W = createAffinity(data, ms, ms_normal, sigmaFlag)
W_all = np.array(W)
lables_labled = np.ones(n) * (-1)
vals = labels[list(labeled_index)]
lables_labled[list(labeled_index)] = list(vals)
for i in range(classNum - 1):
first_group = class_labeles[i]
first_group_indx = np.where(lables_labled == first_group)
first_group_indx = first_group_indx[0]
for j in range(i + 1, classNum):
sec_group = class_labeles[j]
sec_group_indx = np.where(lables_labled == sec_group)
sec_group_indx = sec_group_indx[0]
pairs = list(itertools.product(first_group_indx, sec_group_indx, repeat=1))
pairs_num = len(pairs)
for k in range(pairs_num):
pair_k_flip = np.flip(pairs[k])
W_all[pairs[k]] = 0.0
W_all[pair_k_flip[0], pair_k_flip[1]] = 0.0
W_all = (W_all + W_all.T) / 2
diag_indx = np.arange(0, n, 1)
W_all[diag_indx, diag_indx] = np.max(W_all)
W_all = torch.Tensor(W_all)
return W_all
def createAffinity(data, ms, ms_normal, sigmaFlag):
'''
Computes unsupervised affinity matrix
inputs:
data: array of data featrues
ms: neighbors number per node
ms_normal: neighbor for the kernel std.
sigmaFlag: flag for the kernel variance calculation
returns:
y: the affinity matrix
'''
n = data.shape[0]
nbrs = NearestNeighbors(n_neighbors=ms, algorithm='kd_tree').fit(data)
dist, idx = nbrs.kneighbors(data)
graph_median = np.median(dist)
dist = torch.Tensor(dist.T)
idx = torch.Tensor(idx.T)
id_row = torch.Tensor([range(0, n)])
id_row = id_row.repeat(ms, 1)
id_row = id_row.numpy()
id_col = idx.numpy()
if sigmaFlag == 0:
sigma = torch.diag(1. / dist[ms_normal, :])
W = torch.exp(-(dist @ sigma) ** 2)
if sigmaFlag == 1:
sigma = torch.median(dist[ms_normal, :])
W = torch.exp(-dist ** 2 / (sigma ** 2))
if sigmaFlag == 2:
W = torch.exp(-dist ** 2 / (2 * graph_median ** 2))
if sigmaFlag == 3:
sigma = 1
W = torch.exp(-dist ** 2 / sigma)
y = torch.sparse_coo_tensor([id_row.flatten(), id_col.flatten()], W.flatten(), (n, n))
y = y.to_dense()
y = (y + y.T) / 2
return y
def ev_calculation_L(W, classNum):
'''
Computes the graph Laplacian eigenvectors
inputs:
W: affinity matrix
classNum: number of classes
returns:
RCut_EV: the first K eigenvectors of L
'''
s0 = torch.sum(W, axis=0)
# L
D = torch.diag(s0)
L = D - W
S_L, U_L = torch.linalg.eig(L)
S_L = torch.real(S_L)
U_L = torch.real(U_L)
S_L, indices = torch.sort(S_L, dim=0, descending=False, out=None)
U_L = U_L[:, indices]
RCut_EV = U_L[:, 1:classNum]
# RCut_EV = U_L[:, 0:classNum]
return RCut_EV
def ev_calculation_LN(W, classNum,):
'''
Computes the normalized graph Laplacian eigenvectors
inputs:
W: affinity matrix
classNum: number of classes
returns:
RCut_EV: the first K eigenvectors of L_N
'''
n = W.size(0)
s0 = torch.sum(W, axis=0)
# L_N
D_sqrt = torch.diag(1. / torch.sqrt(s0))
I = torch.eye(n)
N = I - D_sqrt @ W @ D_sqrt
S_N, U_N = torch.linalg.eig(N)
S_N = torch.real(S_N)
U_N = torch.real(U_N)
S_N, indices = torch.sort(S_N, dim=0, descending=False, out=None)
U_N = U_N[:, indices]
RCut_EV = U_N[:, 1:classNum]
# RCut_EV = U_N[:, 0:classNum]
return RCut_EV
def SpectralClusteringFromEV(ev, true_labels, classNum):
'''
performe spectral clutering from the spectral embedding
inputs:
ev: the eigenvectors of the graph Laplacian
true_labels: data true labels
classNum: number of classes
returns:
RCut_labels: spectral clustering assignment
model_nmi: nmi value
model_acc: acc value
'''
RCut_kmeans = KMeans(n_clusters=classNum, random_state=0).fit(ev)
RCut_labels = RCut_kmeans.labels_
model_nmi = normalized_mutual_info_score(true_labels, RCut_labels)
model_acc, _ = get_acc(RCut_labels, true_labels, classNum)
return RCut_labels, model_nmi, model_acc
# Performance measures
def get_orthogonality_measure(U, classNum):
'''
calcute the orthogonality measure
inputs:
U: the matrix whose orthogonality is tested
classNum: number of classes
returns:
orthogonality_measure: orthogonality measure
'''
n, m = U.shape
ev_norm = np.linalg.norm(U, axis=0)
ev_norm = 1 / ev_norm
ev_norm_matrix = np.tile(ev_norm, (m, 1))
orthogonality_matrix = U.T @ U
orthogonality_matrix = np.multiply(np.multiply(ev_norm_matrix.T, orthogonality_matrix), ev_norm_matrix)
dim = orthogonality_matrix.shape[0]
I = np.eye(dim)
orthogonality_measure = np.linalg.norm(orthogonality_matrix - I)
return orthogonality_measure
def grassmann(A, B):
'''
calcute grassmann distance
inputs:
A, B: the matrices for which the distance is checked
returns:
grassmann_val: grassmann distance between A and B
'''
n, m = A.shape
A_col_norm = torch.linalg.norm(A, dim=0)
A_col_norm = 1 / A_col_norm
A_norm_matrix = torch.tile(A_col_norm, (n, 1))
A_normalized = A_norm_matrix * A #elmentwise
A_normalized = A_normalized.float()
B_col_norm = torch.linalg.norm(B, dim=0)
B_col_norm = 1 / B_col_norm
B_norm_matrix = torch.tile(B_col_norm, (n, 1))
B_normalized = B_norm_matrix * B #elmentwise
B_normalized = B_normalized.float()
M = A_normalized.T @ B_normalized
_, s, _ = torch.linalg.svd(M) # return ev with norm 1
s = 1 - torch.square(s)
grassmann_val = torch.sum(s)
return grassmann_val
def get_cluster_labels_from_indices(indices):
n_clusters = len(indices)
clusterLabels = np.zeros(n_clusters)
for i in range(n_clusters):
clusterLabels[i] = indices[i][1]
return clusterLabels
def calculate_cost_matrix(C, n_clusters):
cost_matrix = np.zeros((n_clusters, n_clusters))
# cost_matrix[i,j] will be the cost of assigning cluster i to label j
for j in range(n_clusters):
s = np.sum(C[:, j]) # number of examples in cluster i
for i in range(n_clusters):
t = C[i, j]
cost_matrix[j, i] = s - t
return cost_matrix
def get_y_preds(cluster_assignments, y_true, n_clusters):
'''
Computes the predicted labels, where label assignments now
correspond to the actual labels in y_true (as estimated by Munkres)
cluster_assignments: array of labels, outputted by kmeans
y_true: true labels
n_clusters: number of clusters in the dataset
returns: a tuple containing the accuracy and confusion matrix,
in that order
'''
confusion_matrix = sklearn.metrics.confusion_matrix(y_true, cluster_assignments, labels=None)
# compute accuracy based on optimal 1:1 assignment of clusters to labels
cost_matrix = calculate_cost_matrix(confusion_matrix, n_clusters)
indices = Munkres().compute(cost_matrix)
kmeans_to_true_cluster_labels = get_cluster_labels_from_indices(indices)
y_pred = kmeans_to_true_cluster_labels[cluster_assignments]
return y_pred, confusion_matrix
def get_acc(cluster_assignments, y_true, n_clusters):
'''
Computes the accuracy based on the provided kmeans cluster assignments
and true labels, using the Munkres algorithm
cluster_assignments: array of labels, outputted by kmeans
y_true: true labels
n_clusters: number of clusters in the dataset
returns: a tuple containing the accuracy and confusion matrix,
in that order
'''
y_true = y_true.numpy()
y_pred, confusion_matrix = get_y_preds(cluster_assignments, y_true, n_clusters)
# calculate the accuracy
return np.mean(y_pred == y_true), confusion_matrix
def Dirichlet_Clustering(L, labeled_index, unlabeled_index, y, classes):
'''
Dirichlet multiclass clustering via interpolation
inputs:
L: graph Laplacian
labeled_index: labeled set index array
unlabeled_index: unlabeled set index array
y: true labeles
classes: classes array
returns:
grassmann_val: grassmann distance between A and B
'''
#classes should be numbers from 0 to classNum-1.
n = L.shape[0]
classNum = len(classes)
y_labeled = y[labeled_index]
y_unlabeled = y[unlabeled_index]
A = L.clone()
A[labeled_index, :] = 0
A[labeled_index, labeled_index] = 1
for i in range(classNum):
b = torch.zeros(n)
b[labeled_index] = 1
b[y != i] = 0
phi = torch.linalg.lstsq(A, b, rcond=None).solution
phi_unlabeled = phi[unlabeled_index]
phi_unlabeled = torch.unsqueeze(phi_unlabeled, 1)
if i == 0:
totalPhi = phi_unlabeled
else:
totalPhi = torch.cat((totalPhi, phi_unlabeled), dim=1)
max_values, clusteringRes = torch.max(totalPhi, 1)
model_nmi = normalized_mutual_info_score(y_unlabeled, clusteringRes)
model_acc, _ = get_acc(clusteringRes, y_unlabeled, classNum)
return clusteringRes, model_nmi, model_acc
def Dirichlet_Interploation(L, labeled_index, unlabeled_index, y, classes):
'''
Dirichlet interpolation solution (2 classes)
inputs:
L: graph Laplacian
labeled_index: labeled set index array
unlabeled_index: unlabeled set index array
y: true labeles
classes: classes array
returns:
grassmann_val: grassmann distance between A and B
'''
n = L.shape[0]
classNum = len(classes)
y_labeled = y[labeled_index]
indx0_labeled = np.where(y_labeled == 0)[0]
labeled_index0 = np.array(labeled_index)[indx0_labeled.astype(int)]
indx1_labeled = np.where(y_labeled == 1)[0]
labeled_index1 = np.array(labeled_index)[indx1_labeled.astype(int)]
y_unlabeled = y[unlabeled_index]
A = L.clone()
A[labeled_index, :] = 0
A[labeled_index, labeled_index] = 1
b = torch.zeros(n)
b[labeled_index0] = -1
b[labeled_index1] = 1
phi = torch.linalg.lstsq(A, b, rcond=None).solution
return phi
def TwoMoons_SSL_Solutions(X, y, option_index, ms, ms_normal, sigmaFlag, classes, mu1, labeled_index, model_path):
'''
SSL solutions for 2 moons dataset
inputs:
X: array of data featrues
y: array of data labels
option_index: index of current labeled subset
ms: neighbors number per node
ms_normal: neighbor for the kernel std.
sigmaFlag: flag for the kernel variance calculation
classes: classes array
mu1: unsupervised affinity parameter
labeled_index: labeled set index array
L: graph Laplacian
labeled_index: labeled set index array
unlabeled_index: unlabeled set index array
y: true labeles
classes: classes array
model_path: path for images
'''
print("option: ", option_index)
n_samples = len(X)
nodes_indx_list = range(0, n_samples)
classNum = len(classes)
unlabeled_index = [indx for indx in nodes_indx_list if indx not in labeled_index]
fig, ax = plt.subplots(1, 1, figsize=(12, 5))
ax.scatter(X[unlabeled_index, 0], X[unlabeled_index, 1], c='white', edgecolor="blue")
ax.scatter(X[labeled_index, 0], X[labeled_index, 1], c='red', s=100)
ax.axes.xaxis.set_ticklabels([])
ax.axes.yaxis.set_ticklabels([])
plt.grid(True)
savefig_path = model_path + "/images/labeled_option_" + str(option_index) + ".png"
plt.savefig(savefig_path)
plt.show()
number_of_labeled_nodes = len(labeled_index)
mu2 = (n_samples / number_of_labeled_nodes) - 1
y_unlabeled = y[unlabeled_index]
X0_unlabeled = X[unlabeled_index, 0]
X0_unlabeled_index = np.argmax(X0_unlabeled)
print("Spectral WNLL")
W_WNLL = createAffinityWNLL(X, ms, ms_normal, sigmaFlag, labeled_index)
ev = ev_calculation_L(W_WNLL, classNum)
ev_unlabeled = ev[unlabeled_index]
RCut_labels, model_nmi, model_acc = SpectralClusteringFromEV(ev_unlabeled, y[unlabeled_index], classNum)
print("NMI:", model_nmi)
print("ACC:", model_acc)
RCut_labels_max = RCut_labels[X0_unlabeled_index]
if RCut_labels_max == 0:
RCut_labels[RCut_labels == 1] = -1
RCut_labels[RCut_labels == 0] = 1
RCut_labels[RCut_labels == -1] = 0
fig = plt.figure(figsize=(12, 5))
ax = fig.add_subplot(1, 1, 1)
sc = ax.scatter(X[labeled_index, 0], X[labeled_index, 1], c=y[labeled_index])
ax.scatter(X[unlabeled_index, 0], X[unlabeled_index, 1], c=RCut_labels)
# ax.set_title("Moons Dataset")
ax.axes.xaxis.set_ticklabels([])
ax.axes.yaxis.set_ticklabels([])
plt.grid(True)
#plt.colorbar(sc)
savefig_path = model_path + "/images/spectral_wnll_option_" + str(option_index) + ".png"
plt.savefig(savefig_path)
plt.show()
fig = plt.figure(figsize=(12, 5))
ax = fig.add_subplot(1, 1, 1)
sc = ax.scatter(X[:, 0], X[:, 1], c=ev)
# ax.set_title("Moons Dataset")
ax.axes.xaxis.set_ticklabels([])
ax.axes.yaxis.set_ticklabels([])
plt.grid(True)
#plt.colorbar(sc)
savefig_path = model_path + "/images/spectral_wnll_option_" + str(option_index) + "_ev.png"
plt.savefig(savefig_path)
plt.show()
print("Spectral SSL")
W_ssl = createAffinitySSL(X, y, ms, ms_normal, sigmaFlag, labeled_index, classNum, mu1, mu2)
ev = ev_calculation_L(W_ssl, classNum)
ev_unlabeled = ev[unlabeled_index]
RCut_labels, model_nmi, model_acc = SpectralClusteringFromEV(ev_unlabeled, y[unlabeled_index], classNum)
print("NMI:", model_nmi)
print("ACC:", model_acc)
RCut_labels_max = RCut_labels[X0_unlabeled_index]
if RCut_labels_max == 0:
RCut_labels[RCut_labels == 1] = -1
RCut_labels[RCut_labels == 0] = 1
RCut_labels[RCut_labels == -1] = 0
fig = plt.figure(figsize=(12, 5))
ax = fig.add_subplot(1, 1, 1)
sc = ax.scatter(X[labeled_index, 0], X[labeled_index, 1], c=y[labeled_index])
ax.scatter(X[unlabeled_index, 0], X[unlabeled_index, 1], c=RCut_labels)
# ax.set_title("Moons Dataset")
ax.axes.xaxis.set_ticklabels([])
ax.axes.yaxis.set_ticklabels([])
plt.grid(True)
#plt.colorbar(sc)
savefig_path = model_path + "/images/spectral_ssl_option_" + str(option_index) + ".png"
plt.savefig(savefig_path)
plt.show()
fig = plt.figure(figsize=(12, 5))
ax = fig.add_subplot(1, 1, 1)
sc = ax.scatter(X[:, 0], X[:, 1], c=ev)
# ax.set_title("Moons Dataset")
ax.axes.xaxis.set_ticklabels([])
ax.axes.yaxis.set_ticklabels([])
plt.grid(True)
#plt.colorbar(sc)
savefig_path = model_path + "/images/spectral_ssl_option_" + str(option_index) + "_ev.png"
plt.savefig(savefig_path)
plt.show()
print("Dirichlet US")
W_US = createAffinity(X, ms, ms_normal, sigmaFlag)
s0 = torch.sum(W_US, axis=0)
D = torch.diag(s0)
L = D - W_US
phi = Dirichlet_Interploation(L, labeled_index, unlabeled_index, y, classes)
clusteringRes_kmeans = KMeans(n_clusters=classNum, random_state=0).fit(phi.reshape(-1, 1))
clusteringRes = clusteringRes_kmeans.labels_
clusteringRes = clusteringRes[unlabeled_index]
model_nmi = normalized_mutual_info_score(y_unlabeled, clusteringRes)
model_acc, _ = get_acc(clusteringRes, y_unlabeled, classNum)
print("NMI:", model_nmi)
print("ACC:", model_acc)
clusteringRes_max = clusteringRes[X0_unlabeled_index]
if clusteringRes_max == 0:
clusteringRes[clusteringRes == 1] = -1
clusteringRes[clusteringRes == 0] = 1
clusteringRes[clusteringRes == -1] = 0
fig = plt.figure(figsize=(12, 5))
ax = fig.add_subplot(1, 1, 1)
sc = ax.scatter(X[:, 0], X[:, 1], c=phi)
# ax.set_title("Moons Dataset")
ax.axes.xaxis.set_ticklabels([])
ax.axes.yaxis.set_ticklabels([])
plt.grid(True)
#plt.colorbar(sc)
savefig_path = model_path + "/images/dirichlet_US_option_" + str(option_index) + "_phi.png"
plt.savefig(savefig_path)
plt.show()
fig = plt.figure(figsize=(12, 5))
ax = fig.add_subplot(1, 1, 1)
sc = ax.scatter(X[labeled_index, 0], X[labeled_index, 1], c=y[labeled_index])
ax.scatter(X[unlabeled_index, 0], X[unlabeled_index, 1], c=clusteringRes)
# ax.set_title("Moons Dataset")
ax.axes.xaxis.set_ticklabels([])
ax.axes.yaxis.set_ticklabels([])
plt.grid(True)
#plt.colorbar(sc)
savefig_path = model_path + "/images/dirichlet_US_option_" + str(option_index) + ".png"
plt.savefig(savefig_path)
plt.show()
print("Dirichlet WNLL")
s0 = torch.sum(W_WNLL, axis=0)
D = torch.diag(s0)
L = D - W_WNLL
phi = Dirichlet_Interploation(L, labeled_index, unlabeled_index, y, classes)
clusteringRes_kmeans = KMeans(n_clusters=classNum, random_state=0).fit(phi.reshape(-1, 1))
clusteringRes = clusteringRes_kmeans.labels_
clusteringRes = clusteringRes[unlabeled_index]
model_nmi = normalized_mutual_info_score(y_unlabeled, clusteringRes)
model_acc, _ = get_acc(clusteringRes, y_unlabeled, classNum)
print("NMI:", model_nmi)
print("ACC:", model_acc)
clusteringRes_max = clusteringRes[X0_unlabeled_index]
if clusteringRes_max == 0:
clusteringRes[clusteringRes == 1] = -1
clusteringRes[clusteringRes == 0] = 1
clusteringRes[clusteringRes == -1] = 0
fig = plt.figure(figsize=(12, 5))
ax = fig.add_subplot(1, 1, 1)
sc = ax.scatter(X[:, 0], X[:, 1], c=phi)
# ax.set_title("Moons Dataset")
ax.axes.xaxis.set_ticklabels([])
ax.axes.yaxis.set_ticklabels([])
plt.grid(True)
#plt.colorbar(sc)
savefig_path = model_path + "/images/dirichlet_WNLL_option_" + str(option_index) + "_phi.png"
plt.savefig(savefig_path)
plt.show()
fig = plt.figure(figsize=(12, 5))
ax = fig.add_subplot(1, 1, 1)
sc = ax.scatter(X[labeled_index, 0], X[labeled_index, 1], c=y[labeled_index])
ax.scatter(X[unlabeled_index, 0], X[unlabeled_index, 1], c=clusteringRes)
# ax.set_title("Moons Dataset")
ax.axes.xaxis.set_ticklabels([])
ax.axes.yaxis.set_ticklabels([])
plt.grid(True)
#plt.colorbar(sc)
savefig_path = model_path + "/images/dirichlet_WNLL_option_" + str(option_index) + ".png"
plt.savefig(savefig_path)
plt.show()
print("Dirichlet SSL")
s0 = torch.sum(W_ssl, axis=0)
D = torch.diag(s0)
L = D - W_ssl
phi = Dirichlet_Interploation(L, labeled_index, unlabeled_index, y, classes)
clusteringRes_kmeans = KMeans(n_clusters=classNum, random_state=0).fit(phi.reshape(-1, 1))
clusteringRes = clusteringRes_kmeans.labels_
clusteringRes = clusteringRes[unlabeled_index]
model_nmi = normalized_mutual_info_score(y_unlabeled, clusteringRes)
model_acc, _ = get_acc(clusteringRes, y_unlabeled, classNum)
print("NMI:", model_nmi)
print("ACC:", model_acc)
clusteringRes_max = clusteringRes[X0_unlabeled_index]
if clusteringRes_max == 0:
clusteringRes[clusteringRes == 1] = -1
clusteringRes[clusteringRes == 0] = 1
clusteringRes[clusteringRes == -1] = 0
fig = plt.figure(figsize=(12, 5))
ax = fig.add_subplot(1, 1, 1)
sc = ax.scatter(X[:, 0], X[:, 1], c=phi)
# ax.set_title("Moons Dataset")
ax.axes.xaxis.set_ticklabels([])
ax.axes.yaxis.set_ticklabels([])
plt.grid(True)
#plt.colorbar(sc)
savefig_path = model_path + "/images/dirichlet_SSL_option_" + str(option_index) + "_phi.png"
plt.savefig(savefig_path)
plt.show()
fig = plt.figure(figsize=(12, 5))
ax = fig.add_subplot(1, 1, 1)
sc = ax.scatter(X[labeled_index, 0], X[labeled_index, 1], c=y[labeled_index])
ax.scatter(X[unlabeled_index, 0], X[unlabeled_index, 1], c=clusteringRes)
# ax.set_title("Moons Dataset")
ax.axes.xaxis.set_ticklabels([])
ax.axes.yaxis.set_ticklabels([])
plt.grid(True)
#plt.colorbar(sc)
savefig_path = model_path + "/images/dirichlet_SSL_option_" + str(option_index) + ".png"
plt.savefig(savefig_path)
plt.show()