Beispiel #1
0
    def test_eigvalsh(self):
        with self.test_session(graph=ops.Graph()) as sess:
            sess.graph.seed = random_seed.DEFAULT_GRAPH_SEED
            operator, mat = self.operator_and_matrix(
                shapes_info,
                dtype,
                use_placeholder=use_placeholder,
                ensure_self_adjoint_and_pd=True)
            # Eigenvalues are real, so we'll cast these to float64 and sort
            # for comparison.
            op_eigvals = sort_ops.sort(math_ops.cast(operator.eigvals(),
                                                     dtype=dtypes.float64),
                                       axis=-1)
            if dtype.is_complex:
                mat = math_ops.cast(mat, dtype=dtypes.complex128)
            else:
                mat = math_ops.cast(mat, dtype=dtypes.float64)
            mat_eigvals = sort_ops.sort(math_ops.cast(
                linalg_ops.self_adjoint_eigvals(mat), dtype=dtypes.float64),
                                        axis=-1)
            op_eigvals_v, mat_eigvals_v = sess.run([op_eigvals, mat_eigvals])

            atol = self._atol[dtype]  # pylint: disable=protected-access
            rtol = self._rtol[dtype]  # pylint: disable=protected-access
            if dtype == dtypes.float32 or dtype == dtypes.complex64:
                atol = 2e-4
                rtol = 2e-4
            self.assertAllClose(op_eigvals_v,
                                mat_eigvals_v,
                                atol=atol,
                                rtol=rtol)
Beispiel #2
0
def sort(a, axis=-1, kind='quicksort', order=None):  # pylint: disable=missing-docstring
    if kind != 'quicksort':
        raise ValueError("Only 'quicksort' is supported.")
    if order is not None:
        raise ValueError("'order' argument to sort is not supported.")

    a = np_array_ops.array(a)

    if axis is None:
        return sort_ops.sort(array_ops.reshape(a, [-1]), 0)
    else:
        return sort_ops.sort(a, axis)
Beispiel #3
0
def sort(a, axis=-1, kind='quicksort', order=None):  # pylint: disable=missing-docstring
  if kind != 'quicksort':
    raise ValueError(
        'Invalid value for argument `kind`. '
        'Only kind="quicksort" is supported. '
        f'Received: kind={kind}')
  if order is not None:
    raise ValueError('The `order` argument is not supported. Pass order=None')

  a = np_array_ops.array(a)

  if axis is None:
    return sort_ops.sort(array_ops.reshape(a, [-1]), 0)
  else:
    return sort_ops.sort(a, axis)
def summarize(values, epsilon):
  """Reduce a 1D sequence of values to a summary.

  This algorithm is based on numpy.quantiles but modified to allow for
  intermediate steps between multiple data sets. It first finds the target
  number of bins as the reciprocal of epsilon and then takes the individual
  values spaced at appropriate intervals to arrive at that target.
  The final step is to return the corresponding counts between those values
  If the target num_bins is larger than the size of values, the whole array is
  returned (with weights of 1).

  Args:
      values: 1-D `np.ndarray` to be summarized.
      epsilon: A `'float32'` that determines the approxmiate desired precision.

  Returns:
      A 2-D `np.ndarray` that is a summary of the inputs. First column is the
      interpolated partition values, the second is the weights (counts).
  """

  values = array_ops.reshape(values, [-1])
  values = sort_ops.sort(values)
  elements = math_ops.cast(array_ops.size(values), dtypes.float32)
  num_buckets = 1. / epsilon
  increment = math_ops.cast(elements / num_buckets, dtypes.int32)
  start = increment
  step = math_ops.maximum(increment, 1)
  boundaries = values[start::step]
  weights = array_ops.ones_like(boundaries)
  weights = weights * math_ops.cast(step, dtypes.float32)
  return array_ops.stack([boundaries, weights])
 def testDescending(self):
   arr = np.random.random((10, 5, 5))
   with self.cached_session():
     self.assertAllEqual(
         np.sort(arr, axis=0)[::-1],
         sort_ops.sort(
             constant_op.constant(arr), axis=0, direction='DESCENDING').eval())
Beispiel #6
0
 def testSortOp(self):
     with ops.device("/device:IPU:0"):
         with session.Session() as s:
             t1 = random_ops.random_uniform([1000], dtype=dtypes.float32)
             t2 = sort_ops.sort(t1, name="t2")
             h1, h2 = s.run([t1, t2])
             self.assertEqual(sorted(h1), list(h2))
 def testSort_staticallyKnownRank_constantTransposition(self):
   # The transposition array should be a constant if the rank of "values" is
   # statically known.
   tensor = random_ops.random_uniform(
       # Rank is statically known to be 5, but the dimension lengths are not
       # known.
       random_ops.random_uniform(
           shape=(5,), minval=0, maxval=10, dtype=dtypes.int32))
   sort_ops.sort(tensor, axis=1)
   transposition = (
       ops.get_default_graph().get_tensor_by_name('sort/transposition:0'))
   self.assertFalse(tensor_util.constant_value(transposition) is None)
   self.assertAllEqual(
       # Swaps "1" and "4" to put "1" at the end.
       tensor_util.constant_value(transposition),
       [0, 4, 2, 3, 1])
Beispiel #8
0
 def testDescending(self):
     arr = np.random.random((10, 5, 5))
     with self.cached_session():
         self.assertAllEqual(
             np.sort(arr, axis=0)[::-1],
             sort_ops.sort(constant_op.constant(arr),
                           axis=0,
                           direction='DESCENDING').eval())
Beispiel #9
0
 def testSort_staticallyKnownRank_constantTransposition(self):
     # The transposition array should be a constant if the rank of "values" is
     # statically known.
     tensor = random_ops.random_uniform(
         # Rank is statically known to be 5, but the dimension lengths are not
         # known.
         random_ops.random_uniform(shape=(5, ),
                                   minval=0,
                                   maxval=10,
                                   dtype=dtypes.int32))
     sort_ops.sort(tensor, axis=1)
     transposition = (
         ops.get_default_graph().get_tensor_by_name('sort/transposition:0'))
     self.assertFalse(tensor_util.constant_value(transposition) is None)
     self.assertAllEqual(
         # Swaps "1" and "4" to put "1" at the end.
         tensor_util.constant_value(transposition),
         [0, 4, 2, 3, 1])
Beispiel #10
0
 def _test_sort(self, values, axis, direction):
     expected = np.sort(values, axis=axis)
     if direction == 'DESCENDING':
         expected = np.flip(expected, axis=axis)
     self.assertAllEqual(
         expected,
         sort_ops.sort(constant_op.constant(values),
                       axis=axis,
                       direction=direction))
Beispiel #11
0
 def testScalar(self):
     # Create an empty scalar where the static shape is unknown.
     zeros_length_1 = array_ops.zeros(random_ops.random_uniform(
         [1], minval=0, maxval=1, dtype=dtypes.int32),
                                      dtype=dtypes.int32)
     scalar = array_ops.zeros(zeros_length_1)
     with self.cached_session():
         with self.assertRaisesRegex(
             (ValueError, errors.InvalidArgumentError), 'out of bounds'):
             self.evaluate(sort_ops.sort(scalar))
Beispiel #12
0
    def testStackKeys(self):
        m = map_ops.empty_tensor_map()
        k = constant_op.constant(1.0)
        k2 = constant_op.constant(2.0)
        k3 = constant_op.constant(3.0)
        v = constant_op.constant(21.0)
        v2 = constant_op.constant(22.0)
        v3 = constant_op.constant(23.0)
        m = map_ops.tensor_map_insert(m, k, v)
        m = map_ops.tensor_map_insert(m, k2, v2)
        keys = map_ops.tensor_map_stack_keys(m, k.dtype)
        expected = constant_op.constant([1.0, 2.0])
        self.assertAllClose(array_ops.shape(keys), array_ops.shape(expected))
        self.assertAllClose(sort_ops.sort(keys), expected)

        m = map_ops.tensor_map_insert(m, k3, v3)
        keys = map_ops.tensor_map_stack_keys(m, k.dtype)
        expected = constant_op.constant([1.0, 2.0, 3.0])
        self.assertAllClose(array_ops.shape(keys), array_ops.shape(expected))
        self.assertAllClose(sort_ops.sort(keys), expected)
  def testScalar(self):
    # Create an empty scalar where the static shape is unknown.
    zeros_length_1 = array_ops.zeros(
        random_ops.random_uniform([1], minval=0, maxval=1, dtype=dtypes.int32),
        dtype=dtypes.int32)
    scalar = array_ops.zeros(zeros_length_1)

    sort = sort_ops.sort(scalar)
    with self.cached_session():
      with self.assertRaises(errors.InvalidArgumentError):
        sort.eval()
 def testRandom_highDimensionality(self):
   np.random.seed(100)
   for _ in range(20):
     rank = np.random.randint(5, 15)
     shape = [np.random.randint(1, 4) for _ in range(rank)]
     arr = np.random.random(shape)
     sort_axis = np.random.choice(rank)
     with self.cached_session():
       self.assertAllEqual(
           np.sort(arr, axis=sort_axis),
           sort_ops.sort(constant_op.constant(arr), axis=sort_axis).eval())
Beispiel #15
0
    def testScalar(self):
        # Create an empty scalar where the static shape is unknown.
        zeros_length_1 = array_ops.zeros(random_ops.random_uniform(
            [1], minval=0, maxval=1, dtype=dtypes.int32),
                                         dtype=dtypes.int32)
        scalar = array_ops.zeros(zeros_length_1)

        sort = sort_ops.sort(scalar)
        with self.cached_session():
            with self.assertRaises(errors.InvalidArgumentError):
                sort.eval()
Beispiel #16
0
 def testRandom_highDimensionality(self):
     np.random.seed(100)
     for _ in range(20):
         rank = np.random.randint(5, 15)
         shape = [np.random.randint(1, 4) for _ in range(rank)]
         arr = np.random.random(shape)
         sort_axis = np.random.choice(rank)
         with self.cached_session():
             self.assertAllEqual(
                 np.sort(arr, axis=sort_axis),
                 sort_ops.sort(constant_op.constant(arr),
                               axis=sort_axis).eval())
 def _testRandom_lowDimensionality(self, negative_axis):
   np.random.seed(42)
   for _ in range(20):
     rank = np.random.randint(1, 3)
     shape = [np.random.randint(0, 20) for _ in range(rank)]
     arr = np.random.random(shape)
     sort_axis = np.random.choice(rank)
     if negative_axis:
       sort_axis = -1 - sort_axis
     with self.cached_session():
       self.assertAllEqual(
           np.sort(arr, axis=sort_axis),
           sort_ops.sort(constant_op.constant(arr), axis=sort_axis).eval())
Beispiel #18
0
 def _testRandom_lowDimensionality(self, negative_axis):
     np.random.seed(42)
     for _ in range(20):
         rank = np.random.randint(1, 3)
         shape = [np.random.randint(0, 20) for _ in range(rank)]
         arr = np.random.random(shape)
         sort_axis = np.random.choice(rank)
         if negative_axis:
             sort_axis = -1 - sort_axis
         with self.cached_session():
             self.assertAllEqual(
                 np.sort(arr, axis=sort_axis),
                 sort_ops.sort(constant_op.constant(arr),
                               axis=sort_axis).eval())
Beispiel #19
0
        def _remove_indices(a, b):
            """Remove indices (`b`) from `a`."""
            items = array_ops.unstack(sort_ops.sort(array_ops.stack(b)),
                                      num=len(b))

            i = 0
            result = []

            for item in items:
                result.append(a[i:item])
                i = item + 1

            result.append(a[i:])

            return array_ops.concat(result, 0)
Beispiel #20
0
def _tf_sorted(iterable, key, reverse):
  """Overload of sorted_ for Tensor iterable."""
  if reverse is UNSPECIFIED:
    direction = 'ASCENDING'
  else:
    direction = 'DESCENDING'
  if key is not UNSPECIFIED:
    mapped = parallel_ops.vectorized_map(key, iterable)
    if mapped.shape.rank is not None and mapped.shape.rank != 1:
      raise ValueError('sort only supports only 1D tensors')
    with ops.control_dependencies([
        check_ops.assert_rank_v2(mapped, 1,
                                 'sort only supports only 1D tensors')
    ]):
      order = sort_ops.argsort(mapped, direction=direction)
      return array_ops.gather_v2(iterable, order)
  if iterable.shape.rank is not None and iterable.shape.rank != 1:
    raise ValueError('sort only supports only 1D tensors')
  with ops.control_dependencies([
      check_ops.assert_rank_v2(iterable, 1,
                               'sort only supports only 1D tensors')
  ]):
    return sort_ops.sort(iterable, direction=direction)
Beispiel #21
0
    def sample(self, time, outputs, state, name=None):
        """sample for SampleEmbeddingHelper."""
        del time, state  # unused by sample_fn
        # Outputs are logits, we sample instead of argmax (greedy).
        if not isinstance(outputs, ops.Tensor):
            raise TypeError("Expected outputs to be a single Tensor, got: %s" %
                            type(outputs))

        probs = nn_ops.softmax(outputs, -1)
        sorted_args = sort_ops.argsort(probs, -1, direction='DESCENDING')
        sorted_nucleus_probs = math_ops.cumsum(
            sort_ops.sort(probs, -1, direction='DESCENDING'),
            -1) < self._nucleus
        nucleus_probs = array_ops.gather(sorted_nucleus_probs,
                                         sort_ops.argsort(
                                             sorted_args,
                                             -1,
                                             direction='ASCENDING'),
                                         batch_dims=1)
        argmax_probs = array_ops.one_hot(math_ops.argmax(outputs, -1),
                                         depth=array_ops.shape(outputs)[-1],
                                         on_value=True,
                                         off_value=False,
                                         dtype=dtypes.bool)
        outputs = array_ops.where(
            (nucleus_probs | argmax_probs), outputs,
            -np.inf * array_ops.ones_like(outputs, dtype=dtypes.float32))

        if self._softmax_temperature is None:
            logits = outputs
        else:
            logits = outputs / self._softmax_temperature

        sample_ids = categorical_sample(logits=logits, seed=self._seed)

        return sample_ids
 def testNegativeOutOfBounds_staticShape(self):
   arr = constant_op.constant([3, 4, 5])
   with self.assertRaises(ValueError):
     sort_ops.sort(arr, axis=-4)
Beispiel #23
0
 def testNegativeOutOfBounds_staticShape(self):
     arr = constant_op.constant([3, 4, 5])
     with self.assertRaisesRegex((ValueError, errors.InvalidArgumentError),
                                 'slice index .* out of bounds'):
         self.evaluate(sort_ops.sort(arr, axis=-4))
Beispiel #24
0
 def testNegativeOutOfBounds_staticShape(self):
     arr = constant_op.constant([3, 4, 5])
     with self.assertRaises(ValueError):
         sort_ops.sort(arr, axis=-4)