Ejemplo n.º 1
0
 def test_raises_when_positive(self):
     with self.test_session():
         rachel = tf.constant([0, 2], name="rachel")
         with tf.control_dependencies([tf.assert_non_positive(rachel)]):
             out = tf.identity(rachel)
         with self.assertRaisesOpError("rachel"):
             out.eval()
Ejemplo n.º 2
0
def bounds_unlabeled(lower: float,
                     upper: float,
                     tensor: tf.Tensor,
                     name: Optional[str] = None) -> tf.Tensor:
  """Checks the tensor elements fall in the given bounds.

  Args:
    lower: The lower bound.
    upper: The upper bound.
    tensor: The input tensor.
    name: Optional op name.

  Returns:
    The input tensor.
  """
  with tf.name_scope(name, 'check_bounds', [tensor]) as scope:
    if FLAGS.tensorcheck_enable_checks:
      lower_bound_op = tf.assert_non_negative(
          tensor - lower, name='lower_bound')
      upper_bound_op = tf.assert_non_positive(
          tensor - upper, name='upper_bound')
      with tf.control_dependencies([lower_bound_op, upper_bound_op]):
        tensor = tf.identity(tensor, name=scope)

    return tensor
Ejemplo n.º 3
0
 def test_raises_when_positive(self):
   with self.test_session():
     rachel = tf.constant([0, 2], name="rachel")
     with tf.control_dependencies([tf.assert_non_positive(rachel)]):
       out = tf.identity(rachel)
     with self.assertRaisesOpError("rachel"):
       out.eval()
Ejemplo n.º 4
0
def rois_in_tiles_relative(tiles,
                           rois,
                           tile_size,
                           max_per_tile,
                           assert_on_overflow=True):
    rois, overflow = remove_non_intersecting_rois(
        tiles, rois, max_per_tile)  # [n_tiles, max_per_tile, 4]
    if assert_on_overflow:
        with tf.control_dependencies([
                tf.assert_non_positive(
                    overflow,
                    message=
                    "ROI per tile overflow. Set MAX_TARGET_ROIS_PER_TILE to a larger value."
                )
        ]):
            rois = tf.identity(rois)
    is_roi_empty = find_empty_rois(rois)
    is_roi_empty = tf.stack(
        [is_roi_empty, is_roi_empty, is_roi_empty, is_roi_empty], axis=-1)
    tiles = tf.expand_dims(tiles, axis=1)  # force broadcasting on correct axis
    tile_x1, tile_y1, tile_x2, tile_y2 = tf.unstack(
        tiles, axis=2)  # shape [n_tiles, 1]
    roi_x1, roi_y1, roi_x2, roi_y2 = tf.unstack(
        rois, axis=2)  # shape [n_tiles, max_per_tile]
    roi_x1 = (
        roi_x1 - tile_x1
    ) / tile_size  # shapes [n_tiles, max_per_tile] x [n_tiles] broadcast
    roi_x2 = (roi_x2 - tile_x1) / tile_size
    roi_y1 = (roi_y1 - tile_y1) / tile_size
    roi_y2 = (roi_y2 - tile_y1) / tile_size
    rois = tf.stack([roi_x1, roi_y1, roi_x2, roi_y2],
                    axis=-1)  # shape [n_tiles, max_per_tile, 4]
    # replace empty ROIs by (0,0,0,0) for clarity
    return tf.where(is_roi_empty, tf.zeros_like(rois), rois)
Ejemplo n.º 5
0
 def _assert_valid_sample(self, x):
     if not self.validate_args:
         return x
     return control_flow_ops.with_dependencies([
         tf.assert_non_positive(x),
         distribution_util.assert_close(tf.zeros([], dtype=self.dtype),
                                        tf.reduce_logsumexp(x, axis=[-1])),
     ], x)
 def _assert_valid_sample(self, x):
   if not self.validate_args:
     return x
   return control_flow_ops.with_dependencies([
       tf.assert_non_positive(x),
       tf.assert_near(
           tf.zeros([], dtype=self.dtype), tf.reduce_logsumexp(x, axis=[-1])),
   ], x)
Ejemplo n.º 7
0
def _test_assertions(inf_tensors, gen_tensors, eval_tensors):
    """Returns in-graph assertions for testing."""
    observed, latents, divs, log_probs, elbo = inf_tensors
    generated, sampled_latents = gen_tensors
    eval_log_probs, = eval_tensors

    # For RNN, we return None from infer_latents as an optimization.
    if latents is None:
        latents = sampled_latents

    def _same_batch_and_sequence_size_asserts(t1, name1, t2, name2):
        return [
            tf.assert_equal(util.batch_size_from_nested_tensors(t1),
                            util.batch_size_from_nested_tensors(t2),
                            message="Batch: " + name1 + " vs " + name2),
            tf.assert_equal(util.sequence_size_from_nested_tensors(t1),
                            util.sequence_size_from_nested_tensors(t2),
                            message="Steps: " + name1 + " vs " + name2),
        ]

    def _same_shapes(nested1, nested2):
        return snt.nest.flatten(
            snt.nest.map(
                lambda t1, t2: tf.assert_equal(tf.shape(t1),
                                               tf.shape(t2),
                                               message="Shapes: " + t1.name +
                                               " vs " + t2.name), nested1,
                nested2))

    def _all_same_batch_and_sequence_sizes(nested):
        batch_size = util.batch_size_from_nested_tensors(nested)
        sequence_size = util.sequence_size_from_nested_tensors(nested)
        return [
            tf.assert_equal(tf.shape(tensor)[0],
                            batch_size,
                            message="Batch: " + tensor.name)
            for tensor in snt.nest.flatten(nested)
        ] + [
            tf.assert_equal(tf.shape(tensor)[1],
                            sequence_size,
                            message="Steps: " + tensor.name)
            for tensor in snt.nest.flatten(nested)
        ]

    assertions = [
        tf.assert_non_negative(divs),
        tf.assert_non_positive(log_probs),
    ] + _same_shapes(
        (log_probs, log_probs, observed, latents),
        (divs, eval_log_probs, generated,
         sampled_latents)) + _all_same_batch_and_sequence_sizes(
             (observed, latents, divs)) + _all_same_batch_and_sequence_sizes(
                 (generated, sampled_latents))
    vars_ = tf.trainable_variables()
    grads = tf.gradients(-elbo, vars_)
    for (var, grad) in zip(vars_, grads):
        assertions.append(tf.check_numerics(grad, "Gradient for " + var.name))
    return assertions
Ejemplo n.º 8
0
 def test_empty_tensor_doesnt_raise(self):
     # A tensor is non-positive when it satisfies:
     #   For every element x_i in x, x_i <= 0
     # and an empty tensor has no elements, so this is trivially satisfied.
     # This is standard set theory.
     with self.test_session():
         empty = tf.constant([], name="empty")
         with tf.control_dependencies([tf.assert_non_positive(empty)]):
             out = tf.identity(empty)
         out.eval()
Ejemplo n.º 9
0
 def test_empty_tensor_doesnt_raise(self):
   # A tensor is non-positive when it satisfies:
   #   For every element x_i in x, x_i <= 0
   # and an empty tensor has no elements, so this is trivially satisfied.
   # This is standard set theory.
   with self.test_session():
     empty = tf.constant([], name="empty")
     with tf.control_dependencies([tf.assert_non_positive(empty)]):
       out = tf.identity(empty)
     out.eval()
Ejemplo n.º 10
0
def binomial_logprob(x, rho, N=255):
    """

    :param x:  discrete input in range(0, N), a `Tensor` with shape == (batch, K)
    :param rho: mean parameter between 0 and 1, a `Tensor` with shape == (K)
    :param N: number of categories, integer
    :return: a `Tensor` with shape == (batch)
    """
    # copied from https://github.com/tensorflow/tensorflow/blob/r1.8/tensorflow/contrib/distributions/python/ops/binomial.py
    rho = rho * (1. - 2 * noise) + noise
    unnormalized_activation = x * tf.log(noise + rho) + (N - x) * tf.log(
        noise + 1 - rho)  # shape == (batch, K)
    log_normalization = tf.lgamma(1. + N -
                                  x) + tf.lgamma(1. + x) - tf.lgamma(1. + N)
    out = unnormalized_activation - log_normalization
    # out = tf.Print(out, [tf.reduce_max(out)])
    with tf.control_dependencies([tf.assert_non_positive(out)]):
        return tf.check_numerics(out, "binomial_logprob")
Ejemplo n.º 11
0
def bernoulli_logprob(x, rho):
    """

    :param x: binary input, A `Tensor` with shape == (batch, K)
    :param rho: mean parameter between 0 and 1, A `Tensor` with shape == (K)
    :return: a `Tensor` with shape == (batch)
    :raises:
    """

    activation = x * tf.log(noise + rho) + (1. - x) * tf.log(
        noise + 1. - rho)  # shape == (batch, K)
    print_op = tf.print("logproib",
                        tf.count_nonzero(tf.greater(activation, 0.)))
    with tf.control_dependencies([
            tf.assert_non_positive(activation, message="logprob error"),
            tf.check_numerics(activation, "logprob error"), print_op
    ]):
        return activation
Ejemplo n.º 12
0
    def __call__(self, x):
        if not self.built:
            self.build(x)
        with tf.variable_scope("activation"):
            x_expand = tf.expand_dims(x,
                                      axis=0)  # shape == (1, batch, n_input)
            x_tiled = 255 * tf.tile(x_expand, multiples=(
                self.k_samples, 1, 1))  # shape == (k_samples, batch, n_input)

            # p(x|z)
            lpxgivenz = tf.reduce_sum(binomial_logprob(x_tiled,
                                                       self.x_reconstr_means,
                                                       255),
                                      axis=2)  # shape == (k_samples, batch)

            with tf.control_dependencies([tf.assert_non_positive(lpxgivenz)]):
                # Monte carlo importance weighted integration
                lpx = tf.reduce_logsumexp(lpxgivenz, axis=0) - np.log(
                    self.k_samples)  # shape == (batch)
                return lpx
def rois_in_tiles_relative_and_pad(tiles,
                                   rois,
                                   max_per_tile,
                                   assert_on_overflow=True):
    rois, overflow = remove_non_intersecting_rois_and_pad(
        tiles, rois, max_per_tile)  # [n_tiles, max_per_tile, 4]
    rois = make_rois_relative_to_tiles(tiles, rois)
    # replace empty ROIs by (0,0,0,0) for clarity
    is_roi_empty = find_empty_rois(rois)
    rois = zero_where(rois, is_roi_empty)

    # Log error if padding overflow
    if assert_on_overflow:
        with tf.control_dependencies([
                tf.assert_non_positive(
                    overflow,
                    message=
                    "ROI per tile overflow. Set MAX_TARGET_ROIS_PER_TILE to a larger value."
                )
        ]):
            rois = tf.identity(rois)
    return rois
Ejemplo n.º 14
0
def rois_in_tiles_relative(tiles,
                           rois,
                           tile_size,
                           max_per_tile,
                           assert_on_overflow=True):
    rois, overflow = assign_rois_to_intersecting_tiles(
        tiles, rois, max_per_tile)  # [n_tiles, n_rois, 4]
    if assert_on_overflow:
        with tf.control_dependencies([
                tf.assert_non_positive(
                    overflow,
                    message=
                    "ROI per tile overflow. Set MAX_TARGET_ROIS_PER_TILE to a larger value."
                )
        ]):
            rois = tf.identity(rois)
    is_roi_empty = find_empty_rois(rois)
    is_roi_empty = tf.stack(
        [is_roi_empty, is_roi_empty, is_roi_empty, is_roi_empty], axis=-1)
    tiles = tf.expand_dims(tiles, axis=1)  # force broadcasting on correct axis
    tile_x1, tile_y1, tile_x2, tile_y2 = tf.unstack(
        tiles, axis=2)  # shape [n_tiles, 1]
    roi_x1, roi_y1, roi_x2, roi_y2 = tf.unstack(
        rois, axis=2)  # shape [n_tiles, n_rois]
    roi_x1 = (roi_x1 - tile_x1
              ) / tile_size  # shapes [n_tiles, n_rois] x [n_tiles] broadcast
    roi_x2 = (roi_x2 - tile_x1) / tile_size
    roi_y1 = (roi_y1 - tile_y1) / tile_size
    roi_y2 = (roi_y2 - tile_y1) / tile_size
    rois = tf.stack([roi_x1, roi_y1, roi_x2, roi_y2],
                    axis=-1)  # shape [n_tiles, n_rois, 4]
    # replace empty ROIs by (0,0,0,0) for clarity
    rois = tf.where(is_roi_empty, tf.zeros_like(rois), rois)
    # since this function is used in Datasets, always pad the number of ROIs to max_per_tile so that ROIs can be batched
    rois = tf.pad(rois, [[0, 0], [0, max_per_tile - tf.shape(rois)[1]], [0, 0]
                         ])  # shape [n_tiles, max_per_tile, 4]
    return rois
Ejemplo n.º 15
0
 def test_doesnt_raise_when_zero_and_negative(self):
     with self.test_session():
         tom = tf.constant([0, -2], name="tom")
         with tf.control_dependencies([tf.assert_non_positive(tom)]):
             out = tf.identity(tom)
         out.eval()
Ejemplo n.º 16
0
 def test_doesnt_raise_when_zero_and_negative(self):
   with self.test_session():
     tom = tf.constant([0, -2], name="tom")
     with tf.control_dependencies([tf.assert_non_positive(tom)]):
       out = tf.identity(tom)
     out.eval()