-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ranking_loss.py
60 lines (51 loc) · 1.88 KB
/
Ranking_loss.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
import numpy as np
def Ranking_loss(Outputs,test_target):
num_class = Outputs.shape[0]
num_instance = Outputs.shape[1]
temp_Outputs = []
temp_test_target = []
for i in range(num_instance):
temp = test_target[:, i]
if ((np.sum(temp)!=num_class) and (np.sum(temp)!=-num_class)):
if (len(temp_Outputs) != 0):
temp_Outputs = np.c_[temp_Outputs, Outputs[:, i]]
else:
temp_Outputs = Outputs[:, i]
if (len(temp_test_target) != 0):
temp_test_target = np.c_[temp_test_target, temp]
else:
temp_test_target = temp
Outputs = temp_Outputs
test_target = temp_test_target
num_class = Outputs.shape[0]
num_instance = Outputs.shape[1]
Label = {}
not_Label = {}
Label_size = np.zeros((1, num_instance))
for i in range(num_instance):
temp = test_target[:, i]
Label_size[0, i] = np.sum(temp == np.ones(num_class))
for j in range(num_class):
if (temp[j] == 1):
if(i in Label.keys()):
Label[i] = np.r_[Label[i], j]
else:
Label[i] = np.array([j])
else:
if (i in not_Label.keys()):
not_Label[i] = np.r_[not_Label[i], j]
else:
not_Label[i] = np.array([j])
rankloss = 0
rl_binary = []
for i in range(num_instance):
temp = 0
for m in range(int(Label_size[0, i])):
for n in range(int(num_class - Label_size[0, i])):
if (Outputs[Label[i][m], i] <= Outputs[not_Label[i][n], i]):
temp = temp + 1
t = Label_size[0, i] * (num_class - Label_size[0, i])
rl_binary.append(temp / t)
rankloss = rankloss + temp / t
RankingLoss = rankloss / num_instance
return RankingLoss