/
t_logsumexp.py
99 lines (66 loc) · 2.37 KB
/
t_logsumexp.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
"unit tests for log-sum-exp functions"
import unittest
import numpy as np
from numpy import array, arange
from gpfit.logsumexp import lse_implicit, lse_scaled
SEED = 33404
class TestLSEimplicit1D(unittest.TestCase):
"tests with one-dimensional input"
x = arange(1., 31.).reshape(15, 2)
alpha = arange(1., 3.)
y, dydx, dydalpha = lse_implicit(x, alpha)
def test_y_ndim(self):
self.assertEqual(self.y.ndim, 1)
def test_y_size(self):
self.assertEqual(self.y.size, self.x.shape[0])
def test_dydx_ndim(self):
self.assertEqual(self.dydx.ndim, 2)
def test_dydx_shape_0(self):
self.assertEqual(self.dydx.shape[0], self.x.shape[0])
def test_dydalpha_ndim(self):
self.assertEqual(self.dydalpha.ndim, 2)
def test_dydalpha_size(self):
self.assertEqual(self.dydalpha.shape[0], self.x.shape[0])
class TestLSEimplicit2D(unittest.TestCase):
"tests with 2D input"
np.random.seed(SEED)
K = 4
x = np.random.rand(1000, K)
alpha = array([1.499, 13.703, 3.219, 4.148])
y, dydx, dydalpha = lse_implicit(x, alpha)
def test_dydx_shape(self):
self.assertEqual(self.dydx.shape, self.x.shape)
def test_dydalpha_shape(self):
self.assertEqual(self.dydalpha.shape, self.x.shape)
class TestLSEScaled(unittest.TestCase):
"Test lse_implicit"
x = arange(1., 31.).reshape(15, 2)
alpha = 1.
y, dydx, dydalpha = lse_scaled(x, alpha)
def test_y_ndim(self):
self.assertEqual(self.y.ndim, 1)
def test_y_size(self):
self.assertEqual(self.y.size, self.x.shape[0])
def test_dydx_ndim(self):
self.assertEqual(self.dydx.ndim, 2)
def test_dydx_shape_0(self):
self.assertEqual(self.dydx.shape[0], self.x.shape[0])
def test_dydx_shape_1(self):
pass
# self.assertEqual(self.dydx.shape[0], ???????)
def test_dydalpha_ndim(self):
self.assertEqual(self.dydalpha.ndim, 1)
def test_dydalpha_size(self):
self.assertEqual(self.dydalpha.size, self.x.shape[0])
# test alpha is integer? negative? 0? array?
TESTS = [
TestLSEimplicit1D,
TestLSEimplicit2D,
TestLSEScaled,
]
if __name__ == '__main__':
SUITE = unittest.TestSuite()
LOADER = unittest.TestLoader()
for t in TESTS:
SUITE.addTests(LOADER.loadTestsFromTestCase(t))
unittest.TextTestRunner(verbosity=2).run(SUITE)