def testZeros(self):
        for s in self.all_shapes:
            actual = array_creation.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 = array_creation.zeros(s, t)
            expected = np.zeros(s, t)
            msg = 'shape: {}, dtype: {}'.format(s, t)
            self.match(actual, expected, msg)
Exemplo n.º 2
0
 def _testEvalOnShapes(self, transformer):
   def f(a, b):
     return math.sum(math.sqrt(math.exp(a)) + b)
   f_prime = transformer(f)
   shape = [10]
   dtype = np.float16
   a = array_creation.zeros(shape=shape, dtype=dtype)
   b = array_creation.zeros(shape=shape, dtype=dtype)
   expected = f(a, b)
   got = f_prime(a, b)
   self.assertAllEqual(expected.shape, got.shape)
   self.assertAllEqual(expected.dtype, got.dtype)
   # Call again since the code path is different on second call
   got = f_prime(a, b)
   self.assertAllEqual(expected.shape, got.shape)
   self.assertAllEqual(expected.dtype, got.dtype)
 def testAsAnyArray(self):
     for a, dtype in itertools.product(self.all_arrays, self.all_types):
         self.match(array_creation.asanyarray(a, dtype=dtype),
                    np.asanyarray(a, dtype=dtype))
     zeros_list = array_creation.zeros(5)
     # Same instance is returned if no dtype is specified and input is ndarray.
     self.assertIs(array_creation.asanyarray(zeros_list), zeros_list)
     # Different instance is returned if dtype is specified and input is ndarray.
     self.assertIsNot(array_creation.asanyarray(zeros_list, dtype=int),
                      zeros_list)
    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(
                array_creation.array(a, dtype=dtype, ndmin=ndmin, copy=copy),
                np.array(a, dtype=dtype, ndmin=ndmin, copy=copy))

        zeros_list = array_creation.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(
            array_creation.array(zeros_list, copy=False).data, zeros_list.data)
        self.assertIs(
            array_creation.array(zeros_list.data, copy=False).data,
            zeros_list.data)

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

        # Backing tensor is different if dtype is not satisfied.
        self.assertIsNot(
            array_creation.array(zeros_list, copy=False, dtype=int).data,
            zeros_list.data)
        self.assertIsNot(
            array_creation.array(zeros_list.data, copy=False, dtype=int).data,
            zeros_list.data)
        self.assertIs(
            array_creation.array(zeros_list, copy=False, dtype=float).data,
            zeros_list.data)
        self.assertIs(
            array_creation.array(zeros_list.data, copy=False,
                                 dtype=float).data, zeros_list.data)
Exemplo n.º 5
0
 def f_prime(a, b):
   shape_dtype = extensions.eval_on_shapes(f)(a, b)
   return array_creation.zeros(shape=shape_dtype.shape,
                               dtype=shape_dtype.dtype)