Skip to content

Commit

Permalink
fix: Ensure tensorlib precision used throughout TensorFlow backend (#…
Browse files Browse the repository at this point in the history
…1399)

* Ensure that the precision requested in the set_backend API is used throughout the TensorFlow backend calculations
   - Determine dtype from dtypemap
   - Ensure TensorFlow Probability operations are given a tensor of the correct precision
  • Loading branch information
matthewfeickert authored Apr 6, 2021
1 parent cbf8d03 commit 34bd7f4
Showing 1 changed file with 24 additions and 14 deletions.
38 changes: 24 additions & 14 deletions src/pyhf/tensor/tensorflow_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,15 +160,12 @@ def tolist(self, tensor_in):
raise

def outer(self, tensor_in_1, tensor_in_2):
dtype = self.dtypemap["float"]
tensor_in_1 = (
tensor_in_1
if tensor_in_1.dtype != tf.bool
else tf.cast(tensor_in_1, tf.float32)
tensor_in_1 if tensor_in_1.dtype != tf.bool else tf.cast(tensor_in_1, dtype)
)
tensor_in_1 = (
tensor_in_1
if tensor_in_2.dtype != tf.bool
else tf.cast(tensor_in_2, tf.float32)
tensor_in_1 if tensor_in_2.dtype != tf.bool else tf.cast(tensor_in_2, dtype)
)
return tf.einsum('i,j->ij', tensor_in_1, tensor_in_2)

Expand Down Expand Up @@ -428,6 +425,8 @@ def poisson_logpdf(self, n, lam):
Returns:
TensorFlow Tensor: Value of the continuous approximation to log(Poisson(n|lam))
"""
lam = self.astensor(lam)

return tfp.distributions.Poisson(lam).log_prob(n)

def poisson(self, n, lam):
Expand Down Expand Up @@ -457,6 +456,8 @@ def poisson(self, n, lam):
Returns:
TensorFlow Tensor: Value of the continuous approximation to Poisson(n|lam)
"""
lam = self.astensor(lam)

return tf.exp(tfp.distributions.Poisson(lam).log_prob(n))

def normal_logpdf(self, x, mu, sigma):
Expand Down Expand Up @@ -486,8 +487,10 @@ def normal_logpdf(self, x, mu, sigma):
Returns:
TensorFlow Tensor: Value of log(Normal(x|mu, sigma))
"""
normal = tfp.distributions.Normal(mu, sigma)
return normal.log_prob(x)
mu = self.astensor(mu)
sigma = self.astensor(sigma)

return tfp.distributions.Normal(mu, sigma).log_prob(x)

def normal(self, x, mu, sigma):
r"""
Expand Down Expand Up @@ -516,8 +519,10 @@ def normal(self, x, mu, sigma):
Returns:
TensorFlow Tensor: Value of Normal(x|mu, sigma)
"""
normal = tfp.distributions.Normal(mu, sigma)
return normal.prob(x)
mu = self.astensor(mu)
sigma = self.astensor(sigma)

return tfp.distributions.Normal(mu, sigma).prob(x)

def normal_cdf(self, x, mu=0.0, sigma=1):
"""
Expand All @@ -542,10 +547,10 @@ def normal_cdf(self, x, mu=0.0, sigma=1):
Returns:
TensorFlow Tensor: The CDF
"""
normal = tfp.distributions.Normal(
self.astensor(mu, dtype='float'), self.astensor(sigma, dtype='float')
)
return normal.cdf(x)
mu = self.astensor(mu)
sigma = self.astensor(sigma)

return tfp.distributions.Normal(mu, sigma).cdf(x)

def poisson_dist(self, rate):
r"""
Expand All @@ -568,6 +573,8 @@ def poisson_dist(self, rate):
TensorFlow Probability Poisson distribution: The Poisson distribution class
"""
rate = self.astensor(rate)

return tfp.distributions.Poisson(rate)

def normal_dist(self, mu, sigma):
Expand All @@ -593,6 +600,9 @@ def normal_dist(self, mu, sigma):
TensorFlow Probability Normal distribution: The Normal distribution class
"""
mu = self.astensor(mu)
sigma = self.astensor(sigma)

return tfp.distributions.Normal(mu, sigma)

def to_numpy(self, tensor_in):
Expand Down

0 comments on commit 34bd7f4

Please sign in to comment.