-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlabel_opt.py
More file actions
77 lines (62 loc) · 1.63 KB
/
label_opt.py
File metadata and controls
77 lines (62 loc) · 1.63 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
import torch
import numpy as np
from scipy.optimize import linear_sum_assignment
def labelOpt(X_hat,X,lr=0.001,lamda=25,ITRS = 10000,device='cuda:0'):
'''
X_hat:[1xqxq]
X: [1xqxq]
'''
size = X.shape[-1]
P = torch.zeros(X.shape[0],size,size).to(device)
X_hat_ = X_hat.squeeze(dim=0).detach().cpu().numpy()
X_t = X.squeeze(dim=0).t().cpu().numpy()
r_i,c_i = linear_sum_assignment(X_t@X_hat_,maximize=True)
P[:,r_i,c_i] = 1
X_bar = X @ P
return X_bar
def lexOpt(_,X,lr=0.001,lamda=25,ITRS = 10000,device='cuda:0'):
X_bar = X
newX = X_bar.detach()
X_bar = X.cpu().numpy().astype(int).astype(str)
n,nr,nc = X_bar.shape
for i in range(n):
Y = X_bar[i]
Y = list(Y.transpose())
Y = [ ''.join(list(y)) for y in Y]
Y = np.array(Y)
inds = np.argsort(Y)[::-1]
newY = X[i][:,list(inds)]
newX[i] = newY
return newX
if __name__ == '__main__':
# X = torch.Tensor(
# [
# [0, 0, 1],
# [1, 0, 0],
# [0, 1, 0]
# ]
# )
#
# X_hat = torch.Tensor(
# [
# [0.5, 0.3, 0.2],
# [0.1, 0.6, 0.2],
# [0.2, 0.1, 0.6]
# ]
# )
X = torch.Tensor(
[
[0, 0, 1],
[1, 0, 0],
[0, 1, 0]
]
)
X_hat = torch.Tensor(
[
[0, 1, 0],
[0, 1, 0],
[0, 0, 1]
]
)
X_bar = labelOpt(X_hat[None,:,:].clone(),X[None,:,:].clone(),device='cpu')
print('done')