Skip to content

Commit 81b79c8

Browse files
Add 'less' docstring to tensor backends
For NumPy, TensorFlow, and PyTorch
1 parent 8731a79 commit 81b79c8

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-0
lines changed

src/pyhf/tensor/numpy_backend.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,25 @@ def conditional(self, predicate, true_callable, false_callable):
6161
return true_callable() if predicate else false_callable()
6262

6363
def less(self, tensor_in_1, tensor_in_2):
64+
"""
65+
The boolean value of :code:`(tensor_in_1 < tensor_in_2)` element-wise
66+
67+
Example:
68+
69+
>>> import pyhf
70+
>>> pyhf.set_backend(pyhf.tensor.numpy_backend())
71+
>>> a = pyhf.tensorlib.astensor([4])
72+
>>> b = pyhf.tensorlib.astensor([5])
73+
>>> pyhf.tensorlib.less(a, b)
74+
array([ True])
75+
76+
Args:
77+
tensor_in_1 (`Tensor`): The first tensor
78+
tensor_in_2 (`Tensor`): The tensor of same type as :code:`tensor_in_1`
79+
80+
Returns:
81+
NumPy ndarray: The bool of the comparison
82+
"""
6483
tensor_in_1 = self.astensor(tensor_in_1)
6584
tensor_in_2 = self.astensor(tensor_in_2)
6685
return np.less(tensor_in_1, tensor_in_2)

src/pyhf/tensor/pytorch_backend.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,25 @@ def conditional(self, predicate, true_callable, false_callable):
6161
return true_callable() if predicate else false_callable()
6262

6363
def less(self, tensor_in_1, tensor_in_2):
64+
"""
65+
The boolean value of :code:`(tensor_in_1 < tensor_in_2)` element-wise
66+
67+
Example:
68+
69+
>>> import pyhf
70+
>>> pyhf.set_backend(pyhf.tensor.pytorch_backend())
71+
>>> a = pyhf.tensorlib.astensor([4])
72+
>>> b = pyhf.tensorlib.astensor([5])
73+
>>> pyhf.tensorlib.less(a, b)
74+
tensor([True])
75+
76+
Args:
77+
tensor_in_1 (`Tensor`): The first tensor
78+
tensor_in_2 (`Tensor`): The tensor of same type as :code:`tensor_in_1`
79+
80+
Returns:
81+
PyTorch Tensor: The bool of the comparison
82+
"""
6483
tensor_in_1 = self.astensor(tensor_in_1)
6584
tensor_in_2 = self.astensor(tensor_in_2)
6685
return torch.lt(tensor_in_1, tensor_in_2)

src/pyhf/tensor/tensorflow_backend.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,30 @@ def conditional(self, predicate, true_callable, false_callable):
7777
return tf.cond(predicate, true_callable, false_callable)
7878

7979
def less(self, tensor_in_1, tensor_in_2):
80+
"""
81+
The boolean value of :code:`(tensor_in_1 < tensor_in_2)` element-wise
82+
83+
Example:
84+
85+
>>> import pyhf
86+
>>> import tensorflow as tf
87+
>>> sess = tf.Session()
88+
...
89+
>>> pyhf.set_backend(pyhf.tensor.tensorflow_backend(session=sess))
90+
>>> a = pyhf.tensorlib.astensor([4])
91+
>>> b = pyhf.tensorlib.astensor([5])
92+
>>> with sess.as_default():
93+
... sess.run(pyhf.tensorlib.less(a, b))
94+
...
95+
array([ True])
96+
97+
Args:
98+
tensor_in_1 (`Tensor`): The first tensor
99+
tensor_in_2 (`Tensor`): The tensor of same type as :code:`tensor_in_1`
100+
101+
Returns:
102+
TensorFlow Tensor: The bool of the comparison
103+
"""
80104
tensor_in_1 = self.astensor(tensor_in_1)
81105
tensor_in_2 = self.astensor(tensor_in_2)
82106
return tf.math.less(tensor_in_1, tensor_in_2)

0 commit comments

Comments
 (0)