tensorflow_backend

class pyhf.tensor.tensorflow_backend.tensorflow_backend(**kwargs)[source]

Bases: object

TensorFlow backend for pyhf

Methods

__init__(**kwargs)[source]

Initialize self. See help(type(self)) for accurate signature.

abs(tensor)[source]
astensor(tensor_in, dtype='float')[source]

Convert to a TensorFlow Tensor.

Parameters

tensor_in (Number or Tensor) – Tensor object

Returns

A symbolic handle to one of the outputs of a tf.Operation.

Return type

tf.Tensor

boolean_mask(tensor, mask)[source]
clip(tensor_in, min_value, max_value)[source]

Clips (limits) the tensor values to be within a specified min and max.

Example

>>> import pyhf
>>> import tensorflow as tf
>>> sess = tf.compat.v1.Session()
...
>>> pyhf.set_backend("tensorflow", _session=sess)
>>> a = pyhf.tensorlib.astensor([-2, -1, 0, 1, 2])
>>> with sess.as_default():
...   sess.run(pyhf.tensorlib.clip(a, -1, 1))
...
array([-1., -1.,  0.,  1.,  1.], dtype=float32)
Parameters
  • tensor_in (tensor) – The input tensor object

  • min_value (scalar or tensor or None) – The minimum value to be cliped to

  • max_value (scalar or tensor or None) – The maximum value to be cliped to

Returns

A clipped tensor

Return type

TensorFlow Tensor

concatenate(sequence, axis=0)[source]

Join a sequence of arrays along an existing axis.

Parameters
  • sequence – sequence of tensors

  • axis – dimension along which to concatenate

Returns

the concatenated tensor

Return type

output

conditional(predicate, true_callable, false_callable)[source]

Runs a callable conditional on the boolean value of the evaulation of a predicate

Example

>>> import pyhf
>>> import tensorflow as tf
>>> sess = tf.compat.v1.Session()
...
>>> pyhf.set_backend("tensorflow", _session=sess)
>>> tensorlib = pyhf.tensorlib
>>> a = tensorlib.astensor([4])
>>> b = tensorlib.astensor([5])
>>> compare = tensorlib.conditional((a < b)[0], lambda: a + b, lambda: a - b)
>>> with sess.as_default():
...     sess.run(compare)
...
array([9.], dtype=float32)
Parameters
  • predicate (scalar) – The logical condition that determines which callable to evaluate

  • true_callable (callable) – The callable that is evaluated when the predicate evalutes to true

  • false_callable (callable) – The callable that is evaluated when the predicate evalutes to false

Returns

The output of the callable that was evaluated

Return type

TensorFlow Tensor

divide(tensor_in_1, tensor_in_2)[source]
einsum(subscripts, *operands)[source]

A generalized contraction between tensors of arbitrary dimension.

This function returns a tensor whose elements are defined by equation, which is written in a shorthand form inspired by the Einstein summation convention.

Parameters
  • subscripts – str, specifies the subscripts for summation

  • operands – list of array_like, these are the tensors for the operation

Returns

the calculation based on the Einstein summation convention

Return type

TensorFlow Tensor

exp(tensor_in)[source]
gather(tensor, indices)[source]
isfinite(tensor)[source]
log(tensor_in)[source]
normal(x, mu, sigma)[source]

The probability density function of the Normal distribution evaluated at x given parameters of mean of mu and standard deviation of sigma.

Example

>>> import pyhf
>>> import tensorflow as tf
>>> sess = tf.compat.v1.Session()
>>> pyhf.set_backend("tensorflow", _session=sess)
...
>>> with sess.as_default():
...     sess.run(pyhf.tensorlib.normal(0.5, 0., 1.))
...
0.35206532
>>> values = pyhf.tensorlib.astensor([0.5, 2.0])
>>> means = pyhf.tensorlib.astensor([0., 2.3])
>>> sigmas = pyhf.tensorlib.astensor([1., 0.8])
>>> with sess.as_default():
...     sess.run(pyhf.tensorlib.normal(values, means, sigmas))
...
array([0.35206532, 0.46481887], dtype=float32)
Parameters
  • x (tensor or float) – The value at which to evaluate the Normal distribution p.d.f.

  • mu (tensor or float) – The mean of the Normal distribution

  • sigma (tensor or float) – The standard deviation of the Normal distribution

Returns

Value of Normal(x|mu, sigma)

Return type

TensorFlow Tensor

normal_cdf(x, mu=0, sigma=1)[source]

The cumulative distribution function for the Normal distribution

Example

>>> import pyhf
>>> import tensorflow as tf
>>> sess = tf.compat.v1.Session()
...
>>> pyhf.set_backend("tensorflow", _session=sess)
>>> with sess.as_default():
...   sess.run(pyhf.tensorlib.normal_cdf(0.8))
...
0.7881446
>>> values = pyhf.tensorlib.astensor([0.8, 2.0])
>>> with sess.as_default():
...   sess.run(pyhf.tensorlib.normal_cdf(values))
...
array([0.7881446 , 0.97724986], dtype=float32)
Parameters
  • x (tensor or float) – The observed value of the random variable to evaluate the CDF for

  • mu (tensor or float) – The mean of the Normal distribution

  • sigma (tensor or float) – The standard deviation of the Normal distribution

Returns

The CDF

Return type

TensorFlow Tensor

normal_dist(mu, sigma)[source]

The Normal distribution with mean mu and standard deviation sigma.

Example

>>> import pyhf
>>> import tensorflow as tf
>>> sess = tf.compat.v1.Session()
...
>>> pyhf.set_backend("tensorflow", _session=sess)
>>> means = pyhf.tensorlib.astensor([5, 8])
>>> stds = pyhf.tensorlib.astensor([1, 0.5])
>>> values = pyhf.tensorlib.astensor([4, 9])
>>> normals = pyhf.tensorlib.normal_dist(means, stds)
>>> with sess.as_default():
...   sess.run(normals.log_prob(values))
...
array([-1.4189385, -2.2257915], dtype=float32)
Parameters
  • mu (tensor or float) – The mean of the Normal distribution

  • sigma (tensor or float) – The standard deviation of the Normal distribution

Returns

The Normal distribution class

Return type

TensorFlow Probability Normal distribution

normal_logpdf(x, mu, sigma)[source]

The log of the probability density function of the Normal distribution evaluated at x given parameters of mean of mu and standard deviation of sigma.

Example

>>> import pyhf
>>> import tensorflow as tf
>>> sess = tf.compat.v1.Session()
>>> pyhf.set_backend("tensorflow", _session=sess)
...
>>> with sess.as_default():
...     sess.run(pyhf.tensorlib.normal_logpdf(0.5, 0., 1.))
...
-1.0439385
>>> values = pyhf.tensorlib.astensor([0.5, 2.0])
>>> means = pyhf.tensorlib.astensor([0., 2.3])
>>> sigmas = pyhf.tensorlib.astensor([1., 0.8])
>>> with sess.as_default():
...     sess.run(pyhf.tensorlib.normal_logpdf(values, means, sigmas))
...
array([-1.0439385, -0.7661075], dtype=float32)
Parameters
  • x (tensor or float) – The value at which to evaluate the Normal distribution p.d.f.

  • mu (tensor or float) – The mean of the Normal distribution

  • sigma (tensor or float) – The standard deviation of the Normal distribution

Returns

Value of log(Normal(x|mu, sigma))

Return type

TensorFlow Tensor

ones(shape)[source]
outer(tensor_in_1, tensor_in_2)[source]
poisson(n, lam)[source]

The continous approximation, using \(n! = \Gamma\left(n+1\right)\), to the probability mass function of the Poisson distribution evaluated at n given the parameter lam.

Example

>>> import pyhf
>>> import tensorflow as tf
>>> sess = tf.compat.v1.Session()
>>> pyhf.set_backend("tensorflow", _session=sess)
...
>>> with sess.as_default():
...     sess.run(pyhf.tensorlib.poisson(5., 6.))
...
0.16062315
>>> values = pyhf.tensorlib.astensor([5., 9.])
>>> rates = pyhf.tensorlib.astensor([6., 8.])
>>> with sess.as_default():
...     sess.run(pyhf.tensorlib.poisson(values, rates))
...
array([0.16062315, 0.12407687], dtype=float32)
Parameters
  • n (tensor or float) – The value at which to evaluate the approximation to the Poisson distribution p.m.f. (the observed number of events)

  • lam (tensor or float) – The mean of the Poisson distribution p.m.f. (the expected number of events)

Returns

Value of the continous approximation to Poisson(n|lam)

Return type

TensorFlow Tensor

poisson_dist(rate)[source]

The Poisson distribution with rate parameter rate.

Example

>>> import pyhf
>>> import tensorflow as tf
>>> sess = tf.compat.v1.Session()
...
>>> pyhf.set_backend("tensorflow", _session=sess)
>>> rates = pyhf.tensorlib.astensor([5, 8])
>>> values = pyhf.tensorlib.astensor([4, 9])
>>> poissons = pyhf.tensorlib.poisson_dist(rates)
>>> with sess.as_default():
...   sess.run(poissons.log_prob(values))
...
array([-1.7403021, -2.086854 ], dtype=float32)
Parameters

rate (tensor or float) – The mean of the Poisson distribution (the expected number of events)

Returns

The Poisson distribution class

Return type

TensorFlow Probability Poisson distribution

poisson_logpdf(n, lam)[source]

The log of the continous approximation, using \(n! = \Gamma\left(n+1\right)\), to the probability mass function of the Poisson distribution evaluated at n given the parameter lam.

Example

>>> import pyhf
>>> import tensorflow as tf
>>> sess = tf.compat.v1.Session()
>>> pyhf.set_backend("tensorflow", _session=sess)
...
>>> with sess.as_default():
...     sess.run(pyhf.tensorlib.poisson_logpdf(5., 6.))
...
-1.8286943
>>> values = pyhf.tensorlib.astensor([5., 9.])
>>> rates = pyhf.tensorlib.astensor([6., 8.])
>>> with sess.as_default():
...     sess.run(pyhf.tensorlib.poisson_logpdf(values, rates))
...
array([-1.8286943, -2.086854 ], dtype=float32)
Parameters
  • n (tensor or float) – The value at which to evaluate the approximation to the Poisson distribution p.m.f. (the observed number of events)

  • lam (tensor or float) – The mean of the Poisson distribution p.m.f. (the expected number of events)

Returns

Value of the continous approximation to log(Poisson(n|lam))

Return type

TensorFlow Tensor

power(tensor_in_1, tensor_in_2)[source]
product(tensor_in, axis=None)[source]
reshape(tensor, newshape)[source]
shape(tensor)[source]
simple_broadcast(*args)[source]

Broadcast a sequence of 1 dimensional arrays.

Example

>>> import pyhf
>>> import tensorflow as tf
>>> sess = tf.compat.v1.Session()
...
>>> pyhf.set_backend("tensorflow", _session=sess)
>>> tf.Session().run(pyhf.tensorlib.simple_broadcast(
...   pyhf.tensorlib.astensor([1]),
...   pyhf.tensorlib.astensor([2, 3, 4]),
...   pyhf.tensorlib.astensor([5, 6, 7])))
[array([1., 1., 1.], dtype=float32), array([2., 3., 4.], dtype=float32), array([5., 6., 7.], dtype=float32)]
Parameters

args (Array of Tensors) – Sequence of arrays

Returns

The sequence broadcast together.

Return type

list of Tensors

sqrt(tensor_in)[source]
stack(sequence, axis=0)[source]
sum(tensor_in, axis=None)[source]
tile(tensor_in, repeats)[source]

Repeat tensor data along a specific dimension

Example

>>> import pyhf
>>> import tensorflow as tf
>>> sess = tf.compat.v1.Session()
...
>>> pyhf.set_backend("tensorflow", _session=sess)
>>> a = pyhf.tensorlib.astensor([[1.0], [2.0]])
>>> with sess.as_default():
...   sess.run(pyhf.tensorlib.tile(a, (1, 2)))
...
array([[1., 1.],
       [2., 2.]], dtype=float32)
Parameters
  • tensor_in (Tensor) – The tensor to be repeated

  • repeats (Tensor) – The tuple of multipliers for each dimension

Returns

The tensor with repeated axes

Return type

TensorFlow Tensor

tolist(tensor_in)[source]
where(mask, tensor_in_1, tensor_in_2)[source]
zeros(shape)[source]