1
+ """Tensorflow Tensor Library Module."""
1
2
import logging
2
3
import tensorflow as tf
3
4
import tensorflow_probability as tfp
@@ -11,13 +12,17 @@ class tensorflow_backend(object):
11
12
def __init__ (self , ** kwargs ):
12
13
self .session = kwargs .get ('session' )
13
14
self .name = 'tensorflow'
15
+ self .dtypemap = {
16
+ 'float' : getattr (tf , kwargs .get ('float' , 'float32' )),
17
+ 'int' : getattr (tf , kwargs .get ('int' , 'int32' )),
18
+ 'bool' : tf .bool ,
19
+ }
14
20
15
21
def clip (self , tensor_in , min_value , max_value ):
16
22
"""
17
23
Clips (limits) the tensor values to be within a specified min and max.
18
24
19
25
Example:
20
-
21
26
>>> import pyhf
22
27
>>> import tensorflow as tf
23
28
>>> sess = tf.compat.v1.Session()
@@ -36,6 +41,7 @@ def clip(self, tensor_in, min_value, max_value):
36
41
37
42
Returns:
38
43
TensorFlow Tensor: A clipped `tensor`
44
+
39
45
"""
40
46
if min_value is None :
41
47
min_value = tf .reduce_min (tensor_in )
@@ -48,7 +54,6 @@ def tile(self, tensor_in, repeats):
48
54
Repeat tensor data along a specific dimension
49
55
50
56
Example:
51
-
52
57
>>> import pyhf
53
58
>>> import tensorflow as tf
54
59
>>> sess = tf.compat.v1.Session()
@@ -67,6 +72,7 @@ def tile(self, tensor_in, repeats):
67
72
68
73
Returns:
69
74
TensorFlow Tensor: The tensor with repeated axes
75
+
70
76
"""
71
77
return tf .tile (tensor_in , repeats )
72
78
@@ -75,7 +81,6 @@ def conditional(self, predicate, true_callable, false_callable):
75
81
Runs a callable conditional on the boolean value of the evaulation of a predicate
76
82
77
83
Example:
78
-
79
84
>>> import pyhf
80
85
>>> import tensorflow as tf
81
86
>>> sess = tf.compat.v1.Session()
@@ -97,6 +102,7 @@ def conditional(self, predicate, true_callable, false_callable):
97
102
98
103
Returns:
99
104
TensorFlow Tensor: The output of the callable that was evaluated
105
+
100
106
"""
101
107
return tf .cond (predicate , true_callable , false_callable )
102
108
@@ -157,10 +163,10 @@ def astensor(self, tensor_in, dtype='float'):
157
163
158
164
Returns:
159
165
`tf.Tensor`: A symbolic handle to one of the outputs of a `tf.Operation`.
166
+
160
167
"""
161
- dtypemap = {'float' : tf .float32 , 'int' : tf .int32 , 'bool' : tf .bool }
162
168
try :
163
- dtype = dtypemap [dtype ]
169
+ dtype = self . dtypemap [dtype ]
164
170
except KeyError :
165
171
log .error ('Invalid dtype: dtype must be float, int, or bool.' )
166
172
raise
@@ -198,10 +204,10 @@ def abs(self, tensor):
198
204
return tf .abs (tensor )
199
205
200
206
def ones (self , shape ):
201
- return tf .ones (shape )
207
+ return tf .ones (shape , dtype = self . dtypemap [ 'float' ] )
202
208
203
209
def zeros (self , shape ):
204
- return tf .zeros (shape )
210
+ return tf .zeros (shape , dtype = self . dtypemap [ 'float' ] )
205
211
206
212
def power (self , tensor_in_1 , tensor_in_2 ):
207
213
return tf .pow (tensor_in_1 , tensor_in_2 )
@@ -249,7 +255,6 @@ def simple_broadcast(self, *args):
249
255
Broadcast a sequence of 1 dimensional arrays.
250
256
251
257
Example:
252
-
253
258
>>> import pyhf
254
259
>>> import tensorflow as tf
255
260
>>> sess = tf.compat.v1.Session()
@@ -266,6 +271,7 @@ def simple_broadcast(self, *args):
266
271
267
272
Returns:
268
273
list of Tensors: The sequence broadcast together.
274
+
269
275
"""
270
276
max_dim = max (map (lambda arg : arg .shape [0 ], args ))
271
277
try :
@@ -308,7 +314,6 @@ def poisson_logpdf(self, n, lam):
308
314
at :code:`n` given the parameter :code:`lam`.
309
315
310
316
Example:
311
-
312
317
>>> import pyhf
313
318
>>> import tensorflow as tf
314
319
>>> sess = tf.compat.v1.Session()
@@ -343,7 +348,6 @@ def poisson(self, n, lam):
343
348
at :code:`n` given the parameter :code:`lam`.
344
349
345
350
Example:
346
-
347
351
>>> import pyhf
348
352
>>> import tensorflow as tf
349
353
>>> sess = tf.compat.v1.Session()
@@ -378,7 +382,6 @@ def normal_logpdf(self, x, mu, sigma):
378
382
of :code:`sigma`.
379
383
380
384
Example:
381
-
382
385
>>> import pyhf
383
386
>>> import tensorflow as tf
384
387
>>> sess = tf.compat.v1.Session()
@@ -414,7 +417,6 @@ def normal(self, x, mu, sigma):
414
417
of :code:`sigma`.
415
418
416
419
Example:
417
-
418
420
>>> import pyhf
419
421
>>> import tensorflow as tf
420
422
>>> sess = tf.compat.v1.Session()
@@ -443,12 +445,11 @@ def normal(self, x, mu, sigma):
443
445
normal = tfp .distributions .Normal (mu , sigma )
444
446
return normal .prob (x )
445
447
446
- def normal_cdf (self , x , mu = 0 , sigma = 1 ):
448
+ def normal_cdf (self , x , mu = 0.0 , sigma = 1 ):
447
449
"""
448
- The cumulative distribution function for the Normal distribution
450
+ Compute the value of cumulative distribution function for the Normal distribution at x.
449
451
450
452
Example:
451
-
452
453
>>> import pyhf
453
454
>>> import tensorflow as tf
454
455
>>> sess = tf.compat.v1.Session()
@@ -472,15 +473,16 @@ def normal_cdf(self, x, mu=0, sigma=1):
472
473
Returns:
473
474
TensorFlow Tensor: The CDF
474
475
"""
475
- normal = tfp .distributions .Normal (mu , sigma )
476
+ normal = tfp .distributions .Normal (
477
+ self .astensor (mu , dtype = 'float' )[0 ], self .astensor (sigma , dtype = 'float' )[0 ],
478
+ )
476
479
return normal .cdf (x )
477
480
478
481
def poisson_dist (self , rate ):
479
482
r"""
480
- The Poisson distribution with rate parameter :code:`rate`.
483
+ Construct a Poisson distribution with rate parameter :code:`rate`.
481
484
482
485
Example:
483
-
484
486
>>> import pyhf
485
487
>>> import tensorflow as tf
486
488
>>> sess = tf.compat.v1.Session()
@@ -505,10 +507,9 @@ def poisson_dist(self, rate):
505
507
506
508
def normal_dist (self , mu , sigma ):
507
509
r"""
508
- The Normal distribution with mean :code:`mu` and standard deviation :code:`sigma`.
510
+ Construct a Normal distribution with mean :code:`mu` and standard deviation :code:`sigma`.
509
511
510
512
Example:
511
-
512
513
>>> import pyhf
513
514
>>> import tensorflow as tf
514
515
>>> sess = tf.compat.v1.Session()
0 commit comments