-
Notifications
You must be signed in to change notification settings - Fork 3
/
accuracy.lua
43 lines (40 loc) · 1.22 KB
/
accuracy.lua
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
-- match number
local function Accuracy(label, pred_prob)
local num_nonz = label:size(2)
local _, pred = pred_prob:topk(num_nonz, true)
pred = pred:float()
local t_score = torch.zeros(label:size())
for i = 1, num_nonz do
for j = 1, num_nonz do
t_score[{{},i}]:add(label[{{},i}]:eq(pred[{{},j}]):float())
end
end
return t_score:mean()
end
-- loose match
local function Accuracy2(label, pred_prob)
local num_nonz = label:size(2)
local _, pred = pred_prob:topk(20, true)
pred = pred:float()
local t_score = torch.zeros(label:size())
for i = 1, num_nonz do
for j = 1, 20 do
t_score[{{},i}]:add(label[{{},i}]:eq(pred[{{},j}]):float())
end
end
return t_score:mean()
end
-- strict match
local function Accuracy3(label, pred_prob)
local num_nonz = label:size(2)
local _, pred = pred_prob:topk(num_nonz, true)
pred = pred:float()
local t_score = torch.zeros(label:size())
for i = 1, num_nonz do
for j = 1, num_nonz do
t_score[{{},i}]:add(label[{{},i}]:eq(pred[{{},j}]):float())
end
end
return t_score:sum(2):eq(num_nonz):sum() * 1. / pred:size(1)
end
return {Accuracy, Accuracy2, Accuracy3}