Пример #1
0
    def testZeros(self):
        for s in self.all_shapes:
            actual = 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 = array_ops.zeros(s, t)
            expected = np.zeros(s, t)
            msg = 'shape: {}, dtype: {}'.format(s, t)
            self.match(actual, expected, msg)
Пример #2
0
  def _testEvalOnShapes(self, transformer):
    def f(a, b):
      return array_ops.sum(math_ops.sqrt(math_ops.exp(a)) + b)

    f_prime = transformer(f)
    shape = [10]
    dtype = np.float16
    a = array_ops.zeros(shape=shape, dtype=dtype)
    b = array_ops.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)
Пример #3
0
 def testAsAnyArray(self):
     for a, dtype in itertools.product(self.all_arrays, self.all_types):
         self.match(array_ops.asanyarray(a, dtype=dtype),
                    np.asanyarray(a, dtype=dtype))
     zeros_list = array_ops.zeros(5)
     # Same instance is returned if no dtype is specified and input is ndarray.
     self.assertIs(array_ops.asanyarray(zeros_list), zeros_list)
     # Different instance is returned if dtype is specified and input is ndarray.
     self.assertIsNot(array_ops.asanyarray(zeros_list, dtype=int),
                      zeros_list)
Пример #4
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(array_ops.array(a, dtype=dtype, ndmin=ndmin, copy=copy),
                       np.array(a, dtype=dtype, ndmin=ndmin, copy=copy))

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

        # Backing tensor is different if ndmin is not satisfied.
        self.assertIsNot(
            array_ops.array(zeros_list, copy=False, ndmin=2).data,
            zeros_list.data)
        self.assertIsNot(
            array_ops.array(zeros_list.data, copy=False, ndmin=2).data,
            zeros_list.data)
        self.assertIs(
            array_ops.array(zeros_list, copy=False, ndmin=1).data,
            zeros_list.data)
        self.assertIs(
            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(
            array_ops.array(zeros_list, copy=False, dtype=int).data,
            zeros_list.data)
        self.assertIsNot(
            array_ops.array(zeros_list.data, copy=False, dtype=int).data,
            zeros_list.data)
        self.assertIs(
            array_ops.array(zeros_list, copy=False, dtype=float).data,
            zeros_list.data)
        self.assertIs(
            array_ops.array(zeros_list.data, copy=False, dtype=float).data,
            zeros_list.data)
Пример #5
0
 def f_prime(a, b):
   shape_dtype = extensions.eval_on_shapes(f)(a, b)
   return array_ops.zeros(shape=shape_dtype.shape, dtype=shape_dtype.dtype)
Пример #6
0
 def zeros(x):
   return tf.nest.map_structure(lambda _: array_ops.zeros([], np.float32), x)