@@ -231,27 +231,23 @@ def testBetaincBFloat16(self):
231
231
@parameterized .parameters (np .float32 , np .float64 )
232
232
@test_util .numpy_disable_gradient_test
233
233
def testBetaincGradient (self , dtype ):
234
- space = np .logspace (- 2. , 2. , 10 ).tolist ()
234
+ space = np .logspace (- 2. , 2. , 5 ).tolist ()
235
235
space_x = np .linspace (0.01 , 0.99 , 10 ).tolist ()
236
236
a , b , x = zip (* list (itertools .product (space , space , space_x )))
237
237
238
238
a = np .array (a , dtype = dtype )
239
239
b = np .array (b , dtype = dtype )
240
240
x = np .array (x , dtype = dtype )
241
241
242
- # Wrap in tf.function and compile for faster computations.
243
- betainc = tf .function (special .betainc , autograph = False , jit_compile = True )
242
+ # Wrap in tf.function for faster computations.
243
+ betainc = tf .function (special .betainc , autograph = False )
244
244
245
245
delta = 1e-4 if dtype == np .float64 else 1e-3
246
246
tolerance = 7e-3 if dtype == np .float64 else 7e-2
247
247
tolerance_x = 1e-3 if dtype == np .float64 else 1e-1
248
248
249
249
err = self .compute_max_gradient_error (
250
- lambda z : betainc (z , b , x ), [a ], delta = delta )
251
- self .assertLess (err , tolerance )
252
-
253
- err = self .compute_max_gradient_error (
254
- lambda z : betainc (a , z , x ), [b ], delta = delta )
250
+ lambda r , s : betainc (r , s , x ), [a , b ], delta = delta )
255
251
self .assertLess (err , tolerance )
256
252
257
253
err = self .compute_max_gradient_error (
@@ -263,8 +259,8 @@ def testBetaincGradient(self, dtype):
263
259
def testBetaincDerivativeFinite (self , dtype ):
264
260
eps = np .finfo (dtype ).eps
265
261
266
- space = np .logspace (np .log10 (eps ), 5. ).tolist ()
267
- space_x = np .linspace (eps , 1. - eps ).tolist ()
262
+ space = np .logspace (np .log10 (eps ), 5. , 20 ).tolist ()
263
+ space_x = np .linspace (eps , 1. - eps , 20 ).tolist ()
268
264
a , b , x = zip (* list (itertools .product (space , space , space_x )))
269
265
270
266
a = np .array (a , dtype = dtype )
@@ -699,8 +695,8 @@ def testBetaincinvGradientFinite(self, dtype):
699
695
eps = np .finfo (dtype ).eps
700
696
small = np .sqrt (eps )
701
697
702
- space = np .logspace (np .log10 (small ), 4. ).tolist ()
703
- space_y = np .linspace (eps , 1. - small ).tolist ()
698
+ space = np .logspace (np .log10 (small ), 4. , 20 ).tolist ()
699
+ space_y = np .linspace (eps , 1. - small , 20 ).tolist ()
704
700
a , b , y = [
705
701
tf .constant (z , dtype = dtype )
706
702
for z in zip (* list (itertools .product (space , space , space_y )))]
@@ -822,7 +818,7 @@ def testDawsnOdd(self, dtype):
822
818
seed_stream = test_util .test_seed_stream ()
823
819
x = self .evaluate (
824
820
tf .random .uniform (
825
- [int (1e4 )], 0. , 100. , dtype = dtype , seed = seed_stream ()))
821
+ [int (1e3 )], 0. , 100. , dtype = dtype , seed = seed_stream ()))
826
822
self .assertAllClose (
827
823
self .evaluate (special .dawsn (x )), self .evaluate (- special .dawsn (- x )))
828
824
@@ -831,21 +827,21 @@ def testDawsnSmall(self, dtype):
831
827
seed_stream = test_util .test_seed_stream ()
832
828
x = self .evaluate (
833
829
tf .random .uniform (
834
- [int (1e4 )], 0. , 1. , dtype = dtype , seed = seed_stream ()))
830
+ [int (1e3 )], 0. , 1. , dtype = dtype , seed = seed_stream ()))
835
831
self .assertAllClose (scipy_special .dawsn (x ), self .evaluate (special .dawsn (x )))
836
832
837
833
@parameterized .parameters (np .float32 , np .float64 )
838
834
def testDawsnMedium (self , dtype ):
839
835
seed_stream = test_util .test_seed_stream ()
840
836
x = self .evaluate (
841
- tf .random .uniform ([int (1e4 )], 1. , 10. , dtype = dtype , seed = seed_stream ()))
837
+ tf .random .uniform ([int (1e3 )], 1. , 10. , dtype = dtype , seed = seed_stream ()))
842
838
self .assertAllClose (scipy_special .dawsn (x ), self .evaluate (special .dawsn (x )))
843
839
844
840
@parameterized .parameters (np .float32 , np .float64 )
845
841
def testDawsnLarge (self , dtype ):
846
842
seed_stream = test_util .test_seed_stream ()
847
843
x = self .evaluate (tf .random .uniform (
848
- [int (1e4 )], 10. , 100. , dtype = dtype , seed = seed_stream ()))
844
+ [int (1e3 )], 10. , 100. , dtype = dtype , seed = seed_stream ()))
849
845
self .assertAllClose (scipy_special .dawsn (x ), self .evaluate (special .dawsn (x )))
850
846
851
847
@test_util .numpy_disable_gradient_test
@@ -885,50 +881,50 @@ def test_igammainv_bounds(self):
885
881
@parameterized .parameters ((np .float32 , 1.5e-4 ), (np .float64 , 1e-6 ))
886
882
def test_igammainv_inverse_small_a (self , dtype , rtol ):
887
883
seed_stream = test_util .test_seed_stream ()
888
- a = tf .random .uniform ([int (1e4 )], 0. , 1. , dtype = dtype , seed = seed_stream ())
889
- p = tf .random .uniform ([int (1e4 )], 0. , 1. , dtype = dtype , seed = seed_stream ())
884
+ a = tf .random .uniform ([int (1e3 )], 0. , 1. , dtype = dtype , seed = seed_stream ())
885
+ p = tf .random .uniform ([int (1e3 )], 0. , 1. , dtype = dtype , seed = seed_stream ())
890
886
igammainv , a , p = self .evaluate ([special .igammainv (a , p ), a , p ])
891
887
self .assertAllClose (scipy_special .gammaincinv (a , p ), igammainv , rtol = rtol )
892
888
893
889
@parameterized .parameters ((np .float32 , 1.5e-4 ), (np .float64 , 1e-6 ))
894
890
def test_igammacinv_inverse_small_a (self , dtype , rtol ):
895
891
seed_stream = test_util .test_seed_stream ()
896
- a = tf .random .uniform ([int (1e4 )], 0. , 1. , dtype = dtype , seed = seed_stream ())
897
- p = tf .random .uniform ([int (1e4 )], 0. , 1. , dtype = dtype , seed = seed_stream ())
892
+ a = tf .random .uniform ([int (1e3 )], 0. , 1. , dtype = dtype , seed = seed_stream ())
893
+ p = tf .random .uniform ([int (1e3 )], 0. , 1. , dtype = dtype , seed = seed_stream ())
898
894
igammacinv , a , p = self .evaluate ([special .igammacinv (a , p ), a , p ])
899
895
self .assertAllClose (scipy_special .gammainccinv (a , p ), igammacinv , rtol = rtol )
900
896
901
897
@parameterized .parameters ((np .float32 , 1e-4 ), (np .float64 , 1e-6 ))
902
898
def test_igammainv_inverse_medium_a (self , dtype , rtol ):
903
899
seed_stream = test_util .test_seed_stream ()
904
- a = tf .random .uniform ([int (1e4 )], 1. , 100. , dtype = dtype , seed = seed_stream ())
905
- p = tf .random .uniform ([int (1e4 )], 0. , 1. , dtype = dtype , seed = seed_stream ())
900
+ a = tf .random .uniform ([int (1e3 )], 1. , 100. , dtype = dtype , seed = seed_stream ())
901
+ p = tf .random .uniform ([int (1e3 )], 0. , 1. , dtype = dtype , seed = seed_stream ())
906
902
igammainv , a , p = self .evaluate ([special .igammainv (a , p ), a , p ])
907
903
self .assertAllClose (scipy_special .gammaincinv (a , p ), igammainv , rtol = rtol )
908
904
909
905
@parameterized .parameters ((np .float32 , 1e-4 ), (np .float64 , 1e-6 ))
910
906
def test_igammacinv_inverse_medium_a (self , dtype , rtol ):
911
907
seed_stream = test_util .test_seed_stream ()
912
- a = tf .random .uniform ([int (1e4 )], 1. , 100. , dtype = dtype , seed = seed_stream ())
913
- p = tf .random .uniform ([int (1e4 )], 0. , 1. , dtype = dtype , seed = seed_stream ())
908
+ a = tf .random .uniform ([int (1e3 )], 1. , 100. , dtype = dtype , seed = seed_stream ())
909
+ p = tf .random .uniform ([int (1e3 )], 0. , 1. , dtype = dtype , seed = seed_stream ())
914
910
igammacinv , a , p = self .evaluate ([special .igammacinv (a , p ), a , p ])
915
911
self .assertAllClose (scipy_special .gammainccinv (a , p ), igammacinv , rtol = rtol )
916
912
917
913
@parameterized .parameters ((np .float32 , 3e-4 ), (np .float64 , 1e-6 ))
918
914
def test_igammainv_inverse_large_a (self , dtype , rtol ):
919
915
seed_stream = test_util .test_seed_stream ()
920
916
a = tf .random .uniform (
921
- [int (1e4 )], 100. , 10000. , dtype = dtype , seed = seed_stream ())
922
- p = tf .random .uniform ([int (1e4 )], 0. , 1. , dtype = dtype , seed = seed_stream ())
917
+ [int (1e3 )], 100. , 10000. , dtype = dtype , seed = seed_stream ())
918
+ p = tf .random .uniform ([int (1e3 )], 0. , 1. , dtype = dtype , seed = seed_stream ())
923
919
igammainv , a , p = self .evaluate ([special .igammainv (a , p ), a , p ])
924
920
self .assertAllClose (scipy_special .gammaincinv (a , p ), igammainv , rtol = rtol )
925
921
926
922
@parameterized .parameters ((np .float32 , 3e-4 ), (np .float64 , 1e-6 ))
927
923
def test_igammacinv_inverse_large_a (self , dtype , rtol ):
928
924
seed_stream = test_util .test_seed_stream ()
929
925
a = tf .random .uniform (
930
- [int (1e4 )], 100. , 10000. , dtype = dtype , seed = seed_stream ())
931
- p = tf .random .uniform ([int (1e4 )], 0. , 1. , dtype = dtype , seed = seed_stream ())
926
+ [int (1e3 )], 100. , 10000. , dtype = dtype , seed = seed_stream ())
927
+ p = tf .random .uniform ([int (1e3 )], 0. , 1. , dtype = dtype , seed = seed_stream ())
932
928
igammacinv , a , p = self .evaluate ([special .igammacinv (a , p ), a , p ])
933
929
self .assertAllClose (scipy_special .gammainccinv (a , p ), igammacinv , rtol = rtol )
934
930
@@ -1031,13 +1027,13 @@ def testOwensTOddEven(self, dtype):
1031
1027
def testOwensTSmall (self , dtype ):
1032
1028
seed_stream = test_util .test_seed_stream ()
1033
1029
a = tf .random .uniform (
1034
- shape = [int (1e4 )],
1030
+ shape = [int (1e3 )],
1035
1031
minval = 0. ,
1036
1032
maxval = 1. ,
1037
1033
dtype = dtype ,
1038
1034
seed = seed_stream ())
1039
1035
h = tf .random .uniform (
1040
- shape = [int (1e4 )],
1036
+ shape = [int (1e3 )],
1041
1037
minval = 0. ,
1042
1038
maxval = 1. ,
1043
1039
dtype = dtype ,
@@ -1049,13 +1045,13 @@ def testOwensTSmall(self, dtype):
1049
1045
def testOwensTLarger (self , dtype ):
1050
1046
seed_stream = test_util .test_seed_stream ()
1051
1047
a = tf .random .uniform (
1052
- shape = [int (1e4 )],
1048
+ shape = [int (1e3 )],
1053
1049
minval = 1. ,
1054
1050
maxval = 100. ,
1055
1051
dtype = dtype ,
1056
1052
seed = seed_stream ())
1057
1053
h = tf .random .uniform (
1058
- shape = [int (1e4 )],
1054
+ shape = [int (1e3 )],
1059
1055
minval = 1. ,
1060
1056
maxval = 100. ,
1061
1057
dtype = dtype ,
@@ -1067,13 +1063,13 @@ def testOwensTLarger(self, dtype):
1067
1063
def testOwensTLarge (self , dtype ):
1068
1064
seed_stream = test_util .test_seed_stream ()
1069
1065
a = tf .random .uniform (
1070
- shape = [int (1e4 )],
1066
+ shape = [int (1e3 )],
1071
1067
minval = 100. ,
1072
1068
maxval = 1000. ,
1073
1069
dtype = dtype ,
1074
1070
seed = seed_stream ())
1075
1071
h = tf .random .uniform (
1076
- shape = [int (1e4 )],
1072
+ shape = [int (1e3 )],
1077
1073
minval = 100. ,
1078
1074
maxval = 1000. ,
1079
1075
dtype = dtype ,
@@ -1119,13 +1115,13 @@ class SpecialTest(test_util.TestCase):
1119
1115
def testAtanDifferenceSmall (self , dtype ):
1120
1116
seed_stream = test_util .test_seed_stream ()
1121
1117
x = tf .random .uniform (
1122
- shape = [int (1e5 )],
1118
+ shape = [int (1e3 )],
1123
1119
minval = - 10. ,
1124
1120
maxval = 10. ,
1125
1121
dtype = dtype ,
1126
1122
seed = seed_stream ())
1127
1123
y = tf .random .uniform (
1128
- shape = [int (1e5 )],
1124
+ shape = [int (1e3 )],
1129
1125
minval = - 10. ,
1130
1126
maxval = 10. ,
1131
1127
dtype = dtype ,
@@ -1138,13 +1134,13 @@ def testAtanDifferenceSmall(self, dtype):
1138
1134
def testAtanDifferenceLarge (self , dtype ):
1139
1135
seed_stream = test_util .test_seed_stream ()
1140
1136
x = tf .random .uniform (
1141
- shape = [int (1e5 )],
1137
+ shape = [int (1e3 )],
1142
1138
minval = - 100. ,
1143
1139
maxval = 100. ,
1144
1140
dtype = dtype ,
1145
1141
seed = seed_stream ())
1146
1142
y = tf .random .uniform (
1147
- shape = [int (1e5 )],
1143
+ shape = [int (1e3 )],
1148
1144
minval = - 100. ,
1149
1145
maxval = 100. ,
1150
1146
dtype = dtype ,
@@ -1166,7 +1162,7 @@ def testAtanDifferenceCloseInputs(self, dtype):
1166
1162
def testAtanDifferenceProductIsNegativeOne (self , dtype ):
1167
1163
seed_stream = test_util .test_seed_stream ()
1168
1164
x = tf .random .uniform (
1169
- shape = [int (1e5 )],
1165
+ shape = [int (1e3 )],
1170
1166
minval = - 10. ,
1171
1167
maxval = 10. ,
1172
1168
dtype = dtype ,
@@ -1180,7 +1176,7 @@ def testAtanDifferenceProductIsNegativeOne(self, dtype):
1180
1176
def testErfcinvPreservesDtype (self , dtype ):
1181
1177
x = self .evaluate (
1182
1178
tf .random .uniform (
1183
- shape = [int (1e5 )],
1179
+ shape = [int (1e3 )],
1184
1180
minval = 0. ,
1185
1181
maxval = 1. ,
1186
1182
dtype = dtype ,
@@ -1190,7 +1186,7 @@ def testErfcinvPreservesDtype(self, dtype):
1190
1186
def testErfcinv (self ):
1191
1187
x = self .evaluate (
1192
1188
tf .random .uniform (
1193
- shape = [int (1e5 )],
1189
+ shape = [int (1e3 )],
1194
1190
minval = 0. ,
1195
1191
maxval = 1. ,
1196
1192
seed = test_util .test_seed ()))
@@ -1206,7 +1202,7 @@ def testErfcinv(self):
1206
1202
@parameterized .parameters (tf .float32 , tf .float64 )
1207
1203
def testErfcxSmall (self , dtype ):
1208
1204
x = tf .random .uniform (
1209
- shape = [int (1e5 )],
1205
+ shape = [int (1e3 )],
1210
1206
minval = 0. ,
1211
1207
maxval = 1. ,
1212
1208
dtype = dtype ,
@@ -1218,7 +1214,7 @@ def testErfcxSmall(self, dtype):
1218
1214
@parameterized .parameters (tf .float32 , tf .float64 )
1219
1215
def testErfcxMedium (self , dtype ):
1220
1216
x = tf .random .uniform (
1221
- shape = [int (1e5 )],
1217
+ shape = [int (1e3 )],
1222
1218
minval = 1. ,
1223
1219
maxval = 20. ,
1224
1220
dtype = dtype ,
@@ -1230,7 +1226,7 @@ def testErfcxMedium(self, dtype):
1230
1226
@parameterized .parameters (tf .float32 , tf .float64 )
1231
1227
def testErfcxLarge (self , dtype ):
1232
1228
x = tf .random .uniform (
1233
- shape = [int (1e5 )],
1229
+ shape = [int (1e3 )],
1234
1230
minval = 20. ,
1235
1231
maxval = 100. ,
1236
1232
dtype = dtype ,
@@ -1242,7 +1238,7 @@ def testErfcxLarge(self, dtype):
1242
1238
@parameterized .parameters (tf .float32 , tf .float64 )
1243
1239
def testErfcxSmallNegative (self , dtype ):
1244
1240
x = tf .random .uniform (
1245
- shape = [int (1e5 )],
1241
+ shape = [int (1e3 )],
1246
1242
minval = - 1. ,
1247
1243
maxval = 0. ,
1248
1244
dtype = dtype ,
@@ -1254,7 +1250,7 @@ def testErfcxSmallNegative(self, dtype):
1254
1250
@parameterized .parameters (tf .float32 , tf .float64 )
1255
1251
def testErfcxMediumNegative (self , dtype ):
1256
1252
x = tf .random .uniform (
1257
- shape = [int (1e5 )],
1253
+ shape = [int (1e3 )],
1258
1254
minval = - 20. ,
1259
1255
maxval = - 1. ,
1260
1256
dtype = dtype ,
@@ -1266,7 +1262,7 @@ def testErfcxMediumNegative(self, dtype):
1266
1262
@parameterized .parameters (tf .float32 , tf .float64 )
1267
1263
def testErfcxLargeNegative (self , dtype ):
1268
1264
x = tf .random .uniform (
1269
- shape = [int (1e5 )],
1265
+ shape = [int (1e3 )],
1270
1266
minval = - 100. ,
1271
1267
maxval = - 20. ,
1272
1268
dtype = dtype ,
@@ -1291,7 +1287,7 @@ def testErfcxSecondDerivative(self):
1291
1287
@parameterized .parameters (tf .float32 , tf .float64 )
1292
1288
def testLogErfc (self , dtype ):
1293
1289
x = tf .random .uniform (
1294
- shape = [int (1e5 )],
1290
+ shape = [int (1e3 )],
1295
1291
minval = - 3. ,
1296
1292
maxval = 3. ,
1297
1293
dtype = dtype ,
@@ -1317,7 +1313,7 @@ def testLogErfcValueAndGradientNoNaN(self, dtype):
1317
1313
@parameterized .parameters (tf .float32 , tf .float64 )
1318
1314
def testLogErfcx (self , dtype ):
1319
1315
x = tf .random .uniform (
1320
- shape = [int (1e5 )],
1316
+ shape = [int (1e3 )],
1321
1317
minval = - 3. ,
1322
1318
maxval = 3. ,
1323
1319
dtype = dtype ,
@@ -1400,7 +1396,7 @@ def testLambertWGradient(self, value, expected):
1400
1396
1401
1397
def testLogGammaCorrection (self ):
1402
1398
x = half_cauchy .HalfCauchy (
1403
- loc = 8. , scale = 10. ).sample (10000 , test_util .test_seed ())
1399
+ loc = 8. , scale = 10. ).sample (int ( 1e3 ) , test_util .test_seed ())
1404
1400
pi = 3.14159265
1405
1401
stirling = x * tf .math .log (x ) - x + 0.5 * tf .math .log (2 * pi / x )
1406
1402
tfp_gamma_ = stirling + special .log_gamma_correction (x )
@@ -1409,12 +1405,13 @@ def testLogGammaCorrection(self):
1409
1405
1410
1406
def testLogGammaDifference (self ):
1411
1407
y = half_cauchy .HalfCauchy (
1412
- loc = 8. , scale = 10. ).sample (10000 , test_util .test_seed ())
1408
+ loc = 8. , scale = 10. ).sample (int ( 1e3 ) , test_util .test_seed ())
1413
1409
y_64 = tf .cast (y , tf .float64 )
1414
1410
# Not testing x near zero because the naive method is too inaccurate.
1415
1411
# We will get implicit coverage in testLogBeta, where a good reference
1416
1412
# implementation is available (scipy_special.betaln).
1417
- x = uniform .Uniform (low = 4. , high = 12. ).sample (10000 , test_util .test_seed ())
1413
+ x = uniform .Uniform (
1414
+ low = 4. , high = 12. ).sample (int (1e3 ), test_util .test_seed ())
1418
1415
x_64 = tf .cast (x , tf .float64 )
1419
1416
naive_64_ = tf .math .lgamma (y_64 ) - tf .math .lgamma (x_64 + y_64 )
1420
1417
naive_64 , sophisticated , sophisticated_64 = self .evaluate ([
@@ -1437,8 +1434,8 @@ def simple_difference(x, y):
1437
1434
return tf .math .lgamma (y ) - tf .math .lgamma (x + y )
1438
1435
1439
1436
y = half_cauchy .HalfCauchy (
1440
- loc = 8. , scale = 10. ).sample (10000 , test_util .test_seed ())
1441
- x = uniform .Uniform (low = 0. , high = 8. ).sample (10000 , test_util .test_seed ())
1437
+ loc = 8. , scale = 10. ).sample (int ( 1e3 ) , test_util .test_seed ())
1438
+ x = uniform .Uniform (low = 0. , high = 8. ).sample (int ( 1e3 ) , test_util .test_seed ())
1442
1439
_ , [simple_gx_ ,
1443
1440
simple_gy_ ] = gradient .value_and_gradient (simple_difference , [x , y ])
1444
1441
_ , [gx_ , gy_ ] = gradient .value_and_gradient (special .log_gamma_difference ,
@@ -1465,9 +1462,9 @@ def simple_difference(x, y):
1465
1462
1466
1463
def testLogBeta (self ):
1467
1464
strm = test_util .test_seed_stream ()
1468
- x = half_cauchy .HalfCauchy (loc = 1. , scale = 15. ).sample (10000 , strm ())
1465
+ x = half_cauchy .HalfCauchy (loc = 1. , scale = 15. ).sample (int ( 1e3 ) , strm ())
1469
1466
x = self .evaluate (x )
1470
- y = half_cauchy .HalfCauchy (loc = 1. , scale = 15. ).sample (10000 , strm ())
1467
+ y = half_cauchy .HalfCauchy (loc = 1. , scale = 15. ).sample (int ( 1e3 ) , strm ())
1471
1468
y = self .evaluate (y )
1472
1469
# Why not 1e-8?
1473
1470
# - Could be because scipy does the reduction loops recommended
@@ -1484,8 +1481,8 @@ def testLogBetaGradient(self):
1484
1481
def simple_lbeta (x , y ):
1485
1482
return tf .math .lgamma (x ) + tf .math .lgamma (y ) - tf .math .lgamma (x + y )
1486
1483
strm = test_util .test_seed_stream ()
1487
- x = half_cauchy .HalfCauchy (loc = 1. , scale = 15. ).sample (10000 , strm ())
1488
- y = half_cauchy .HalfCauchy (loc = 1. , scale = 15. ).sample (10000 , strm ())
1484
+ x = half_cauchy .HalfCauchy (loc = 1. , scale = 15. ).sample (int ( 1e3 ) , strm ())
1485
+ y = half_cauchy .HalfCauchy (loc = 1. , scale = 15. ).sample (int ( 1e3 ) , strm ())
1489
1486
_ , [simple_gx_ ,
1490
1487
simple_gy_ ] = gradient .value_and_gradient (simple_lbeta , [x , y ])
1491
1488
_ , [gx_ , gy_ ] = gradient .value_and_gradient (special .lbeta , [x , y ])
0 commit comments