def testEmpty(self): for dtype in [ dtypes.float32, dtypes.float64, dtypes.int32, dtypes.int64, dtypes.bool ]: with self.test_session(use_gpu=True): test_shapes = [(), (1, ), (2, 3), (0, 2), (2, 3, 5), (2, 0, 5)] for shape in test_shapes: val = inplace_ops.empty(shape, dtype).eval() self.assertEqual(val.shape, shape) self.assertEqual(val.dtype, dtype.as_numpy_dtype) val = inplace_ops.empty(shape, dtype, init=True).eval() self.assertEqual(val.shape, shape) self.assertEqual(val.dtype, dtype.as_numpy_dtype) self.assertAllEqual(val, np.zeros(shape, dtype.as_numpy_dtype)) val = inplace_ops.empty_like(array_ops.zeros( shape, dtype)).eval() self.assertEqual(val.shape, shape) self.assertEqual(val.dtype, dtype.as_numpy_dtype) val = inplace_ops.empty_like(array_ops.zeros(shape, dtype), init=True).eval() self.assertEqual(val.shape, shape) self.assertEqual(val.dtype, dtype.as_numpy_dtype) self.assertAllEqual(val, np.zeros(shape, dtype.as_numpy_dtype)) val = inplace_ops.empty((1, 2), dtypes.string, init=True).eval() self.assertEqual(val.tolist(), [[b"", b""]]) val = inplace_ops.empty((1, 2), dtypes.string, init=False).eval() self.assertEqual(val.tolist(), [[b"", b""]])
def testEmpty(self): for dtype in [ dtypes.float32, dtypes.float64, dtypes.int32, dtypes.int64, dtypes.bool, dtypes.uint8 ]: with self.test_session(use_gpu=True): test_shapes = [(), (1,), (2, 3), (0, 2), (2, 3, 5), (2, 0, 5)] for shape in test_shapes: val = inplace_ops.empty(shape, dtype).eval() self.assertEqual(val.shape, shape) self.assertEqual(val.dtype, dtype.as_numpy_dtype) val = inplace_ops.empty(shape, dtype, init=True).eval() self.assertEqual(val.shape, shape) self.assertEqual(val.dtype, dtype.as_numpy_dtype) self.assertAllEqual(val, np.zeros(shape, dtype.as_numpy_dtype)) val = inplace_ops.empty_like(array_ops.zeros(shape, dtype)).eval() self.assertEqual(val.shape, shape) self.assertEqual(val.dtype, dtype.as_numpy_dtype) val = inplace_ops.empty_like( array_ops.zeros(shape, dtype), init=True).eval() self.assertEqual(val.shape, shape) self.assertEqual(val.dtype, dtype.as_numpy_dtype) self.assertAllEqual(val, np.zeros(shape, dtype.as_numpy_dtype)) with self.test_session(use_gpu=True): val = inplace_ops.empty((1, 2), dtypes.string, init=True).eval() self.assertEqual(val.tolist(), [[b"", b""]]) val = inplace_ops.empty((1, 2), dtypes.string, init=False).eval() self.assertEqual(val.tolist(), [[b"", b""]])
def _EmptyLike(nmap): """Creates a set of empty initialized tensors. Args: nmap: A `.NestedMap` of tensors. Returns: A `.NestedMap` of tensors. Each tensor has the same shape and dtype as its corresponding tensor in nmap. And each tensor is initialized. """ return nmap.Transform(lambda x: inplace_ops.empty_like(x, init=True))
def _EmptyLike(struct): """Creates a set of empty initialized tensors. Args: struct: A structure of tensors. Returns: A struct of tensors. Each tensor has the same shape and dtype as its corresponding tensor in `struct`. And each tensor is initialized. """ return nest.map_structure( lambda x: inplace_ops.empty_like(x, init=True), struct)