• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2020 The TensorFlow Authors. All Rights Reserved.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#     http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14# ==============================================================================
15"""Tests for tf numpy array methods."""
16
17import itertools
18import operator
19import sys
20from absl.testing import parameterized
21import numpy as np
22
23from tensorflow.python.eager import context
24from tensorflow.python.eager import def_function
25from tensorflow.python.framework import config
26from tensorflow.python.framework import constant_op
27from tensorflow.python.framework import dtypes
28from tensorflow.python.framework import indexed_slices
29from tensorflow.python.framework import ops
30from tensorflow.python.framework import tensor_shape
31from tensorflow.python.framework import tensor_spec
32from tensorflow.python.ops import array_ops
33from tensorflow.python.ops.numpy_ops import np_array_ops
34from tensorflow.python.ops.numpy_ops import np_arrays
35from tensorflow.python.ops.numpy_ops import np_math_ops
36from tensorflow.python.platform import test
37
38
39_virtual_devices_ready = False
40
41
42def set_up_virtual_devices():
43  global _virtual_devices_ready
44  if _virtual_devices_ready:
45    return
46  physical_devices = config.list_physical_devices('CPU')
47  config.set_logical_device_configuration(
48      physical_devices[0], [
49          context.LogicalDeviceConfiguration(),
50          context.LogicalDeviceConfiguration()
51      ])
52  _virtual_devices_ready = True
53
54
55class ArrayCreationTest(test.TestCase):
56
57  def setUp(self):
58    super(ArrayCreationTest, self).setUp()
59    set_up_virtual_devices()
60    python_shapes = [
61        0, 1, 2, (), (1,), (2,), (1, 2, 3), [], [1], [2], [1, 2, 3]
62    ]
63    self.shape_transforms = [
64        lambda x: x, lambda x: np.array(x, dtype=int),
65        lambda x: np_array_ops.array(x, dtype=int), tensor_shape.TensorShape
66    ]
67
68    self.all_shapes = []
69    for fn in self.shape_transforms:
70      self.all_shapes.extend([fn(s) for s in python_shapes])
71
72    if sys.version_info.major == 3:
73      # There is a bug of np.empty (and alike) in Python 3 causing a crash when
74      # the `shape` argument is an np_arrays.ndarray scalar (or tf.Tensor
75      # scalar).
76      def not_ndarray_scalar(s):
77        return not (isinstance(s, np_arrays.ndarray) and s.ndim == 0)
78
79      self.all_shapes = list(filter(not_ndarray_scalar, self.all_shapes))
80
81    self.all_types = [
82        int, float, np.int16, np.int32, np.int64, np.float16, np.float32,
83        np.float64, np.complex64, np.complex128
84    ]
85
86    source_array_data = [
87        1,
88        5.5,
89        7,
90        (),
91        (8, 10.),
92        ((), ()),
93        ((1, 4), (2, 8)),
94        [],
95        [7],
96        [8, 10.],
97        [[], []],
98        [[1, 4], [2, 8]],
99        ([], []),
100        ([1, 4], [2, 8]),
101        [(), ()],
102        [(1, 4), (2, 8)],
103    ]
104
105    self.array_transforms = [
106        lambda x: x,
107        ops.convert_to_tensor,
108        np.array,
109        np_array_ops.array,
110    ]
111    self.all_arrays = []
112    for fn in self.array_transforms:
113      self.all_arrays.extend([fn(s) for s in source_array_data])
114
115  def testEmpty(self):
116    for s in self.all_shapes:
117      actual = np_array_ops.empty(s)
118      expected = np.empty(s)
119      msg = 'shape: {}'.format(s)
120      self.match_shape(actual, expected, msg)
121      self.match_dtype(actual, expected, msg)
122
123    for s, t in itertools.product(self.all_shapes, self.all_types):
124      actual = np_array_ops.empty(s, t)
125      expected = np.empty(s, t)
126      msg = 'shape: {}, dtype: {}'.format(s, t)
127      self.match_shape(actual, expected, msg)
128      self.match_dtype(actual, expected, msg)
129
130  def testEmptyLike(self):
131    for a in self.all_arrays:
132      actual = np_array_ops.empty_like(a)
133      expected = np.empty_like(a)
134      msg = 'array: {}'.format(a)
135      self.match_shape(actual, expected, msg)
136      self.match_dtype(actual, expected, msg)
137
138    for a, t in itertools.product(self.all_arrays, self.all_types):
139      actual = np_array_ops.empty_like(a, t)
140      expected = np.empty_like(a, t)
141      msg = 'array: {} type: {}'.format(a, t)
142      self.match_shape(actual, expected, msg)
143      self.match_dtype(actual, expected, msg)
144
145  def testZeros(self):
146    for s in self.all_shapes:
147      actual = np_array_ops.zeros(s)
148      expected = np.zeros(s)
149      msg = 'shape: {}'.format(s)
150      self.match(actual, expected, msg)
151
152    for s, t in itertools.product(self.all_shapes, self.all_types):
153      actual = np_array_ops.zeros(s, t)
154      expected = np.zeros(s, t)
155      msg = 'shape: {}, dtype: {}'.format(s, t)
156      self.match(actual, expected, msg)
157
158  def testZerosLike(self):
159    for a in self.all_arrays:
160      actual = np_array_ops.zeros_like(a)
161      expected = np.zeros_like(a)
162      msg = 'array: {}'.format(a)
163      self.match(actual, expected, msg)
164
165    for a, t in itertools.product(self.all_arrays, self.all_types):
166      actual = np_array_ops.zeros_like(a, t)
167      expected = np.zeros_like(a, t)
168      msg = 'array: {} type: {}'.format(a, t)
169      self.match(actual, expected, msg)
170
171  def testOnes(self):
172    for s in self.all_shapes:
173      actual = np_array_ops.ones(s)
174      expected = np.ones(s)
175      msg = 'shape: {}'.format(s)
176      self.match(actual, expected, msg)
177
178    for s, t in itertools.product(self.all_shapes, self.all_types):
179      actual = np_array_ops.ones(s, t)
180      expected = np.ones(s, t)
181      msg = 'shape: {}, dtype: {}'.format(s, t)
182      self.match(actual, expected, msg)
183
184  def testOnesLike(self):
185    for a in self.all_arrays:
186      actual = np_array_ops.ones_like(a)
187      expected = np.ones_like(a)
188      msg = 'array: {}'.format(a)
189      self.match(actual, expected, msg)
190
191    for a, t in itertools.product(self.all_arrays, self.all_types):
192      actual = np_array_ops.ones_like(a, t)
193      expected = np.ones_like(a, t)
194      msg = 'array: {} type: {}'.format(a, t)
195      self.match(actual, expected, msg)
196
197  def testEye(self):
198    n_max = 3
199    m_max = 3
200
201    for n in range(1, n_max + 1):
202      self.match(np_array_ops.eye(n), np.eye(n))
203      for k in range(-n, n + 1):
204        self.match(np_array_ops.eye(n, k=k), np.eye(n, k=k))
205      for m in range(1, m_max + 1):
206        self.match(np_array_ops.eye(n, m), np.eye(n, m))
207        for k in range(-n, m):
208          self.match(np_array_ops.eye(n, k=k), np.eye(n, k=k))
209          self.match(np_array_ops.eye(n, m, k), np.eye(n, m, k))
210
211    for dtype in self.all_types:
212      for n in range(1, n_max + 1):
213        self.match(np_array_ops.eye(n, dtype=dtype), np.eye(n, dtype=dtype))
214        for k in range(-n, n + 1):
215          self.match(
216              np_array_ops.eye(n, k=k, dtype=dtype),
217              np.eye(n, k=k, dtype=dtype))
218        for m in range(1, m_max + 1):
219          self.match(
220              np_array_ops.eye(n, m, dtype=dtype), np.eye(n, m, dtype=dtype))
221          for k in range(-n, m):
222            self.match(
223                np_array_ops.eye(n, k=k, dtype=dtype),
224                np.eye(n, k=k, dtype=dtype))
225            self.match(
226                np_array_ops.eye(n, m, k, dtype=dtype),
227                np.eye(n, m, k, dtype=dtype))
228
229  def testIdentity(self):
230    n_max = 3
231
232    for n in range(1, n_max + 1):
233      self.match(np_array_ops.identity(n), np.identity(n))
234
235    for dtype in self.all_types:
236      for n in range(1, n_max + 1):
237        self.match(
238            np_array_ops.identity(n, dtype=dtype), np.identity(n, dtype=dtype))
239
240  def testFull(self):
241    # List of 2-tuples of fill value and shape.
242    data = [
243        (5, ()),
244        (5, (7,)),
245        (5., (7,)),
246        ([5, 8], (2,)),
247        ([5, 8], (3, 2)),
248        ([[5], [8]], (2, 3)),
249        ([[5], [8]], (3, 2, 5)),
250        ([[5.], [8.]], (3, 2, 5)),
251        ([[3, 4], [5, 6], [7, 8]], (3, 3, 2)),
252    ]
253    for f, s in data:
254      for fn1, fn2 in itertools.product(self.array_transforms,
255                                        self.shape_transforms):
256        fill_value = fn1(f)
257        shape = fn2(s)
258        self.match(
259            np_array_ops.full(shape, fill_value), np.full(shape, fill_value))
260        for dtype in self.all_types:
261          self.match(
262              np_array_ops.full(shape, fill_value, dtype=dtype),
263              np.full(shape, fill_value, dtype=dtype))
264
265  def testFullLike(self):
266    # List of 2-tuples of fill value and shape.
267    data = [
268        (5, ()),
269        (5, (7,)),
270        (5., (7,)),
271        ([5, 8], (2,)),
272        ([5, 8], (3, 2)),
273        ([[5], [8]], (2, 3)),
274        ([[5], [8]], (3, 2, 5)),
275        ([[5.], [8.]], (3, 2, 5)),
276    ]
277    zeros_builders = [np_array_ops.zeros, np.zeros]
278    for f, s in data:
279      for fn1, fn2, arr_dtype in itertools.product(self.array_transforms,
280                                                   zeros_builders,
281                                                   self.all_types):
282        fill_value = fn1(f)
283        arr = fn2(s, arr_dtype)
284        self.match(
285            np_array_ops.full_like(arr, fill_value),
286            np.full_like(arr, fill_value))
287        for dtype in self.all_types:
288          self.match(
289              np_array_ops.full_like(arr, fill_value, dtype=dtype),
290              np.full_like(arr, fill_value, dtype=dtype))
291
292  def testArray(self):
293    ndmins = [0, 1, 2, 5]
294    for a, dtype, ndmin, copy in itertools.product(self.all_arrays,
295                                                   self.all_types, ndmins,
296                                                   [True, False]):
297      self.match(
298          np_array_ops.array(a, dtype=dtype, ndmin=ndmin, copy=copy),
299          np.array(a, dtype=dtype, ndmin=ndmin, copy=copy))
300
301    zeros_list = np_array_ops.zeros(5)
302
303    def test_copy_equal_false():
304      # Backing tensor is the same if copy=False, other attributes being None.
305      self.assertIs(np_array_ops.array(zeros_list, copy=False), zeros_list)
306      self.assertIs(np_array_ops.array(zeros_list, copy=False), zeros_list)
307
308      # Backing tensor is different if ndmin is not satisfied.
309      self.assertIsNot(
310          np_array_ops.array(zeros_list, copy=False, ndmin=2),
311          zeros_list)
312      self.assertIsNot(
313          np_array_ops.array(zeros_list, copy=False, ndmin=2),
314          zeros_list)
315      self.assertIs(
316          np_array_ops.array(zeros_list, copy=False, ndmin=1),
317          zeros_list)
318      self.assertIs(
319          np_array_ops.array(zeros_list, copy=False, ndmin=1),
320          zeros_list)
321
322      # Backing tensor is different if dtype is not satisfied.
323      self.assertIsNot(
324          np_array_ops.array(zeros_list, copy=False, dtype=int),
325          zeros_list)
326      self.assertIsNot(
327          np_array_ops.array(zeros_list, copy=False, dtype=int),
328          zeros_list)
329      self.assertIs(
330          np_array_ops.array(zeros_list, copy=False, dtype=float),
331          zeros_list)
332      self.assertIs(
333          np_array_ops.array(zeros_list, copy=False, dtype=float),
334          zeros_list)
335
336    test_copy_equal_false()
337    with ops.device('CPU:1'):
338      test_copy_equal_false()
339
340    self.assertNotIn('CPU:1', zeros_list.backing_device)
341    with ops.device('CPU:1'):
342      self.assertIn(
343          'CPU:1', np_array_ops.array(zeros_list, copy=True).backing_device)
344      self.assertIn(
345          'CPU:1', np_array_ops.array(np.array(0), copy=True).backing_device)
346
347  def testAsArray(self):
348    for a, dtype in itertools.product(self.all_arrays, self.all_types):
349      self.match(
350          np_array_ops.asarray(a, dtype=dtype), np.asarray(a, dtype=dtype))
351
352    zeros_list = np_array_ops.zeros(5)
353    # Same instance is returned if no dtype is specified and input is ndarray.
354    self.assertIs(np_array_ops.asarray(zeros_list), zeros_list)
355    with ops.device('CPU:1'):
356      self.assertIs(np_array_ops.asarray(zeros_list), zeros_list)
357    # Different instance is returned if dtype is specified and input is ndarray.
358    self.assertIsNot(np_array_ops.asarray(zeros_list, dtype=int), zeros_list)
359
360  def testAsAnyArray(self):
361    for a, dtype in itertools.product(self.all_arrays, self.all_types):
362      self.match(
363          np_array_ops.asanyarray(a, dtype=dtype),
364          np.asanyarray(a, dtype=dtype))
365    zeros_list = np_array_ops.zeros(5)
366    # Same instance is returned if no dtype is specified and input is ndarray.
367    self.assertIs(np_array_ops.asanyarray(zeros_list), zeros_list)
368    with ops.device('CPU:1'):
369      self.assertIs(np_array_ops.asanyarray(zeros_list), zeros_list)
370    # Different instance is returned if dtype is specified and input is ndarray.
371    self.assertIsNot(np_array_ops.asanyarray(zeros_list, dtype=int), zeros_list)
372
373  def testAsContiguousArray(self):
374    for a, dtype in itertools.product(self.all_arrays, self.all_types):
375      self.match(
376          np_array_ops.ascontiguousarray(a, dtype=dtype),
377          np.ascontiguousarray(a, dtype=dtype))
378
379  def testARange(self):
380    int_values = np.arange(-3, 3).tolist()
381    float_values = np.arange(-3.5, 3.5).tolist()
382    all_values = int_values + float_values
383    for dtype in self.all_types:
384      for start in all_values:
385        msg = 'dtype:{} start:{}'.format(dtype, start)
386        self.match(np_array_ops.arange(start), np.arange(start), msg=msg)
387        self.match(
388            np_array_ops.arange(start, dtype=dtype),
389            np.arange(start, dtype=dtype),
390            msg=msg)
391        for stop in all_values:
392          msg = 'dtype:{} start:{} stop:{}'.format(dtype, start, stop)
393          self.match(
394              np_array_ops.arange(start, stop), np.arange(start, stop), msg=msg)
395          # TODO(srbs): Investigate and remove check.
396          # There are some bugs when start or stop is float and dtype is int.
397          if not isinstance(start, float) and not isinstance(stop, float):
398            self.match(
399                np_array_ops.arange(start, stop, dtype=dtype),
400                np.arange(start, stop, dtype=dtype),
401                msg=msg)
402          # Note: We intentionally do not test with float values for step
403          # because numpy.arange itself returns inconsistent results. e.g.
404          # np.arange(0.5, 3, step=0.5, dtype=int) returns
405          # array([0, 1, 2, 3, 4])
406          for step in int_values:
407            msg = 'dtype:{} start:{} stop:{} step:{}'.format(
408                dtype, start, stop, step)
409            if not step:
410              with self.assertRaises(ValueError):
411                self.match(
412                    np_array_ops.arange(start, stop, step),
413                    np.arange(start, stop, step),
414                    msg=msg)
415                if not isinstance(start, float) and not isinstance(stop, float):
416                  self.match(
417                      np_array_ops.arange(start, stop, step, dtype=dtype),
418                      np.arange(start, stop, step, dtype=dtype),
419                      msg=msg)
420            else:
421              self.match(
422                  np_array_ops.arange(start, stop, step),
423                  np.arange(start, stop, step),
424                  msg=msg)
425              if not isinstance(start, float) and not isinstance(stop, float):
426                self.match(
427                    np_array_ops.arange(start, stop, step, dtype=dtype),
428                    np.arange(start, stop, step, dtype=dtype),
429                    msg=msg)
430
431  def testDiag(self):
432    array_transforms = [
433        lambda x: x,  # Identity,
434        ops.convert_to_tensor,
435        np.array,
436        lambda x: np.array(x, dtype=np.float32),
437        lambda x: np.array(x, dtype=np.float64),
438        np_array_ops.array,
439        lambda x: np_array_ops.array(x, dtype=np.float32),
440        lambda x: np_array_ops.array(x, dtype=np.float64)
441    ]
442
443    def run_test(arr):
444      for fn in array_transforms:
445        arr = fn(arr)
446        self.match(
447            np_array_ops.diag(arr), np.diag(arr), msg='diag({})'.format(arr))
448        for k in range(-3, 3):
449          self.match(
450              np_array_ops.diag(arr, k),
451              np.diag(arr, k),
452              msg='diag({}, k={})'.format(arr, k))
453
454    # 2-d arrays.
455    run_test(np.arange(9).reshape((3, 3)).tolist())
456    run_test(np.arange(6).reshape((2, 3)).tolist())
457    run_test(np.arange(6).reshape((3, 2)).tolist())
458    run_test(np.arange(3).reshape((1, 3)).tolist())
459    run_test(np.arange(3).reshape((3, 1)).tolist())
460    run_test([[5]])
461    run_test([[]])
462    run_test([[], []])
463
464    # 1-d arrays.
465    run_test([])
466    run_test([1])
467    run_test([1, 2])
468
469  def testDiagFlat(self):
470    array_transforms = [
471        lambda x: x,  # Identity,
472        ops.convert_to_tensor,
473        np.array,
474        lambda x: np.array(x, dtype=np.float32),
475        lambda x: np.array(x, dtype=np.float64),
476        np_array_ops.array,
477        lambda x: np_array_ops.array(x, dtype=np.float32),
478        lambda x: np_array_ops.array(x, dtype=np.float64)
479    ]
480
481    def run_test(arr):
482      for fn in array_transforms:
483        arr = fn(arr)
484        self.match(
485            np_array_ops.diagflat(arr),
486            np.diagflat(arr),
487            msg='diagflat({})'.format(arr))
488        for k in range(-3, 3):
489          self.match(
490              np_array_ops.diagflat(arr, k),
491              np.diagflat(arr, k),
492              msg='diagflat({}, k={})'.format(arr, k))
493
494    # 1-d arrays.
495    run_test([])
496    run_test([1])
497    run_test([1, 2])
498    # 2-d arrays.
499    run_test([[]])
500    run_test([[5]])
501    run_test([[], []])
502    run_test(np.arange(4).reshape((2, 2)).tolist())
503    run_test(np.arange(2).reshape((2, 1)).tolist())
504    run_test(np.arange(2).reshape((1, 2)).tolist())
505    # 3-d arrays
506    run_test(np.arange(8).reshape((2, 2, 2)).tolist())
507
508  def match_shape(self, actual, expected, msg=None):
509    if msg:
510      msg = 'Shape match failed for: {}. Expected: {} Actual: {}'.format(
511          msg, expected.shape, actual.shape)
512    self.assertEqual(actual.shape, expected.shape, msg=msg)
513
514  def match_dtype(self, actual, expected, msg=None):
515    if msg:
516      msg = 'Dtype match failed for: {}. Expected: {} Actual: {}.'.format(
517          msg, expected.dtype, actual.dtype)
518    self.assertEqual(actual.dtype, expected.dtype, msg=msg)
519
520  def match(self, actual, expected, msg=None, almost=False, decimal=7):
521    msg_ = 'Expected: {} Actual: {}'.format(expected, actual)
522    if msg:
523      msg = '{} {}'.format(msg_, msg)
524    else:
525      msg = msg_
526    self.assertIsInstance(actual, np_arrays.ndarray)
527    self.match_dtype(actual, expected, msg)
528    self.match_shape(actual, expected, msg)
529    if not almost:
530      if not actual.shape.rank:
531        self.assertEqual(actual.tolist(), expected.tolist())
532      else:
533        self.assertSequenceEqual(actual.tolist(), expected.tolist())
534    else:
535      np.testing.assert_almost_equal(
536          actual.tolist(), expected.tolist(), decimal=decimal)
537
538  def testIndexedSlices(self):
539    dtype = dtypes.int64
540    iss = indexed_slices.IndexedSlices(
541        values=np_array_ops.ones([2, 3], dtype=dtype),
542        indices=constant_op.constant([1, 9]),
543        dense_shape=[10, 3])
544    a = np_array_ops.array(iss, copy=False)
545    expected = array_ops.scatter_nd([[1], [9]],
546                                    array_ops.ones([2, 3], dtype=dtype),
547                                    [10, 3])
548    self.assertAllEqual(expected, a)
549
550
551class ArrayMethodsTest(test.TestCase):
552
553  def setUp(self):
554    super(ArrayMethodsTest, self).setUp()
555    set_up_virtual_devices()
556    self.array_transforms = [
557        lambda x: x,
558        ops.convert_to_tensor,
559        np.array,
560        np_array_ops.array,
561    ]
562
563  def testAllAny(self):
564
565    def run_test(arr, *args, **kwargs):
566      for fn in self.array_transforms:
567        arr = fn(arr)
568        self.match(
569            np_array_ops.all(arr, *args, **kwargs),
570            np.all(arr, *args, **kwargs))
571        self.match(
572            np_array_ops.any(arr, *args, **kwargs),
573            np.any(arr, *args, **kwargs))
574
575    run_test(0)
576    run_test(1)
577    run_test([])
578    run_test([[True, False], [True, True]])
579    run_test([[True, False], [True, True]], axis=0)
580    run_test([[True, False], [True, True]], axis=0, keepdims=True)
581    run_test([[True, False], [True, True]], axis=1)
582    run_test([[True, False], [True, True]], axis=1, keepdims=True)
583    run_test([[True, False], [True, True]], axis=(0, 1))
584    run_test([[True, False], [True, True]], axis=(0, 1), keepdims=True)
585    run_test([5.2, 3.5], axis=0)
586    run_test([1, 0], axis=0)
587
588  def testCompress(self):
589
590    def run_test(condition, arr, *args, **kwargs):
591      for fn1 in self.array_transforms:
592        for fn2 in self.array_transforms:
593          arg1 = fn1(condition)
594          arg2 = fn2(arr)
595          self.match(
596              np_array_ops.compress(arg1, arg2, *args, **kwargs),
597              np.compress(
598                  np.asarray(arg1).astype(np.bool_), arg2, *args, **kwargs))
599
600    run_test([True], 5)
601    run_test([False], 5)
602    run_test([], 5)
603    run_test([True, False, True], [1, 2, 3])
604    run_test([True, False], [1, 2, 3])
605    run_test([False, True], [[1, 2], [3, 4]])
606    run_test([1, 0, 1], [1, 2, 3])
607    run_test([1, 0], [1, 2, 3])
608    run_test([0, 1], [[1, 2], [3, 4]])
609    run_test([True], [[1, 2], [3, 4]])
610    run_test([False, True], [[1, 2], [3, 4]], axis=1)
611    run_test([False, True], [[1, 2], [3, 4]], axis=0)
612    run_test([False, True], [[1, 2], [3, 4]], axis=-1)
613    run_test([False, True], [[1, 2], [3, 4]], axis=-2)
614
615  def testCopy(self):
616
617    def run_test(arr, *args, **kwargs):
618      for fn in self.array_transforms:
619        arg = fn(arr)
620        self.match(
621            np_array_ops.copy(arg, *args, **kwargs),
622            np.copy(arg, *args, **kwargs))
623
624    run_test([])
625    run_test([1, 2, 3])
626    run_test([1., 2., 3.])
627    run_test([True])
628    run_test(np.arange(9).reshape((3, 3)).tolist())
629
630    a = np_array_ops.asarray(0)
631    self.assertNotIn('CPU:1', a.backing_device)
632    with ops.device('CPU:1'):
633      self.assertIn('CPU:1', np_array_ops.array(a, copy=True)
634                    .backing_device)
635      self.assertIn('CPU:1', np_array_ops.array(np.array(0), copy=True)
636                    .backing_device)
637
638  def testCumProdAndSum(self):
639
640    def run_test(arr, *args, **kwargs):
641      for fn in self.array_transforms:
642        arg = fn(arr)
643        self.match(
644            np_array_ops.cumprod(arg, *args, **kwargs),
645            np.cumprod(arg, *args, **kwargs))
646        self.match(
647            np_array_ops.cumsum(arg, *args, **kwargs),
648            np.cumsum(arg, *args, **kwargs))
649
650    run_test([])
651    run_test([1, 2, 3])
652    run_test([1, 2, 3], dtype=float)
653    run_test([1, 2, 3], dtype=np.float32)
654    run_test([1, 2, 3], dtype=np.float64)
655    run_test([1., 2., 3.])
656    run_test([1., 2., 3.], dtype=int)
657    run_test([1., 2., 3.], dtype=np.int32)
658    run_test([1., 2., 3.], dtype=np.int64)
659    run_test([[1, 2], [3, 4]], axis=1)
660    run_test([[1, 2], [3, 4]], axis=0)
661    run_test([[1, 2], [3, 4]], axis=-1)
662    run_test([[1, 2], [3, 4]], axis=-2)
663
664  def testImag(self):
665
666    def run_test(arr, *args, **kwargs):
667      for fn in self.array_transforms:
668        arg = fn(arr)
669        self.match(
670            np_array_ops.imag(arg, *args, **kwargs),
671            # np.imag may return a scalar so we convert to a np.ndarray.
672            np.array(np.imag(arg, *args, **kwargs)))
673
674    run_test(1)
675    run_test(5.5)
676    run_test(5 + 3j)
677    run_test(3j)
678    run_test([])
679    run_test([1, 2, 3])
680    run_test([1 + 5j, 2 + 3j])
681    run_test([[1 + 5j, 2 + 3j], [1 + 7j, 2 + 8j]])
682
683  def testAMaxAMin(self):
684
685    def run_test(arr, *args, **kwargs):
686      axis = kwargs.pop('axis', None)
687      for fn1 in self.array_transforms:
688        for fn2 in self.array_transforms:
689          arr_arg = fn1(arr)
690          axis_arg = fn2(axis) if axis is not None else None
691          self.match(
692              np_array_ops.amax(arr_arg, axis=axis_arg, *args, **kwargs),
693              np.amax(arr_arg, axis=axis, *args, **kwargs))
694          self.match(
695              np_array_ops.amin(arr_arg, axis=axis_arg, *args, **kwargs),
696              np.amin(arr_arg, axis=axis, *args, **kwargs))
697
698    run_test([1, 2, 3])
699    run_test([1., 2., 3.])
700    run_test([[1, 2], [3, 4]], axis=1)
701    run_test([[1, 2], [3, 4]], axis=0)
702    run_test([[1, 2], [3, 4]], axis=-1)
703    run_test([[1, 2], [3, 4]], axis=-2)
704    run_test([[1, 2], [3, 4]], axis=(0, 1))
705    run_test(np.arange(8).reshape((2, 2, 2)).tolist(), axis=(0, 2))
706    run_test(
707        np.arange(8).reshape((2, 2, 2)).tolist(), axis=(0, 2), keepdims=True)
708    run_test(np.arange(8).reshape((2, 2, 2)).tolist(), axis=(2, 0))
709    run_test(
710        np.arange(8).reshape((2, 2, 2)).tolist(), axis=(2, 0), keepdims=True)
711    self.assertRaises(ValueError, np_array_ops.amax, np.ones([2, 2]), out=[])
712    self.assertRaises(ValueError, np_array_ops.amin, np.ones([2, 2]), out=[])
713
714  def testMean(self):
715
716    def run_test(arr, *args, **kwargs):
717      axis = kwargs.pop('axis', None)
718      for fn1 in self.array_transforms:
719        for fn2 in self.array_transforms:
720          arr_arg = fn1(arr)
721          axis_arg = fn2(axis) if axis is not None else None
722          self.match(
723              np_array_ops.mean(arr_arg, axis=axis_arg, *args, **kwargs),
724              np.mean(arr_arg, axis=axis, *args, **kwargs))
725
726    run_test([1, 2, 1])
727    run_test([1., 2., 1.])
728    run_test([1., 2., 1.], dtype=int)
729    run_test([[1, 2], [3, 4]], axis=1)
730    run_test([[1, 2], [3, 4]], axis=0)
731    run_test([[1, 2], [3, 4]], axis=-1)
732    run_test([[1, 2], [3, 4]], axis=-2)
733    run_test([[1, 2], [3, 4]], axis=(0, 1))
734    run_test(np.arange(8).reshape((2, 2, 2)).tolist(), axis=(0, 2))
735    run_test(
736        np.arange(8).reshape((2, 2, 2)).tolist(), axis=(0, 2), keepdims=True)
737    run_test(np.arange(8).reshape((2, 2, 2)).tolist(), axis=(2, 0))
738    run_test(
739        np.arange(8).reshape((2, 2, 2)).tolist(), axis=(2, 0), keepdims=True)
740    self.assertRaises(ValueError, np_array_ops.mean, np.ones([2, 2]), out=[])
741
742  def testStd(self):
743
744    def run_test(arr, *args, **kwargs):
745      axis = kwargs.pop('axis', None)
746      for fn1 in self.array_transforms:
747        for fn2 in self.array_transforms:
748          arr_arg = fn1(arr)
749          axis_arg = fn2(axis) if axis is not None else None
750          self.match(
751              np_array_ops.std(arr_arg, axis=axis_arg, *args, **kwargs),
752              np.std(arr_arg, axis=axis, *args, **kwargs))
753
754    run_test([1, 2, 1])
755    run_test([1., 2., 1.])
756    run_test([1.j, 2., 1.j])
757    run_test([[1, 2], [3, 4]], axis=1)
758    run_test([[1, 2], [3, 4]], axis=0)
759    run_test([[1, 2], [3, 4]], axis=-1)
760    run_test([[1, 2], [3, 4]], axis=-2)
761    run_test([[1, 2], [3, 4]], axis=(0, 1))
762    run_test(np.arange(8).reshape((2, 2, 2)).tolist(), axis=(0, 2))
763    run_test(
764        np.arange(8).reshape((2, 2, 2)).tolist(), axis=(0, 2), keepdims=True)
765    run_test(np.arange(8).reshape((2, 2, 2)).tolist(), axis=(2, 0))
766    run_test(
767        np.arange(8).reshape((2, 2, 2)).tolist(), axis=(2, 0), keepdims=True)
768
769  def testVar(self):
770
771    def run_test(arr, *args, **kwargs):
772      axis = kwargs.pop('axis', None)
773      for fn1 in self.array_transforms:
774        for fn2 in self.array_transforms:
775          arr_arg = fn1(arr)
776          axis_arg = fn2(axis) if axis is not None else None
777          self.match(
778              np_array_ops.var(arr_arg, axis=axis_arg, *args, **kwargs),
779              np.var(arr_arg, axis=axis, *args, **kwargs))
780
781    run_test([1, 2, 1])
782    run_test([1., 2., 1.])
783    run_test([1.j, 2., 1.j])
784    run_test([1., 2., 1.], dtype=np.int64)
785    run_test([[1, 2], [3, 4]], axis=1)
786    run_test([[1, 2], [3, 4]], axis=0)
787    run_test([[1, 2], [3, 4]], axis=-1)
788    run_test([[1, 2], [3, 4]], axis=-2)
789    run_test([[1, 2], [3, 4]], axis=(0, 1))
790    run_test(np.arange(8).reshape((2, 2, 2)).tolist(), axis=(0, 2))
791    run_test(
792        np.arange(8).reshape((2, 2, 2)).tolist(), axis=(0, 2), keepdims=True)
793    run_test(np.arange(8).reshape((2, 2, 2)).tolist(), axis=(2, 0))
794    run_test(
795        np.arange(8).reshape((2, 2, 2)).tolist(), axis=(2, 0), keepdims=True)
796    self.assertRaises(ValueError, np_array_ops.var, np.ones([2, 2]), out=[])
797
798  def testProd(self):
799
800    def run_test(arr, *args, **kwargs):
801      for fn in self.array_transforms:
802        arg = fn(arr)
803        self.match(
804            np_array_ops.prod(arg, *args, **kwargs),
805            np.prod(arg, *args, **kwargs))
806
807    run_test([1, 2, 3])
808    run_test([1., 2., 3.])
809    run_test(np.array([1, 2, 3], dtype=np.int16))
810    run_test([[1, 2], [3, 4]], axis=1)
811    run_test([[1, 2], [3, 4]], axis=0)
812    run_test([[1, 2], [3, 4]], axis=-1)
813    run_test([[1, 2], [3, 4]], axis=-2)
814    run_test([[1, 2], [3, 4]], axis=(0, 1))
815    run_test(np.arange(8).reshape((2, 2, 2)).tolist(), axis=(0, 2))
816    run_test(
817        np.arange(8).reshape((2, 2, 2)).tolist(), axis=(0, 2), keepdims=True)
818    run_test(np.arange(8).reshape((2, 2, 2)).tolist(), axis=(2, 0))
819    run_test(
820        np.arange(8).reshape((2, 2, 2)).tolist(), axis=(2, 0), keepdims=True)
821
822  def _testReduce(self, math_fun, np_fun, name):
823    axis_transforms = [
824        lambda x: x,  # Identity,
825        ops.convert_to_tensor,
826        np.array,
827        np_array_ops.array,
828        lambda x: np_array_ops.array(x, dtype=np.float32),
829        lambda x: np_array_ops.array(x, dtype=np.float64),
830    ]
831
832    def run_test(a, **kwargs):
833      axis = kwargs.pop('axis', None)
834      for fn1 in self.array_transforms:
835        for fn2 in axis_transforms:
836          arg1 = fn1(a)
837          axis_arg = fn2(axis) if axis is not None else None
838          self.match(
839              math_fun(arg1, axis=axis_arg, **kwargs),
840              np_fun(arg1, axis=axis, **kwargs),
841              msg='{}({}, axis={}, keepdims={})'.format(name, arg1, axis,
842                                                        kwargs.get('keepdims')))
843
844    run_test(5)
845    run_test([2, 3])
846    run_test([[2, -3], [-6, 7]])
847    run_test([[2, -3], [-6, 7]], axis=0)
848    run_test([[2, -3], [-6, 7]], axis=0, keepdims=True)
849    run_test([[2, -3], [-6, 7]], axis=1)
850    run_test([[2, -3], [-6, 7]], axis=1, keepdims=True)
851    run_test([[2, -3], [-6, 7]], axis=(0, 1))
852    run_test([[2, -3], [-6, 7]], axis=(1, 0))
853
854  def testSum(self):
855    self._testReduce(np_array_ops.sum, np.sum, 'sum')
856
857  def testAmax(self):
858    self._testReduce(np_array_ops.amax, np.amax, 'amax')
859
860  def testSize(self):
861
862    def run_test(arr, axis=None):
863      onp_arr = np.array(arr)
864      self.assertEqual(np_array_ops.size(arr, axis), np.size(onp_arr, axis))
865
866    run_test(np_array_ops.array([1]))
867    run_test(np_array_ops.array([1, 2, 3, 4, 5]))
868    run_test(np_array_ops.ones((2, 3, 2)))
869    run_test(np_array_ops.ones((3, 2)))
870    run_test(np_array_ops.zeros((5, 6, 7)))
871    run_test(1)
872    run_test(np_array_ops.ones((3, 2, 1)))
873    run_test(constant_op.constant(5))
874    run_test(constant_op.constant([1, 1, 1]))
875    self.assertRaises(NotImplementedError, np_array_ops.size, np.ones((2, 2)),
876                      1)
877
878    @def_function.function(input_signature=[
879        tensor_spec.TensorSpec(dtype=dtypes.float64, shape=None)])
880    def f(arr):
881      arr = np_array_ops.asarray(arr)
882      return np_array_ops.size(arr)
883
884    self.assertEqual(f(np_array_ops.ones((3, 2))).numpy(), 6)
885
886  def testRavel(self):
887
888    def run_test(arr, *args, **kwargs):
889      for fn in self.array_transforms:
890        arg = fn(arr)
891        self.match(
892            np_array_ops.ravel(arg, *args, **kwargs),
893            np.ravel(arg, *args, **kwargs))
894
895    run_test(5)
896    run_test(5.)
897    run_test([])
898    run_test([[]])
899    run_test([[], []])
900    run_test([1, 2, 3])
901    run_test([1., 2., 3.])
902    run_test([[1, 2], [3, 4]])
903    run_test(np.arange(8).reshape((2, 2, 2)).tolist())
904
905  def testReal(self):
906
907    def run_test(arr, *args, **kwargs):
908      for fn in self.array_transforms:
909        arg = fn(arr)
910        self.match(
911            np_array_ops.real(arg, *args, **kwargs),
912            np.array(np.real(arg, *args, **kwargs)))
913
914    run_test(1)
915    run_test(5.5)
916    run_test(5 + 3j)
917    run_test(3j)
918    run_test([])
919    run_test([1, 2, 3])
920    run_test([1 + 5j, 2 + 3j])
921    run_test([[1 + 5j, 2 + 3j], [1 + 7j, 2 + 8j]])
922
923  def testRepeat(self):
924
925    def run_test(arr, repeats, *args, **kwargs):
926      for fn1 in self.array_transforms:
927        for fn2 in self.array_transforms:
928          arr_arg = fn1(arr)
929          repeats_arg = fn2(repeats)
930          self.match(
931              np_array_ops.repeat(arr_arg, repeats_arg, *args, **kwargs),
932              np.repeat(arr_arg, repeats_arg, *args, **kwargs))
933
934    run_test(1, 2)
935    run_test([1, 2], 2)
936    run_test([1, 2], [2])
937    run_test([1, 2], [1, 2])
938    run_test([[1, 2], [3, 4]], 3, axis=0)
939    run_test([[1, 2], [3, 4]], 3, axis=1)
940    run_test([[1, 2], [3, 4]], [3], axis=0)
941    run_test([[1, 2], [3, 4]], [3], axis=1)
942    run_test([[1, 2], [3, 4]], [3, 2], axis=0)
943    run_test([[1, 2], [3, 4]], [3, 2], axis=1)
944    run_test([[1, 2], [3, 4]], [3, 2], axis=-1)
945    run_test([[1, 2], [3, 4]], [3, 2], axis=-2)
946
947  def testAround(self):
948
949    def run_test(arr, *args, **kwargs):
950      for fn in self.array_transforms:
951        arg = fn(arr)
952        self.match(
953            np_array_ops.around(arg, *args, **kwargs),
954            np.around(arg, *args, **kwargs))
955
956    run_test(5.5)
957    run_test(5.567, decimals=2)
958    run_test([])
959    run_test([1.27, 2.49, 2.75], decimals=1)
960    run_test([23.6, 45.1], decimals=-1)
961
962  def testReshape(self):
963
964    def run_test(arr, newshape, *args, **kwargs):
965      for fn1 in self.array_transforms:
966        for fn2 in self.array_transforms:
967          arr_arg = fn1(arr)
968          newshape_arg = fn2(newshape)
969          self.match(
970              np_array_ops.reshape(arr_arg, newshape_arg, *args, **kwargs),
971              np.reshape(arr_arg, newshape, *args, **kwargs))
972
973    run_test(5, [-1])
974    run_test([], [-1])
975    run_test([1, 2, 3], [1, 3])
976    run_test([1, 2, 3], [3, 1])
977    run_test([1, 2, 3, 4], [2, 2])
978    run_test([1, 2, 3, 4], [2, 1, 2])
979
980  def testExpandDims(self):
981
982    def run_test(arr, axis):
983      self.match(np_array_ops.expand_dims(arr, axis), np.expand_dims(arr, axis))
984
985    run_test([1, 2, 3], 0)
986    run_test([1, 2, 3], 1)
987
988  def testSqueeze(self):
989
990    def run_test(arr, *args, **kwargs):
991      for fn in self.array_transforms:
992        arg = fn(arr)
993        # Note: np.squeeze ignores the axis arg for non-ndarray objects.
994        # This looks like a bug: https://github.com/numpy/numpy/issues/8201
995        # So we convert the arg to np.ndarray before passing to np.squeeze.
996        self.match(
997            np_array_ops.squeeze(arg, *args, **kwargs),
998            np.squeeze(np.array(arg), *args, **kwargs))
999
1000    run_test(5)
1001    run_test([])
1002    run_test([5])
1003    run_test([[1, 2, 3]])
1004    run_test([[[1], [2], [3]]])
1005    run_test([[[1], [2], [3]]], axis=0)
1006    run_test([[[1], [2], [3]]], axis=2)
1007    run_test([[[1], [2], [3]]], axis=(0, 2))
1008    run_test([[[1], [2], [3]]], axis=-1)
1009    run_test([[[1], [2], [3]]], axis=-3)
1010
1011  def testTranspose(self):
1012
1013    def run_test(arr, axes=None):
1014      for fn1 in self.array_transforms:
1015        for fn2 in self.array_transforms:
1016          arr_arg = fn1(arr)
1017          axes_arg = fn2(axes) if axes is not None else None
1018          self.match(
1019              np_array_ops.transpose(arr_arg, axes_arg),
1020              np.transpose(arr_arg, axes))
1021
1022    run_test(5)
1023    run_test([])
1024    run_test([5])
1025    run_test([5, 6, 7])
1026    run_test(np.arange(30).reshape(2, 3, 5).tolist())
1027    run_test(np.arange(30).reshape(2, 3, 5).tolist(), [0, 1, 2])
1028    run_test(np.arange(30).reshape(2, 3, 5).tolist(), [0, 2, 1])
1029    run_test(np.arange(30).reshape(2, 3, 5).tolist(), [1, 0, 2])
1030    run_test(np.arange(30).reshape(2, 3, 5).tolist(), [1, 2, 0])
1031    run_test(np.arange(30).reshape(2, 3, 5).tolist(), [2, 0, 1])
1032    run_test(np.arange(30).reshape(2, 3, 5).tolist(), [2, 1, 0])
1033
1034  def match_shape(self, actual, expected, msg=None):
1035    if msg:
1036      msg = 'Shape match failed for: {}. Expected: {} Actual: {}'.format(
1037          msg, expected.shape, actual.shape)
1038    self.assertEqual(actual.shape, expected.shape, msg=msg)
1039
1040  def match_dtype(self, actual, expected, msg=None):
1041    if msg:
1042      msg = 'Dtype match failed for: {}. Expected: {} Actual: {}.'.format(
1043          msg, expected.dtype, actual.dtype)
1044    self.assertEqual(actual.dtype, expected.dtype, msg=msg)
1045
1046  def match(self, actual, expected, msg=None, check_dtype=True):
1047    msg_ = 'Expected: {} Actual: {}'.format(expected, actual)
1048    if msg:
1049      msg = '{} {}'.format(msg_, msg)
1050    else:
1051      msg = msg_
1052    self.assertIsInstance(actual, np_arrays.ndarray)
1053    if check_dtype:
1054      self.match_dtype(actual, expected, msg)
1055    self.match_shape(actual, expected, msg)
1056    if not actual.shape.rank:
1057      self.assertAllClose(actual.tolist(), expected.tolist())
1058    else:
1059      self.assertAllClose(actual.tolist(), expected.tolist())
1060
1061  def testPad(self):
1062    t = [[1, 2, 3], [4, 5, 6]]
1063    paddings = [[
1064        1,
1065        1,
1066    ], [2, 2]]
1067    self.assertAllEqual(
1068        np_array_ops.pad(t, paddings, 'constant'),
1069        [[0, 0, 0, 0, 0, 0, 0], [0, 0, 1, 2, 3, 0, 0], [0, 0, 4, 5, 6, 0, 0],
1070         [0, 0, 0, 0, 0, 0, 0]])
1071
1072    self.assertAllEqual(
1073        np_array_ops.pad(t, paddings, 'reflect'),
1074        [[6, 5, 4, 5, 6, 5, 4], [3, 2, 1, 2, 3, 2, 1], [6, 5, 4, 5, 6, 5, 4],
1075         [3, 2, 1, 2, 3, 2, 1]])
1076
1077    self.assertAllEqual(
1078        np_array_ops.pad(t, paddings, 'symmetric'),
1079        [[2, 1, 1, 2, 3, 3, 2], [2, 1, 1, 2, 3, 3, 2], [5, 4, 4, 5, 6, 6, 5],
1080         [5, 4, 4, 5, 6, 6, 5]])
1081
1082  def testTake(self):
1083    a = [4, 3, 5, 7, 6, 8]
1084    indices = [0, 1, 4]
1085    self.assertAllEqual([4, 3, 6], np_array_ops.take(a, indices))
1086    indices = [[0, 1], [2, 3]]
1087    self.assertAllEqual([[4, 3], [5, 7]], np_array_ops.take(a, indices))
1088    a = [[4, 3, 5], [7, 6, 8]]
1089    self.assertAllEqual([[4, 3], [5, 7]], np_array_ops.take(a, indices))
1090    a = np.random.rand(2, 16, 3)
1091    axis = 1
1092    self.assertAllEqual(
1093        np.take(a, indices, axis=axis),
1094        np_array_ops.take(a, indices, axis=axis))
1095
1096  def testWhere(self):
1097    self.assertAllEqual([[1.0, 1.0], [1.0, 1.0]],
1098                        np_array_ops.where([True], [1.0, 1.0],
1099                                           [[0, 0], [0, 0]]))
1100
1101  def testShape(self):
1102    self.assertAllEqual((1, 2), np_array_ops.shape([[0, 0]]))
1103
1104  def testSwapaxes(self):
1105    x = [[1, 2, 3]]
1106    self.assertAllEqual([[1], [2], [3]], np_array_ops.swapaxes(x, 0, 1))
1107    self.assertAllEqual([[1], [2], [3]], np_array_ops.swapaxes(x, -2, -1))
1108    x = [[[0, 1], [2, 3]], [[4, 5], [6, 7]]]
1109    self.assertAllEqual([[[0, 4], [2, 6]], [[1, 5], [3, 7]]],
1110                        np_array_ops.swapaxes(x, 0, 2))
1111    self.assertAllEqual([[[0, 4], [2, 6]], [[1, 5], [3, 7]]],
1112                        np_array_ops.swapaxes(x, -3, -1))
1113
1114  def testMoveaxis(self):
1115
1116    def _test(*args):
1117      # pylint: disable=no-value-for-parameter
1118      expected = np.moveaxis(*args)
1119      raw_ans = np_array_ops.moveaxis(*args)
1120
1121      self.assertAllEqual(expected, raw_ans)
1122
1123    a = np.random.rand(1, 2, 3, 4, 5, 6)
1124
1125    # Basic
1126    _test(a, (0, 2), (3, 5))
1127    _test(a, (0, 2), (-1, -3))
1128    _test(a, (-6, -4), (3, 5))
1129    _test(a, (-6, -4), (-1, -3))
1130    _test(a, 0, 4)
1131    _test(a, -6, -2)
1132    _test(a, tuple(range(6)), tuple(range(6)))
1133    _test(a, tuple(range(6)), tuple(reversed(range(6))))
1134    _test(a, (), ())
1135
1136  def testNdim(self):
1137    self.assertAllEqual(0, np_array_ops.ndim(0.5))
1138    self.assertAllEqual(1, np_array_ops.ndim([1, 2]))
1139
1140  def testIsscalar(self):
1141    self.assertTrue(np_array_ops.isscalar(0.5))
1142    self.assertTrue(np_array_ops.isscalar(5))
1143    self.assertTrue(np_array_ops.isscalar(False))
1144    self.assertFalse(np_array_ops.isscalar([1, 2]))
1145
1146  def assertListEqual(self, a, b):
1147    self.assertAllEqual(len(a), len(b))
1148    for x, y in zip(a, b):
1149      self.assertAllEqual(x, y)
1150
1151  def testSplit(self):
1152    x = np_array_ops.arange(9)
1153    y = np_array_ops.split(x, 3)
1154    self.assertListEqual([([0, 1, 2]), ([3, 4, 5]), ([6, 7, 8])], y)
1155
1156    x = np_array_ops.arange(8)
1157    y = np_array_ops.split(x, [3, 5, 6, 10])
1158    self.assertListEqual([([0, 1, 2]), ([3, 4]), ([5]), ([6, 7]), ([])], y)
1159
1160  def testSign(self):
1161    state = np.random.RandomState(0)
1162    test_types = [np.float16, np.float32, np.float64, np.int32, np.int64,
1163                  np.complex64, np.complex128]
1164    test_shapes = [(), (1,), (2, 3, 4), (2, 3, 0, 4)]
1165
1166    for dtype in test_types:
1167      for shape in test_shapes:
1168        if np.issubdtype(dtype, np.complexfloating):
1169          arr = (np.asarray(state.randn(*shape) * 100, dtype=dtype) +
1170                 1j * np.asarray(state.randn(*shape) * 100, dtype=dtype))
1171        else:
1172          arr = np.asarray(state.randn(*shape) * 100, dtype=dtype)
1173        self.match(np_array_ops.sign(arr), np.sign(arr))
1174
1175
1176class ArrayManipulationTest(test.TestCase):
1177
1178  def setUp(self):
1179    super(ArrayManipulationTest, self).setUp()
1180    self.array_transforms = [
1181        lambda x: x,
1182        ops.convert_to_tensor,
1183        np.array,
1184        np_array_ops.array,
1185    ]
1186
1187  def testBroadcastTo(self):
1188
1189    def run_test(arr, shape):
1190      for fn in self.array_transforms:
1191        arg1 = fn(arr)
1192        self.match(
1193            np_array_ops.broadcast_to(arg1, shape),
1194            np.broadcast_to(arg1, shape))
1195
1196    run_test(1, 2)
1197    run_test(1, (2, 2))
1198    run_test([1, 2], (2, 2))
1199    run_test([[1], [2]], (2, 2))
1200    run_test([[1, 2]], (3, 2))
1201    run_test([[[1, 2]], [[3, 4]], [[5, 6]]], (3, 4, 2))
1202
1203  def testIx_(self):
1204    possible_arys = [[True, True], [True, False], [False, False],
1205                     list(range(5)), np_array_ops.empty(0, dtype=np.int64)]
1206    for r in range(len(possible_arys)):
1207      for arys in itertools.combinations_with_replacement(possible_arys, r):
1208        tnp_ans = np_array_ops.ix_(*arys)
1209        onp_ans = np.ix_(*arys)
1210        for t, o in zip(tnp_ans, onp_ans):
1211          self.match(t, o)
1212
1213  def match_shape(self, actual, expected, msg=None):
1214    if msg:
1215      msg = 'Shape match failed for: {}. Expected: {} Actual: {}'.format(
1216          msg, expected.shape, actual.shape)
1217    self.assertEqual(actual.shape, expected.shape, msg=msg)
1218
1219  def match_dtype(self, actual, expected, msg=None):
1220    if msg:
1221      msg = 'Dtype match failed for: {}. Expected: {} Actual: {}.'.format(
1222          msg, expected.dtype, actual.dtype)
1223    self.assertEqual(actual.dtype, expected.dtype, msg=msg)
1224
1225  def match(self, actual, expected, msg=None):
1226    msg_ = 'Expected: {} Actual: {}'.format(expected, actual)
1227    if msg:
1228      msg = '{} {}'.format(msg_, msg)
1229    else:
1230      msg = msg_
1231    self.assertIsInstance(actual, np_arrays.ndarray)
1232    self.match_dtype(actual, expected, msg)
1233    self.match_shape(actual, expected, msg)
1234    if not actual.shape.rank:
1235      self.assertEqual(actual.tolist(), expected.tolist())
1236    else:
1237      self.assertSequenceEqual(actual.tolist(), expected.tolist())
1238
1239
1240class ArrayMathTest(test.TestCase, parameterized.TestCase):
1241
1242  @parameterized.named_parameters(
1243      ('complex_mul_1', 2j, [2], int, [4j], operator.mul),
1244      ('complex_mul_2', 2j, [0], int, [0], operator.mul),
1245      ('complex_mul_3', 2j, [-2.0], float, [-4j], operator.mul),
1246      ('complex_mul_4', 2j, [2j], complex, [-4], operator.mul),
1247      ('float_mul_1', 2.0, [2], int, [4], operator.mul),
1248      ('float_mul_2', 2.0, [0], int, [0], operator.mul),
1249      ('float_mul_3', 2.0, [-2.0], float, [-4], operator.mul),
1250      ('float_mul_4', 2.0, [2j], complex, [4j], operator.mul))
1251  def testConstantBinOp(self, a, b, b_type, expected_result, test_func):
1252    b = np_array_ops.array(b, dtype=b_type)
1253    result = test_func(a, b)
1254    if np.issubdtype(result.dtype.as_numpy_dtype, np.inexact):
1255      self.assertAllClose(result, expected_result)
1256    else:
1257      self.assertAllEqual(result, expected_result)
1258
1259
1260class StringArrayTest(test.TestCase, parameterized.TestCase):
1261
1262  StringParameters = parameterized.named_parameters(  # pylint: disable=invalid-name
1263      # Tensorflow always encodes python string into bytes, regardless of
1264      # requested dtype.
1265      ('str_u8', 'abcde\U0001f005', 'U8', b'abcde\xf0\x9f\x80\x85'),
1266      ('str_s8', 'abcde\U0001f005', 'S8', b'abcde\xf0\x9f\x80\x85'),
1267      ('str_none', 'abcde\U0001f005', None, b'abcde\xf0\x9f\x80\x85'),
1268      ('zstr_u8', '\0abcde\U0001f005', 'U8', b'\0abcde\xf0\x9f\x80\x85'),
1269      ('zstr_s8', '\0abcde\U0001f005', 'S8', b'\0abcde\xf0\x9f\x80\x85'),
1270      ('zstr_none', '\0abcde\U0001f005', None, b'\0abcde\xf0\x9f\x80\x85'),
1271      ('bytes_u8', b'abcdef', 'U8', b'abcdef'),
1272      ('bytes_s8', b'abcdef', 'S8', b'abcdef'),
1273      ('bytes_none', b'abcdef', None, b'abcdef'),
1274      ('zbytes_u8', b'\0abcdef', 'U8', b'\0abcdef'),
1275      ('zbytes_s8', b'\0abcdef', 'S8', b'\0abcdef'),
1276      ('zbytes_none', b'\0abcdef', None, b'\0abcdef'),
1277  )
1278
1279  @StringParameters
1280  def testArray(self, a, dtype, a_as_bytes):
1281    b = np_array_ops.array(a, dtype=dtype)
1282    self.assertIsInstance(b.numpy(), bytes)
1283    self.assertEqual(b.numpy(), a_as_bytes)
1284
1285  @StringParameters
1286  def testAsArray(self, a, dtype, a_as_bytes):
1287    b = np_array_ops.asarray(a, dtype=dtype)
1288    self.assertIsInstance(b.numpy(), bytes)
1289    self.assertEqual(b.numpy(), a_as_bytes)
1290
1291  @StringParameters
1292  def testZerosLike(self, a, dtype, unused_a_as_bytes):
1293    b = np_array_ops.zeros_like(a, dtype=dtype)
1294    self.assertIsInstance(b.numpy(), bytes)
1295    self.assertEqual(b.numpy(), b'')
1296
1297  @StringParameters
1298  def testEmptyLike(self, a, dtype, unused_a_as_bytes):
1299    b = np_array_ops.empty_like(a, dtype=dtype)
1300    self.assertIsInstance(b.numpy(), bytes)
1301    self.assertEqual(b.numpy(), b'')
1302
1303
1304if __name__ == '__main__':
1305  ops.enable_eager_execution()
1306  ops.enable_numpy_style_type_promotion()
1307  np_math_ops.enable_numpy_methods_on_tensor()
1308  test.main()
1309