示例#1
0
 def _set_difference(self, a, b, aminusb=True):
   # Validate that we get the same results with or without `validate_indices`,
   # and with a & b swapped.
   ops = (
       sets.set_difference(
           a, b, aminusb=aminusb, validate_indices=True),
       sets.set_difference(
           a, b, aminusb=aminusb, validate_indices=False),
       sets.set_difference(
           b, a, aminusb=not aminusb, validate_indices=True),
       sets.set_difference(
           b, a, aminusb=not aminusb, validate_indices=False),)
   for op in ops:
     self._assert_static_shapes(a, op)
   return self._run_equivalent_set_ops(ops)
示例#2
0
 def _set_difference(self, a, b, aminusb=True):
   # Validate that we get the same results with or without `validate_indices`,
   # and with a & b swapped.
   ops = (
       sets.set_difference(
           a, b, aminusb=aminusb, validate_indices=True),
       sets.set_difference(
           a, b, aminusb=aminusb, validate_indices=False),
       sets.set_difference(
           b, a, aminusb=not aminusb, validate_indices=True),
       sets.set_difference(
           b, a, aminusb=not aminusb, validate_indices=False),)
   for op in ops:
     self._assert_static_shapes(a, op)
   return self._run_equivalent_set_ops(ops)
示例#3
0
def recall_at_k(labels, predictions, k):
    '''
    Compute recall at position k.

    :param labels: shape=(num_examples,), dtype=tf.int64
    :param predictions: logits of shape=(num_examples, num_classes)
    :param k: recall position
    :return: recall at position k


    Example:

    labels = tf.constant([0, 1, 1], dtype=tf.int64)
    predictions = tf.constant([[0.1, 0.2, 0.3], [3, 5, 2], [0.3, 0.4, 0.7]])
    recall_at_k(labels, predictions, 2)
    # recall_at_k(labels, predictions, 2) = 0.6667

    '''
    labels = expand_dims(labels, axis=1)
    _, predictions_idx = nn.top_k(predictions, k)
    predictions_idx = math_ops.to_int64(predictions_idx)
    tp = sets.set_size(sets.set_intersection(predictions_idx, labels))
    tp = math_ops.to_double(tp)
    tp = math_ops.reduce_sum(tp)
    fn = sets.set_size(
        sets.set_difference(predictions_idx, labels, aminusb=False))
    fn = math_ops.to_double(fn)
    fn = math_ops.reduce_sum(fn)
    recall = math_ops.div(tp, math_ops.add(tp, fn), name='recall_at_k')

    return recall
def _has_valid_dims(weights_shape, values_shape):
  with ops.name_scope(
      None, "has_invalid_dims", (weights_shape, values_shape)) as scope:
    values_shape_2d = array_ops.expand_dims(values_shape, -1)
    valid_dims = array_ops.concat_v2(
        (values_shape_2d, array_ops.ones_like(values_shape_2d)), axis=1)
    weights_shape_2d = array_ops.expand_dims(weights_shape, -1)
    invalid_dims = sets.set_difference(weights_shape_2d, valid_dims)
    num_invalid_dims = array_ops.size(
        invalid_dims.values, name="num_invalid_dims")
    return math_ops.equal(0, num_invalid_dims, name=scope)
def _has_valid_dims(weights_shape, values_shape):
    with ops.name_scope(None, "has_invalid_dims",
                        (weights_shape, values_shape)) as scope:
        values_shape_2d = array_ops.expand_dims(values_shape, -1)
        valid_dims = array_ops.concat(
            (values_shape_2d, array_ops.ones_like(values_shape_2d)), axis=1)
        weights_shape_2d = array_ops.expand_dims(weights_shape, -1)
        invalid_dims = sets.set_difference(weights_shape_2d, valid_dims)
        num_invalid_dims = array_ops.size(invalid_dims.values,
                                          name="num_invalid_dims")
        return math_ops.equal(0, num_invalid_dims, name=scope)
示例#6
0
 def _set_difference(self, a, b, aminusb=True):
   # Validate that we get the same results with or without `validate_indices`,
   # and with a & b swapped.
   ops = (
       sets.set_difference(
           a, b, aminusb=aminusb, validate_indices=True),
       sets.set_difference(
           a, b, aminusb=aminusb, validate_indices=False),
       sets.set_difference(
           b, a, aminusb=not aminusb, validate_indices=True),
       sets.set_difference(
           b, a, aminusb=not aminusb, validate_indices=False),)
   for op in ops:
     self._assert_shapes(a, op)
   with self.test_session() as sess:
     results = sess.run(ops)
   for i in range(1, 4):
     self.assertAllEqual(results[0].indices, results[i].indices)
     self.assertAllEqual(results[0].values, results[i].values)
     self.assertAllEqual(results[0].dense_shape, results[i].dense_shape)
   return results[0]
示例#7
0
 def _set_difference(self, a, b, aminusb=True):
     # Validate that we get the same results with or without `validate_indices`,
     # and with a & b swapped.
     ops = (
         sets.set_difference(a, b, aminusb=aminusb, validate_indices=True),
         sets.set_difference(a, b, aminusb=aminusb, validate_indices=False),
         sets.set_difference(b,
                             a,
                             aminusb=not aminusb,
                             validate_indices=True),
         sets.set_difference(b,
                             a,
                             aminusb=not aminusb,
                             validate_indices=False),
     )
     for op in ops:
         self._assert_shapes(a, op)
     with self.test_session() as sess:
         results = sess.run(ops)
     for i in range(1, 4):
         self.assertAllEqual(results[0].indices, results[i].indices)
         self.assertAllEqual(results[0].values, results[i].values)
         self.assertAllEqual(results[0].dense_shape, results[i].dense_shape)
     return results[0]
示例#8
0
 def _set_difference_count(self, a, b, aminusb=True):
     op = sets.set_size(sets.set_difference(a, b, aminusb))
     with self.test_session() as sess:
         return sess.run(op)
示例#9
0
 def _set_difference_count(self, a, b, aminusb=True):
   op = sets.set_size(sets.set_difference(a, b, aminusb))
   with self.cached_session() as sess:
     return sess.run(op)
示例#10
0
 def _set_difference_count(self, a, b, aminusb=True):
   op = sets.set_size(sets.set_difference(a, b, aminusb))
   with self.cached_session() as sess:
     return self.evaluate(op)