Skip to content

Commit

Permalink
Remove unused get_logits_and_probs from internal/distribution_util.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 290747370
  • Loading branch information
derifatives authored and tensorflower-gardener committed Jan 21, 2020
1 parent 27ac5dd commit a914215
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 233 deletions.
83 changes: 0 additions & 83 deletions tensorflow_probability/python/internal/distribution_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -539,89 +539,6 @@ def maybe_get_static_value(x, dtype=None):
return np.array(x_, dtype)


def get_logits_and_probs(logits=None,
probs=None,
multidimensional=False,
validate_args=False,
name='get_logits_and_probs',
dtype=None):
"""Converts logit to probabilities (or vice-versa), and returns both.
Args:
logits: Floating-point `Tensor` representing log-odds.
probs: Floating-point `Tensor` representing probabilities.
multidimensional: Python `bool`, default `False`. If `True`, represents
whether the last dimension of `logits` or `probs`, a `[N1, N2, ... k]`
dimensional tensor, representing the logit or probability of `shape[-1]`
classes.
validate_args: Python `bool`, default `False`. When `True`, either assert `0
<= probs <= 1` (if not `multidimensional`) or that the last dimension of
`probs` sums to one.
name: A name for this operation (optional).
dtype: `tf.DType` to prefer when converting args to `Tensor`s.
Returns:
logits, probs: Tuple of `Tensor`s. If `probs` has an entry that is `0` or
`1`, then the corresponding entry in the returned logit will be `-Inf` and
`Inf` respectively.
Raises:
ValueError: if neither `probs` nor `logits` were passed in, or both were.
"""
if dtype is None:
dtype = dtype_util.common_dtype([probs, logits], dtype_hint=tf.float32)
with tf.name_scope(name):
if (probs is None) == (logits is None):
raise ValueError('Must pass probs or logits, but not both.')

if probs is None:
logits = tf.convert_to_tensor(logits, name='logits', dtype=dtype)
if not dtype_util.is_floating(logits.dtype):
raise TypeError('logits must having floating type.')
# We can early return since we constructed probs and therefore know
# they're valid.
if multidimensional:
if validate_args:
logits = embed_check_categorical_event_shape(logits)
return logits, tf.nn.softmax(logits, name='probs')
return logits, tf.sigmoid(logits, name='probs')

probs = tf.convert_to_tensor(probs, name='probs', dtype=dtype)
if not dtype_util.is_floating(probs.dtype):
raise TypeError('probs must having floating type.')

if validate_args:
with tf.name_scope('validate_probs'):
one = tf.constant(1., probs.dtype)
dependencies = [assert_util.assert_non_negative(probs)]
if multidimensional:
probs = embed_check_categorical_event_shape(probs)
dependencies += [
assert_util.assert_near(
tf.reduce_sum(probs, axis=-1),
one,
message='probs does not sum to 1.')
]
else:
dependencies += [
assert_util.assert_less_equal(
probs, one, message='probs has components greater than 1.')
]
probs = with_dependencies(dependencies, probs)

with tf.name_scope('logits'):
if multidimensional:
# Here we don't compute the multidimensional case, in a manner
# consistent with respect to the unidimensional case. We do so
# following the TF convention. Typically, you might expect to see
# logits = log(probs) - log(probs[pivot]). A side-effect of
# being consistent with the TF approach is that the unidimensional case
# implicitly handles the second dimension but the multidimensional case
# explicitly keeps the pivot dimension.
return tf.math.log(probs), probs
return tf.math.log(probs) - tf.math.log1p(-1. * probs), probs


def _is_known_unsigned_by_dtype(dt):
"""Helper returning True if dtype is known to be unsigned."""
return {
Expand Down
150 changes: 0 additions & 150 deletions tensorflow_probability/python/internal/distribution_util_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -555,156 +555,6 @@ def testGetStaticPlaceholder(self):
None, distribution_util.maybe_get_static_value(x, dtype=np.float64))


@test_util.test_all_tf_execution_regimes
class GetLogitsAndProbsTest(test_util.TestCase):

def testImproperArguments(self):
with self.assertRaises(ValueError):
distribution_util.get_logits_and_probs(logits=None, probs=None)

with self.assertRaises(ValueError):
distribution_util.get_logits_and_probs(logits=[0.1], probs=[0.1])

def testLogits(self):
p = np.array([0.01, 0.2, 0.5, 0.7, .99], dtype=np.float32)
logits = _logit(p)

new_logits, new_p = distribution_util.get_logits_and_probs(
logits=logits, validate_args=True)

self.assertAllClose(p, self.evaluate(new_p), rtol=1e-5, atol=0.)
self.assertAllClose(logits, self.evaluate(new_logits), rtol=1e-5, atol=0.)

def testLogitsMultidimensional(self):
p = np.array([0.2, 0.3, 0.5], dtype=np.float32)
logits = np.log(p)

new_logits, new_p = distribution_util.get_logits_and_probs(
logits=logits, multidimensional=True, validate_args=True)

self.assertAllClose(self.evaluate(new_p), p)
self.assertAllClose(self.evaluate(new_logits), logits)

def testProbability(self):
p = np.array([0.01, 0.2, 0.5, 0.7, .99], dtype=np.float32)

new_logits, new_p = distribution_util.get_logits_and_probs(
probs=p, validate_args=True)

self.assertAllClose(_logit(p), self.evaluate(new_logits))
self.assertAllClose(p, self.evaluate(new_p))

def testProbabilityMultidimensional(self):
p = np.array([[0.3, 0.4, 0.3], [0.1, 0.5, 0.4]], dtype=np.float32)

new_logits, new_p = distribution_util.get_logits_and_probs(
probs=p, multidimensional=True, validate_args=True)

self.assertAllClose(np.log(p), self.evaluate(new_logits))
self.assertAllClose(p, self.evaluate(new_p))

def testProbabilityValidateArgs(self):
p = [0.01, 0.2, 0.5, 0.7, .99]
# Component less than 0.
p2 = [-1, 0.2, 0.5, 0.3, .2]
# Component greater than 1.
p3 = [2, 0.2, 0.5, 0.3, .2]

_, prob = distribution_util.get_logits_and_probs(
probs=p, validate_args=True)
self.evaluate(prob)

with self.assertRaisesOpError('Condition x >= 0'):
_, prob = distribution_util.get_logits_and_probs(
probs=p2, validate_args=True)
self.evaluate(prob)

_, prob = distribution_util.get_logits_and_probs(
probs=p2, validate_args=False)
self.evaluate(prob)

with self.assertRaisesOpError('probs has components greater than 1'):
_, prob = distribution_util.get_logits_and_probs(
probs=p3, validate_args=True)
self.evaluate(prob)

_, prob = distribution_util.get_logits_and_probs(
probs=p3, validate_args=False)
self.evaluate(prob)

def testProbabilityValidateArgsMultidimensional(self):
p = np.array([[0.3, 0.4, 0.3], [0.1, 0.5, 0.4]], dtype=np.float32)
# Component less than 0. Still sums to 1.
p2 = np.array([[-.3, 0.4, 0.9], [0.1, 0.5, 0.4]], dtype=np.float32)
# Component greater than 1. Does not sum to 1.
p3 = np.array([[1.3, 0.0, 0.0], [0.1, 0.5, 0.4]], dtype=np.float32)
# Does not sum to 1.
p4 = np.array([[1.1, 0.3, 0.4], [0.1, 0.5, 0.4]], dtype=np.float32)

_, prob = distribution_util.get_logits_and_probs(
probs=p, multidimensional=True)
self.evaluate(prob)

with self.assertRaisesOpError('Condition x >= 0'):
_, prob = distribution_util.get_logits_and_probs(
probs=p2, multidimensional=True, validate_args=True)
self.evaluate(prob)

_, prob = distribution_util.get_logits_and_probs(
probs=p2, multidimensional=True, validate_args=False)
self.evaluate(prob)

with self.assertRaisesOpError(
'(probs has components greater than 1|probs does not sum to 1)'):
_, prob = distribution_util.get_logits_and_probs(
probs=p3, multidimensional=True, validate_args=True)
self.evaluate(prob)

_, prob = distribution_util.get_logits_and_probs(
probs=p3, multidimensional=True, validate_args=False)
self.evaluate(prob)

with self.assertRaisesOpError('probs does not sum to 1'):
_, prob = distribution_util.get_logits_and_probs(
probs=p4, multidimensional=True, validate_args=True)
self.evaluate(prob)

_, prob = distribution_util.get_logits_and_probs(
probs=p4, multidimensional=True, validate_args=False)
self.evaluate(prob)

def testProbsMultidimShape(self):
with self.assertRaises(ValueError):
p = tf.ones([int(2**11+1)], dtype=tf.float16)
distribution_util.get_logits_and_probs(
probs=p, multidimensional=True, validate_args=True)

if tf.executing_eagerly(): return

with self.assertRaisesOpError(
'Number of classes exceeds `dtype` precision'):
p = np.ones([int(2**11+1)], dtype=np.float16)
p = tf1.placeholder_with_default(p, shape=None)
self.evaluate(distribution_util.get_logits_and_probs(
probs=p, multidimensional=True, validate_args=True))

def testLogitsMultidimShape(self):
with self.assertRaises(ValueError):
l = tf.ones([int(2**11+1)], dtype=tf.float16)
distribution_util.get_logits_and_probs(
logits=l, multidimensional=True, validate_args=True)

if tf.executing_eagerly(): return

with self.assertRaisesOpError(
'Number of classes exceeds `dtype` precision'):
l = np.ones([int(2**11+1)], dtype=np.float16)
l = tf1.placeholder_with_default(l, shape=None)
logit, _ = distribution_util.get_logits_and_probs(
logits=l, multidimensional=True, validate_args=True)
self.evaluate(logit)


@test_util.test_all_tf_execution_regimes
class EmbedCheckCategoricalEventShapeTest(test_util.TestCase):

Expand Down

0 comments on commit a914215

Please sign in to comment.