-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_losses.py
117 lines (86 loc) · 3.87 KB
/
test_losses.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
from lambdaloss import lambdaLoss
from approxndcg import approxNDCGLoss
from neuralndcg import neuralNDCGLoss
from unbiased import upu, nnpu
import torch
import unittest
class LossesTest(unittest.TestCase):
def test_lambdarank(self):
y_true = torch.Tensor((0, 0, 0, 0))
y_pred = torch.Tensor((0, 0, 0, 0))
minimal_loss = lambdaLoss(y_pred, y_true)
self.assertEqual(minimal_loss, 0)
y_true = torch.Tensor((1, 0, 0, 0))
y_pred = torch.Tensor((1, 0, 0, 0))
one_correct = lambdaLoss(y_pred, y_true)
y_true = torch.Tensor((1, 0, 0, 0))
y_pred = torch.Tensor((1, 1, 0, 0))
false_positive = lambdaLoss(y_pred, y_true)
y_true = torch.Tensor((1, 0, 0, 0))
y_pred = torch.Tensor((0, 0, 0, 0))
false_negative = lambdaLoss(y_pred, y_true)
self.assertLess(one_correct, false_positive)
self.assertLess(one_correct, false_negative)
self.assertLess(false_positive, false_negative)
def test_approxNDCG(self):
y_true = torch.Tensor((0, 0, 0, 0))
y_pred = torch.Tensor((0, 0, 0, 0))
minimal_loss = approxNDCGLoss(y_pred, y_true)
self.assertEqual(minimal_loss, 0)
y_true = torch.Tensor((1, 0, 0, 0))
y_pred = torch.Tensor((1, 0, 0, 0))
one_correct = approxNDCGLoss(y_pred, y_true)
y_true = torch.Tensor((1, 0, 0, 0))
y_pred = torch.Tensor((1, 1, 0, 0))
false_positive = approxNDCGLoss(y_pred, y_true)
y_true = torch.Tensor((1, 0, 0, 0))
y_pred = torch.Tensor((0, 0, 0, 0))
false_negative = approxNDCGLoss(y_pred, y_true)
self.assertLess(one_correct, false_positive)
self.assertLess(one_correct, false_negative)
self.assertLess(false_positive, false_negative)
def test_neuralNDCG(self):
y_true = torch.DoubleTensor((0, 0, 0, 0))
y_pred = torch.DoubleTensor((0, 0, 0, 0))
minimal_loss = neuralNDCGLoss(y_pred, y_true)
self.assertEqual(minimal_loss, 0)
y_true = torch.DoubleTensor((1, 0, 0, 0))
y_pred = torch.DoubleTensor((1, 0, 0, 0))
one_correct = neuralNDCGLoss(y_pred, y_true)
y_true = torch.DoubleTensor((1, 0, 0, 0))
y_pred = torch.DoubleTensor((1, 1, 0, 0))
false_positive = neuralNDCGLoss(y_pred, y_true)
y_true = torch.DoubleTensor((1, 0, 0, 0))
y_pred = torch.DoubleTensor((0, 0, 0, 0))
false_negative = neuralNDCGLoss(y_pred, y_true)
self.assertLess(one_correct, false_positive)
self.assertLess(one_correct, false_negative)
self.assertLess(false_positive, false_negative)
def test_upu(self):
y_true = torch.DoubleTensor((1, 0, 0, 0))
y_pred = torch.DoubleTensor((1, 0, 0, 0))
one_correct = upu(y_pred, y_true)
y_true = torch.DoubleTensor((1, 0, 0, 0))
y_pred = torch.DoubleTensor((1, 1, 0, 0))
false_positive = upu(y_pred, y_true)
y_true = torch.DoubleTensor((1, 0, 0, 0))
y_pred = torch.DoubleTensor((0, 1, 0, 0))
one_wrong = upu(y_pred, y_true)
self.assertLess(one_correct, false_positive)
self.assertLess(one_correct, one_wrong)
self.assertLess(false_positive, one_wrong)
def test_nnpu(self):
y_true = torch.DoubleTensor((1, 0, 0, 0))
y_pred = torch.DoubleTensor((1, 0, 0, 0))
one_correct = nnpu(y_pred, y_true)
y_true = torch.DoubleTensor((1, 0, 0, 0))
y_pred = torch.DoubleTensor((1, 1, 0, 0))
false_positive = nnpu(y_pred, y_true)
y_true = torch.DoubleTensor((1, 0, 0, 0))
y_pred = torch.DoubleTensor((0, 1, 0, 0))
one_wrong = nnpu(y_pred, y_true)
self.assertLessEqual(one_correct, false_positive)
self.assertLess(one_correct, one_wrong)
self.assertLess(false_positive, one_wrong)
if __name__ == '__main__':
unittest.main(warnings='ignore')