def testAssertIntegerForm(self): # This should only be detected as an integer. x = tf1.placeholder_with_default(np.array([1., 5, 10, 15, 20], dtype=np.float32), shape=None) y = tf1.placeholder_with_default(np.array([1.1, 5, 10, 15, 20], dtype=np.float32), shape=None) # First component isn't less than float32.eps = 1e-7 z = tf1.placeholder_with_default(np.array([1.0001, 5, 10, 15, 20], dtype=np.float32), shape=None) # This shouldn't be detected as an integer. w = tf1.placeholder_with_default(np.array([1e-8, 5, 10, 15, 20], dtype=np.float32), shape=None) with tf.control_dependencies( [distribution_util.assert_integer_form(x)]): self.evaluate(tf.identity(x)) with self.assertRaisesOpError('has non-integer components'): with tf.control_dependencies( [distribution_util.assert_integer_form(y)]): self.evaluate(tf.identity(y)) with self.assertRaisesOpError('has non-integer components'): with tf.control_dependencies( [distribution_util.assert_integer_form(z)]): self.evaluate(tf.identity(z)) with self.assertRaisesOpError('has non-integer components'): with tf.control_dependencies( [distribution_util.assert_integer_form(w)]): self.evaluate(tf.identity(w))
def _parameter_control_dependencies(self, is_init): if not self.validate_args: return [] sample_shape = tf.concat([ self.distribution.batch_shape_tensor(), self._event_shape_tensor() ], axis=0) low = None if self._low is None else tf.convert_to_tensor(self._low) high = None if self._high is None else tf.convert_to_tensor(self._high) assertions = [] if self._low is not None and is_init != tensor_util.is_ref(self._low): low_shape = ps.shape(low) broadcast_shape = ps.broadcast_shape(sample_shape, low_shape) assertions.extend([ distribution_util.assert_integer_form( low, message='`low` has non-integer components.'), assert_util.assert_equal( tf.reduce_prod(broadcast_shape), tf.reduce_prod(sample_shape), message=('Shape of `low` adds extra batch dimensions to ' 'sample shape.')) ]) if self._high is not None and is_init != tensor_util.is_ref( self._high): high_shape = ps.shape(high) broadcast_shape = ps.broadcast_shape(sample_shape, high_shape) assertions.extend([ distribution_util.assert_integer_form( high, message='`high` has non-integer components.'), assert_util.assert_equal( tf.reduce_prod(broadcast_shape), tf.reduce_prod(sample_shape), message=('Shape of `high` adds extra batch dimensions to ' 'sample shape.')) ]) if (self._low is not None and self._high is not None and (is_init != (tensor_util.is_ref(self._low) or tensor_util.is_ref(self._high)))): assertions.append( assert_util.assert_less( low, high, message='`low` must be strictly less than `high`.')) return assertions
def _parameter_control_dependencies(self, is_init): if not self.validate_args: return [] assertions = [] if is_init != tensor_util.is_ref(self.total_count): total_count = tf.convert_to_tensor(self.total_count) msg1 = 'Argument `total_count` must be non-negative.' msg2 = 'Argument `total_count` cannot contain fractional components.' assertions += [ assert_util.assert_non_negative(total_count, message=msg1), distribution_util.assert_integer_form(total_count, message=msg2), ] if self._probs is not None: if is_init != tensor_util.is_ref(self._probs): probs = tf.convert_to_tensor(self._probs) one = tf.constant(1., probs.dtype) assertions += [ assert_util.assert_non_negative( probs, message='probs has components less than 0.'), assert_util.assert_less_equal( probs, one, message='probs has components greater than 1.') ] return assertions
def _sample_control_dependencies(self, x): assertions = [] if not self.validate_args: return assertions assertions.append(distribution_util.assert_integer_form( x, message='Sample has non-integer components.')) return assertions
def maybe_assert_negative_binomial_param_correctness( is_init, validate_args, total_count, probs, logits): """Return assertions for `NegativeBinomial`-type distributions.""" if is_init: x, name = (probs, 'probs') if logits is None else (logits, 'logits') if not dtype_util.is_floating(x.dtype): raise TypeError( 'Argument `{}` must having floating type.'.format(name)) if not validate_args: return [] assertions = [] if is_init != tensor_util.is_ref(total_count): total_count = tf.convert_to_tensor(total_count) assertions.extend([ assert_util.assert_positive( total_count, message='`total_count` has components less than or equal to 0.'), distribution_util.assert_integer_form( total_count, message='`total_count` has fractional components.') ]) if probs is not None: if is_init != tensor_util.is_ref(probs): probs = tf.convert_to_tensor(probs) one = tf.constant(1., probs.dtype) assertions.extend([ assert_util.assert_non_negative( probs, message='`probs` has components less than 0.'), assert_util.assert_less_equal( probs, one, message='`probs` has components greater than 1.') ]) return assertions
def _check_integer(self, value): with tf.name_scope("check_integer"): value = tf.convert_to_tensor(value, name="value") if not self.validate_args: return value dependencies = [distribution_util.assert_integer_form( value, message="value has non-integer components.")] return distribution_util.with_dependencies(dependencies, value)
def _check_integer(self, value): with tf.name_scope("check_integer", values=[value]): value = tf.convert_to_tensor(value, name="value") if not self.validate_args: return value dependencies = [distribution_util.assert_integer_form( value, message="value has non-integer components.")] return control_flow_ops.with_dependencies(dependencies, value)
def _maybe_assert_valid_total_count(self, total_count, validate_args): if not validate_args: return total_count return control_flow_ops.with_dependencies([ tf.assert_non_negative( total_count, message="total_count must be non-negative."), distribution_util.assert_integer_form( total_count, message="total_count cannot contain fractional components."), ], total_count)
def _maybe_assert_valid_total_count(self, total_count, validate_args): if not validate_args: return total_count return distribution_util.with_dependencies([ assert_util.assert_non_negative( total_count, message='total_count must be non-negative.'), distribution_util.assert_integer_form( total_count, message='total_count cannot contain fractional components.'), ], total_count)
def _sample_control_dependencies(self, x): assertions = [] if not self.validate_args: return assertions assertions.append(assert_util.assert_non_negative( x, message='samples must be non-negative')) if self.force_probs_to_zero_outside_support: assertions.append(distribution_util.assert_integer_form( x, message='samples cannot contain fractional components.')) return assertions
def _sample_control_dependencies(self, x): assertions = [] if not self.validate_args: return assertions assertions.append(assert_util.assert_non_negative( x, message='samples must be non-negative')) if not self.interpolate_nondiscrete: assertions.append(distribution_util.assert_integer_form( x, message='samples cannot contain fractional components.')) return assertions
def _check_integer(self, value): with tf.name_scope('check_integer'): value = tf.convert_to_tensor(value, name='value') dependencies = [] if not self.validate_args: dependencies.append( distribution_util.assert_integer_form( value, message='value has non-integer components.')) with tf.control_dependencies(dependencies): return value
def _parameter_control_dependencies(self, is_init): if not self.validate_args: return [] if is_init == tensor_util.is_ref(self.total_count): return [] total_count = tf.convert_to_tensor(self.total_count) msg1 = 'Argument `total_count` must be non-negative.' msg2 = 'Argument `total_count` cannot contain fractional components.' return [ assert_util.assert_non_negative(total_count, message=msg1), distribution_util.assert_integer_form(total_count, message=msg2), ]
def _parameter_control_dependencies(self, is_init): if not self.validate_args: return [] assertions = [] loc = self._loc disp = self._disp if is_init != tensor_util.is_ref(loc): assertions.extend([ assert_util.assert_non_negative( loc, message='`loc` has components less than 0.'), distribution_util.assert_integer_form( loc, message='`loc` has fractional components.') ]) if is_init != tensor_util.is_ref(disp): assertions.extend([ assert_util.assert_positive( disp, message='`disp` has components less than or equal to 0.'), distribution_util.assert_integer_form( disp, message='`disp` has fractional components.') ]) return assertions
def _parameter_control_dependencies(self, is_init): if not self.validate_args: return [] if is_init: try: self._batch_shape() except ValueError: raise ValueError( 'Arguments `total_count`, `low` and `high` must have compatible ' 'shapes; total_count.shape={}, low.shape={}, ' 'high.shape={}.'.format(tf.shape(self.total_count), tf.shape(self.low), tf.shape(self.high))) assertions = [] if is_init != tensor_util.is_ref(self.total_count): total_count = tf.convert_to_tensor(self.total_count) limit = BATES_TOTAL_COUNT_STABILITY_LIMITS[self.dtype] assertions.extend([ assert_util.assert_positive( total_count, message='`total_count` must be positive.'), distribution_util.assert_integer_form( total_count, message='`total_count` must be integer-valued.'), assert_util.assert_less_equal( tf.cast(total_count, self.dtype), tf.cast(limit, self.dtype), message='`total_count` > {} is numerically unstable.'. format(limit)) ]) if is_init != (tensor_util.is_ref(self.low) or tensor_util.is_ref(self.high)): assertions.append( assert_util.assert_less( self.low, self.high, message='`low` must be less than `high`.')) return assertions
def _parameter_control_dependencies(self, is_init): if not self.validate_args: return [] assertions = [] ok_to_check = lambda x: ( # pylint:disable=g-long-lambda x is not None) and (is_init != tensor_util.is_ref(x)) bias_variance = self.bias_variance slope_variance = self.slope_variance if ok_to_check(self.exponent): exponent = tf.convert_to_tensor(self.exponent) assertions.append( assert_util.assert_positive( exponent, message='`exponent` must be positive.')) from tensorflow_probability.python.internal import distribution_util # pylint: disable=g-import-not-at-top assertions.append( distribution_util.assert_integer_form( exponent, message='`exponent` must be an integer.')) if ok_to_check(self.bias_variance): bias_variance = tf.convert_to_tensor(self.bias_variance) assertions.append( assert_util.assert_non_negative( bias_variance, message='`bias_variance` must be non-negative.')) if ok_to_check(self.slope_variance): slope_variance = tf.convert_to_tensor(self.slope_variance) assertions.append( assert_util.assert_non_negative( slope_variance, message='`slope_variance` must be non-negative.')) if (ok_to_check(self.bias_variance) and ok_to_check(self.slope_variance)): assertions.append( assert_util.assert_positive( tf.math.abs(slope_variance) + tf.math.abs(bias_variance), message=('`slope_variance` and `bias_variance` ' 'can not both be zero.'))) return assertions
def _parameter_control_dependencies(self, is_init): if not self.validate_args: return [] assertions = [] if is_init != tensor_util.is_ref(self.total_count): total_count = tf.convert_to_tensor(self.total_count) msg1 = 'Argument `total_count` must be non-negative.' msg2 = 'Argument `total_count` cannot contain fractional components.' assertions += [ assert_util.assert_non_negative(total_count, message=msg1), distribution_util.assert_integer_form(total_count, message=msg2), ] for concentration in [self.concentration1, self.concentration0]: if is_init != tensor_util.is_ref(concentration): assertions.append( assert_util.assert_positive( concentration, message='Concentration parameter must be positive.')) return assertions
def _sample_control_dependencies(self, x): assertions = [] if not self.validate_args: return assertions assertions.append(distribution_util.assert_integer_form(x)) return assertions