Beispiel #1
0
  def testZeros(self):
    for s in self.all_shapes:
      actual = np_array_ops.zeros(s)
      expected = np.zeros(s)
      msg = 'shape: {}'.format(s)
      self.match(actual, expected, msg)

    for s, t in itertools.product(self.all_shapes, self.all_types):
      actual = np_array_ops.zeros(s, t)
      expected = np.zeros(s, t)
      msg = 'shape: {}, dtype: {}'.format(s, t)
      self.match(actual, expected, msg)
  def testSize(self):

    def run_test(arr, axis=None):
      onp_arr = np.array(arr)
      self.assertEqual(np_array_ops.size(arr, axis), np.size(onp_arr, axis))

    run_test(np_array_ops.array([1]))
    run_test(np_array_ops.array([1, 2, 3, 4, 5]))
    run_test(np_array_ops.ones((2, 3, 2)))
    run_test(np_array_ops.ones((3, 2)))
    run_test(np_array_ops.zeros((5, 6, 7)))
    run_test(1)
    run_test(np_array_ops.ones((3, 2, 1)))
    run_test(constant_op.constant(5))
    run_test(constant_op.constant([1, 1, 1]))
    self.assertRaises(NotImplementedError, np_array_ops.size, np.ones((2, 2)),
                      1)

    @def_function.function(input_signature=[
        tensor_spec.TensorSpec(dtype=dtypes.float64, shape=None)])
    def f(arr):
      arr = np_array_ops.asarray(arr)
      return np_array_ops.size(arr)

    self.assertEqual(f(np_array_ops.ones((3, 2))).numpy(), 6)
Beispiel #3
0
  def testArray(self):
    ndmins = [0, 1, 2, 5]
    for a, dtype, ndmin, copy in itertools.product(self.all_arrays,
                                                   self.all_types, ndmins,
                                                   [True, False]):
      self.match(
          np_array_ops.array(a, dtype=dtype, ndmin=ndmin, copy=copy),
          np.array(a, dtype=dtype, ndmin=ndmin, copy=copy))

    zeros_list = np_array_ops.zeros(5)

    def test_copy_equal_false():
      # Backing tensor is the same if copy=False, other attributes being None.
      self.assertIs(
          np_array_ops.array(zeros_list, copy=False).data, zeros_list.data)
      self.assertIs(
          np_array_ops.array(zeros_list.data, copy=False).data, zeros_list.data)

      # Backing tensor is different if ndmin is not satisfied.
      self.assertIsNot(
          np_array_ops.array(zeros_list, copy=False, ndmin=2).data,
          zeros_list.data)
      self.assertIsNot(
          np_array_ops.array(zeros_list.data, copy=False, ndmin=2).data,
          zeros_list.data)
      self.assertIs(
          np_array_ops.array(zeros_list, copy=False, ndmin=1).data,
          zeros_list.data)
      self.assertIs(
          np_array_ops.array(zeros_list.data, copy=False, ndmin=1).data,
          zeros_list.data)

      # Backing tensor is different if dtype is not satisfied.
      self.assertIsNot(
          np_array_ops.array(zeros_list, copy=False, dtype=int).data,
          zeros_list.data)
      self.assertIsNot(
          np_array_ops.array(zeros_list.data, copy=False, dtype=int).data,
          zeros_list.data)
      self.assertIs(
          np_array_ops.array(zeros_list, copy=False, dtype=float).data,
          zeros_list.data)
      self.assertIs(
          np_array_ops.array(zeros_list.data, copy=False, dtype=float).data,
          zeros_list.data)

    test_copy_equal_false()
    with ops.device('CPU:1'):
      test_copy_equal_false()

    self.assertNotIn('CPU:1', zeros_list.data.backing_device)
    with ops.device('CPU:1'):
      self.assertIn('CPU:1', np_array_ops.array(zeros_list, copy=True).data
                    .backing_device)
      self.assertIn('CPU:1', np_array_ops.array(np.array(0), copy=True).data
                    .backing_device)
Beispiel #4
0
 def testAsAnyArray(self):
     for a, dtype in itertools.product(self.all_arrays, self.all_types):
         self.match(np_array_ops.asanyarray(a, dtype=dtype),
                    np.asanyarray(a, dtype=dtype))
     zeros_list = np_array_ops.zeros(5)
     # Same instance is returned if no dtype is specified and input is ndarray.
     self.assertIs(np_array_ops.asanyarray(zeros_list), zeros_list)
     # Different instance is returned if dtype is specified and input is ndarray.
     self.assertIsNot(np_array_ops.asanyarray(zeros_list, dtype=int),
                      zeros_list)
Beispiel #5
0
    def testArray(self):
        ndmins = [0, 1, 2, 5]
        for a, dtype, ndmin, copy in itertools.product(self.all_arrays,
                                                       self.all_types, ndmins,
                                                       [True, False]):
            self.match(
                np_array_ops.array(a, dtype=dtype, ndmin=ndmin, copy=copy),
                np.array(a, dtype=dtype, ndmin=ndmin, copy=copy))

        zeros_list = np_array_ops.zeros(5)

        # TODO(srbs): Test that copy=True when context.device is different from
        # tensor device copies the tensor.

        # Backing tensor is the same if copy=False, other attributes being None.
        self.assertIs(
            np_array_ops.array(zeros_list, copy=False).data, zeros_list.data)
        self.assertIs(
            np_array_ops.array(zeros_list.data, copy=False).data,
            zeros_list.data)

        # Backing tensor is different if ndmin is not satisfied.
        self.assertIsNot(
            np_array_ops.array(zeros_list, copy=False, ndmin=2).data,
            zeros_list.data)
        self.assertIsNot(
            np_array_ops.array(zeros_list.data, copy=False, ndmin=2).data,
            zeros_list.data)
        self.assertIs(
            np_array_ops.array(zeros_list, copy=False, ndmin=1).data,
            zeros_list.data)
        self.assertIs(
            np_array_ops.array(zeros_list.data, copy=False, ndmin=1).data,
            zeros_list.data)

        # Backing tensor is different if dtype is not satisfied.
        self.assertIsNot(
            np_array_ops.array(zeros_list, copy=False, dtype=int).data,
            zeros_list.data)
        self.assertIsNot(
            np_array_ops.array(zeros_list.data, copy=False, dtype=int).data,
            zeros_list.data)
        self.assertIs(
            np_array_ops.array(zeros_list, copy=False, dtype=float).data,
            zeros_list.data)
        self.assertIs(
            np_array_ops.array(zeros_list.data, copy=False, dtype=float).data,
            zeros_list.data)
Beispiel #6
0
def geomspace(start, stop, num=50, endpoint=True, dtype=None, axis=0):  # pylint: disable=missing-docstring
    dtype = dtypes.as_dtype(dtype) if dtype else np_utils.result_type(
        start, stop, float(num), np_array_ops.zeros((), dtype))
    computation_dtype = np.promote_types(dtype.as_numpy_dtype, np.float32)
    start = np_array_ops.asarray(start, dtype=computation_dtype)
    stop = np_array_ops.asarray(stop, dtype=computation_dtype)
    # follow the numpy geomspace convention for negative and complex endpoints
    start_sign = 1 - np_array_ops.sign(np_array_ops.real(start))
    stop_sign = 1 - np_array_ops.sign(np_array_ops.real(stop))
    signflip = 1 - start_sign * stop_sign // 2
    res = signflip * logspace(log10(signflip * start),
                              log10(signflip * stop),
                              num,
                              endpoint=endpoint,
                              base=10.0,
                              dtype=computation_dtype,
                              axis=0)
    if axis != 0:
        res = np_array_ops.moveaxis(res, 0, axis)
    return math_ops.cast(res, dtype)