def testTooSmall(self): with self.assertRaises(ValueError): param = tf.ones([1], dtype=np.float16) checked_param = distribution_util.embed_check_categorical_event_shape( param) if tf.executing_eagerly(): return with self.assertRaisesOpError('must have at least 2 events'): param = tf1.placeholder_with_default(np.ones([1], dtype=np.float16), shape=None) checked_param = distribution_util.embed_check_categorical_event_shape( param) self.evaluate(checked_param)
def testTooLarge(self): with self.assertRaises(ValueError): param = tf.ones([int(2**11+1)], dtype=tf.float16) checked_param = distribution_util.embed_check_categorical_event_shape( param) if tf.executing_eagerly(): return with self.assertRaisesOpError( 'Number of classes exceeds `dtype` precision'): param = tf1.placeholder_with_default( np.ones([int(2**11+1)], dtype=np.float16), shape=None) checked_param = distribution_util.embed_check_categorical_event_shape( param) self.evaluate(checked_param)
def _maybe_assert_valid_concentration(self, concentration, validate_args): """Checks the validity of the concentration parameter.""" if not validate_args: return concentration concentration = distribution_util.embed_check_categorical_event_shape( concentration) return distribution_util.with_dependencies([ tf.compat.v1.assert_positive( concentration, message="Concentration parameter must be positive."), ], concentration)
def __init__(self, logits=None, probs=None, dtype=tf.int32, validate_args=False, allow_nan_stats=True, name="Categorical"): """Initialize Categorical distributions using class log-probabilities. Args: logits: An N-D `Tensor`, `N >= 1`, representing the log probabilities of a set of Categorical distributions. The first `N - 1` dimensions index into a batch of independent distributions and the last dimension represents a vector of logits for each class. Only one of `logits` or `probs` should be passed in. probs: An N-D `Tensor`, `N >= 1`, representing the probabilities of a set of Categorical distributions. The first `N - 1` dimensions index into a batch of independent distributions and the last dimension represents a vector of probabilities for each class. Only one of `logits` or `probs` should be passed in. dtype: The type of the event samples (default: int32). validate_args: Python `bool`, default `False`. When `True` distribution parameters are checked for validity despite possibly degrading runtime performance. When `False` invalid inputs may silently render incorrect outputs. allow_nan_stats: Python `bool`, default `True`. When `True`, statistics (e.g., mean, mode, variance) use the value "`NaN`" to indicate the result is undefined. When `False`, an exception is raised if one or more of the statistic's batch members are undefined. name: Python `str` name prefixed to Ops created by this class. """ parameters = dict(locals()) with tf.compat.v1.name_scope(name, values=[logits, probs]) as name: self._logits, self._probs = util.get_logits_and_probs( logits=logits, probs=probs, validate_args=validate_args, multidimensional=True, name=name) if validate_args: self._logits = util.embed_check_categorical_event_shape( self._logits) logits_shape_static = self._logits.shape.with_rank_at_least(1) if logits_shape_static.ndims is not None: self._batch_rank = tf.convert_to_tensor( value=logits_shape_static.ndims - 1, dtype=tf.int32, name="batch_rank") else: with tf.compat.v1.name_scope(name="batch_rank"): self._batch_rank = tf.rank(self._logits) - 1 logits_shape = tf.shape(input=self._logits, name="logits_shape") num_categories = tf.compat.dimension_value(logits_shape_static[-1]) if num_categories is not None: self._num_categories = tf.convert_to_tensor( value=num_categories, dtype=tf.int32, name="num_categories") else: with tf.compat.v1.name_scope(name="num_categories"): self._num_categories = logits_shape[self._batch_rank] if logits_shape_static[:-1].is_fully_defined(): self._batch_shape_val = tf.constant( logits_shape_static[:-1].as_list(), dtype=tf.int32, name="batch_shape") else: with tf.compat.v1.name_scope(name="batch_shape"): self._batch_shape_val = logits_shape[:-1] super(Categorical, self).__init__( dtype=dtype, reparameterization_type=reparameterization.NOT_REPARAMETERIZED, validate_args=validate_args, allow_nan_stats=allow_nan_stats, parameters=parameters, graph_parents=[self._logits, self._probs], name=name)
def testUnsupportedDtype(self): param = tf.convert_to_tensor(value=np.ones([2**11 + 1]).astype( tf.qint16.as_numpy_dtype), dtype=tf.qint16) with self.assertRaises(TypeError): distribution_util.embed_check_categorical_event_shape(param)
def __init__( self, logits=None, probs=None, dtype=tf.int32, validate_args=False, allow_nan_stats=True, name="Categorical"): """Initialize Categorical distributions using class log-probabilities. Args: logits: An N-D `Tensor`, `N >= 1`, representing the log probabilities of a set of Categorical distributions. The first `N - 1` dimensions index into a batch of independent distributions and the last dimension represents a vector of logits for each class. Only one of `logits` or `probs` should be passed in. probs: An N-D `Tensor`, `N >= 1`, representing the probabilities of a set of Categorical distributions. The first `N - 1` dimensions index into a batch of independent distributions and the last dimension represents a vector of probabilities for each class. Only one of `logits` or `probs` should be passed in. dtype: The type of the event samples (default: int32). validate_args: Python `bool`, default `False`. When `True` distribution parameters are checked for validity despite possibly degrading runtime performance. When `False` invalid inputs may silently render incorrect outputs. allow_nan_stats: Python `bool`, default `True`. When `True`, statistics (e.g., mean, mode, variance) use the value "`NaN`" to indicate the result is undefined. When `False`, an exception is raised if one or more of the statistic's batch members are undefined. name: Python `str` name prefixed to Ops created by this class. """ parameters = dict(locals()) with tf.name_scope(name, values=[logits, probs]) as name: self._logits, self._probs = util.get_logits_and_probs( logits=logits, probs=probs, validate_args=validate_args, multidimensional=True, name=name) if validate_args: self._logits = util.embed_check_categorical_event_shape( self._logits) logits_shape_static = self._logits.shape.with_rank_at_least(1) if logits_shape_static.ndims is not None: self._batch_rank = tf.convert_to_tensor( logits_shape_static.ndims - 1, dtype=tf.int32, name="batch_rank") else: with tf.name_scope(name="batch_rank"): self._batch_rank = tf.rank(self._logits) - 1 logits_shape = tf.shape(self._logits, name="logits_shape") if logits_shape_static[-1].value is not None: self._event_size = tf.convert_to_tensor( logits_shape_static[-1].value, dtype=tf.int32, name="event_size") else: with tf.name_scope(name="event_size"): self._event_size = logits_shape[self._batch_rank] if logits_shape_static[:-1].is_fully_defined(): self._batch_shape_val = tf.constant( logits_shape_static[:-1].as_list(), dtype=tf.int32, name="batch_shape") else: with tf.name_scope(name="batch_shape"): self._batch_shape_val = logits_shape[:-1] super(Categorical, self).__init__( dtype=dtype, reparameterization_type=reparameterization.NOT_REPARAMETERIZED, validate_args=validate_args, allow_nan_stats=allow_nan_stats, parameters=parameters, graph_parents=[self._logits, self._probs], name=name)