18
18
import theano .tensor as tt
19
19
from theano .tests import unittest_tools as utt
20
20
from pymc3 .math import (
21
- LogDet , logdet , probit , invprobit , expand_packed_triangular ,
22
- log1pexp , log1mexp , kronecker , cartesian , kron_dot , kron_solve_lower )
21
+ LogDet ,
22
+ logdet ,
23
+ probit ,
24
+ invprobit ,
25
+ expand_packed_triangular ,
26
+ log1pexp ,
27
+ log1mexp ,
28
+ log1mexp_numpy ,
29
+ kronecker ,
30
+ cartesian ,
31
+ kron_dot ,
32
+ kron_solve_lower ,
33
+ )
23
34
from .helpers import SeededTest
24
35
import pytest
25
36
from pymc3 .theanof import floatX
28
39
def test_kronecker ():
29
40
np .random .seed (1 )
30
41
# Create random matrices
31
- [a , b , c ] = [np .random .rand (3 , 3 + i ) for i in range (3 )]
42
+ [a , b , c ] = [np .random .rand (3 , 3 + i ) for i in range (3 )]
32
43
33
- custom = kronecker (a , b , c ) # Custom version
44
+ custom = kronecker (a , b , c ) # Custom version
34
45
nested = tt .slinalg .kron (a , tt .slinalg .kron (b , c ))
35
46
np .testing .assert_array_almost_equal (
36
- custom .eval (),
37
- nested .eval () # Standard nested version
38
- )
47
+ custom .eval (), nested .eval () # Standard nested version
48
+ )
39
49
40
50
41
51
def test_cartesian ():
@@ -44,20 +54,21 @@ def test_cartesian():
44
54
b = [0 , 2 ]
45
55
c = [5 , 6 ]
46
56
manual_cartesian = np .array (
47
- [[1 , 0 , 5 ],
48
- [1 , 0 , 6 ],
49
- [1 , 2 , 5 ],
50
- [1 , 2 , 6 ],
51
- [2 , 0 , 5 ],
52
- [2 , 0 , 6 ],
53
- [2 , 2 , 5 ],
54
- [2 , 2 , 6 ],
55
- [3 , 0 , 5 ],
56
- [3 , 0 , 6 ],
57
- [3 , 2 , 5 ],
58
- [3 , 2 , 6 ],
59
- ]
60
- )
57
+ [
58
+ [1 , 0 , 5 ],
59
+ [1 , 0 , 6 ],
60
+ [1 , 2 , 5 ],
61
+ [1 , 2 , 6 ],
62
+ [2 , 0 , 5 ],
63
+ [2 , 0 , 6 ],
64
+ [2 , 2 , 5 ],
65
+ [2 , 2 , 6 ],
66
+ [3 , 0 , 5 ],
67
+ [3 , 0 , 6 ],
68
+ [3 , 2 , 5 ],
69
+ [3 , 2 , 6 ],
70
+ ]
71
+ )
61
72
auto_cart = cartesian (a , b , c )
62
73
np .testing .assert_array_almost_equal (manual_cartesian , auto_cart )
63
74
@@ -102,16 +113,19 @@ def test_log1pexp():
102
113
# import mpmath
103
114
# mpmath.mp.dps = 1000
104
115
# [float(mpmath.log(1 + mpmath.exp(x))) for x in vals]
105
- expected = np .array ([
106
- 0.0 ,
107
- 3.720075976020836e-44 ,
108
- 4.539889921686465e-05 ,
109
- 0.6930971818099453 ,
110
- 0.6931471805599453 ,
111
- 0.6931971818099453 ,
112
- 10.000045398899218 ,
113
- 100.0 ,
114
- 1e+20 ])
116
+ expected = np .array (
117
+ [
118
+ 0.0 ,
119
+ 3.720075976020836e-44 ,
120
+ 4.539889921686465e-05 ,
121
+ 0.6930971818099453 ,
122
+ 0.6931471805599453 ,
123
+ 0.6931971818099453 ,
124
+ 10.000045398899218 ,
125
+ 100.0 ,
126
+ 1e20 ,
127
+ ]
128
+ )
115
129
actual = log1pexp (vals ).eval ()
116
130
npt .assert_allclose (actual , expected )
117
131
@@ -121,16 +135,21 @@ def test_log1mexp():
121
135
# import mpmath
122
136
# mpmath.mp.dps = 1000
123
137
# [float(mpmath.log(1 - mpmath.exp(-x))) for x in vals]
124
- expected = np .array ([
125
- np .nan ,
126
- - np .inf ,
127
- - 46.051701859880914 ,
128
- - 9.210390371559516 ,
129
- - 4.540096037048921e-05 ,
130
- - 3.720075976020836e-44 ,
131
- 0.0 ])
138
+ expected = np .array (
139
+ [
140
+ np .nan ,
141
+ - np .inf ,
142
+ - 46.051701859880914 ,
143
+ - 9.210390371559516 ,
144
+ - 4.540096037048921e-05 ,
145
+ - 3.720075976020836e-44 ,
146
+ 0.0 ,
147
+ ]
148
+ )
132
149
actual = log1mexp (vals ).eval ()
133
150
npt .assert_allclose (actual , expected )
151
+ actual_ = log1mexp_numpy (vals )
152
+ npt .assert_allclose (actual_ , expected )
134
153
135
154
136
155
class TestLogDet (SeededTest ):
@@ -154,8 +173,10 @@ def validate(self, input_mat):
154
173
# Test gradient:
155
174
utt .verify_grad (self .op , [input_mat ])
156
175
157
- @pytest .mark .skipif (theano .config .device in ["cuda" , "gpu" ],
158
- reason = "No logDet implementation on GPU." )
176
+ @pytest .mark .skipif (
177
+ theano .config .device in ["cuda" , "gpu" ],
178
+ reason = "No logDet implementation on GPU." ,
179
+ )
159
180
def test_basic (self ):
160
181
# Calls validate with different params
161
182
test_case_1 = np .random .randn (3 , 3 ) / np .sqrt (3 )
@@ -166,11 +187,11 @@ def test_basic(self):
166
187
167
188
def test_expand_packed_triangular ():
168
189
with pytest .raises (ValueError ):
169
- x = tt .matrix ('x' )
170
- x .tag .test_value = np .array ([[1. ]])
190
+ x = tt .matrix ("x" )
191
+ x .tag .test_value = np .array ([[1.0 ]])
171
192
expand_packed_triangular (5 , x )
172
193
N = 5
173
- packed = tt .vector (' packed' )
194
+ packed = tt .vector (" packed" )
174
195
packed .tag .test_value = floatX (np .zeros (N * (N + 1 ) // 2 ))
175
196
with pytest .raises (TypeError ):
176
197
expand_packed_triangular (packed .shape [0 ], packed )
@@ -182,9 +203,18 @@ def test_expand_packed_triangular():
182
203
upper_packed = floatX (vals [upper != 0 ])
183
204
expand_lower = expand_packed_triangular (N , packed , lower = True )
184
205
expand_upper = expand_packed_triangular (N , packed , lower = False )
185
- expand_diag_lower = expand_packed_triangular (N , packed , lower = True , diagonal_only = True )
186
- expand_diag_upper = expand_packed_triangular (N , packed , lower = False , diagonal_only = True )
206
+ expand_diag_lower = expand_packed_triangular (
207
+ N , packed , lower = True , diagonal_only = True
208
+ )
209
+ expand_diag_upper = expand_packed_triangular (
210
+ N , packed , lower = False , diagonal_only = True
211
+ )
187
212
assert np .all (expand_lower .eval ({packed : lower_packed }) == lower )
188
213
assert np .all (expand_upper .eval ({packed : upper_packed }) == upper )
189
- assert np .all (expand_diag_lower .eval ({packed : lower_packed }) == floatX (np .diag (vals )))
190
- assert np .all (expand_diag_upper .eval ({packed : upper_packed }) == floatX (np .diag (vals )))
214
+ assert np .all (
215
+ expand_diag_lower .eval ({packed : lower_packed }) == floatX (np .diag (vals ))
216
+ )
217
+ assert np .all (
218
+ expand_diag_upper .eval ({packed : upper_packed }) == floatX (np .diag (vals ))
219
+ )
220
+
0 commit comments