def test_full_like(self): # scalar like_int = ht.full_like(3, 4) self.assertIsInstance(like_int, ht.tensor) self.assertEqual(like_int.shape, (1, )) self.assertEqual(like_int.lshape, (1, )) self.assertEqual(like_int.split, None) self.assertEqual(like_int.dtype, ht.float32) self.assertTrue(ht.allclose(like_int, ht.float32(4))) # sequence like_str = ht.full_like('abc', 2) self.assertIsInstance(like_str, ht.tensor) self.assertEqual(like_str.shape, (3, )) self.assertEqual(like_str.lshape, (3, )) self.assertEqual(like_str.split, None) self.assertEqual(like_str.dtype, ht.float32) self.assertTrue(ht.allclose(like_str, ht.float32(2))) # elaborate tensor zeros = ht.zeros(( 2, 3, ), dtype=ht.uint8) like_zeros = ht.full_like(zeros, 7) self.assertIsInstance(like_zeros, ht.tensor) self.assertEqual(like_zeros.shape, ( 2, 3, )) self.assertEqual(like_zeros.lshape, ( 2, 3, )) self.assertEqual(like_zeros.split, None) self.assertEqual(like_zeros.dtype, ht.float32) self.assertTrue(ht.allclose(like_zeros, ht.float32(7))) # elaborate tensor with split zeros_split = ht.zeros(( 2, 3, ), dtype=ht.uint8, split=0) like_zeros_split = ht.full_like(zeros_split, 6) self.assertIsInstance(like_zeros_split, ht.tensor) self.assertEqual(like_zeros_split.shape, ( 2, 3, )) self.assertLessEqual(like_zeros_split.lshape[0], 2) self.assertEqual(like_zeros_split.lshape[1], 3) self.assertEqual(like_zeros_split.split, 0) self.assertEqual(like_zeros_split.dtype, ht.float32) self.assertTrue(ht.allclose(like_zeros_split, ht.float32(6))) # exceptions with self.assertRaises(TypeError): ht.ones_like(zeros, dtype='abc') with self.assertRaises(TypeError): ht.ones_like(zeros, split='axis')
def test_full_like(self): # scalar like_int = ht.full_like(3, 4) self.assertIsInstance(like_int, ht.DNDarray) self.assertEqual(like_int.shape, (1, )) self.assertEqual(like_int.lshape, (1, )) self.assertEqual(like_int.split, None) self.assertEqual(like_int.dtype, ht.float32) self.assertTrue(ht.allclose(like_int, ht.float32(4, device=ht_device))) # sequence like_str = ht.full_like("abc", 2) self.assertIsInstance(like_str, ht.DNDarray) self.assertEqual(like_str.shape, (3, )) self.assertEqual(like_str.lshape, (3, )) self.assertEqual(like_str.split, None) self.assertEqual(like_str.dtype, ht.float32) self.assertTrue(ht.allclose(like_str, ht.float32(2, device=ht_device))) # elaborate tensor zeros = ht.zeros((2, 3), dtype=ht.uint8, device=ht_device) like_zeros = ht.full_like(zeros, 7) self.assertIsInstance(like_zeros, ht.DNDarray) self.assertEqual(like_zeros.shape, (2, 3)) self.assertEqual(like_zeros.lshape, (2, 3)) self.assertEqual(like_zeros.split, None) self.assertEqual(like_zeros.dtype, ht.float32) self.assertTrue( ht.allclose(like_zeros, ht.float32(7, device=ht_device))) # elaborate tensor with split zeros_split = ht.zeros((2, 3), dtype=ht.uint8, split=0, device=ht_device) like_zeros_split = ht.full_like(zeros_split, 6) self.assertIsInstance(like_zeros_split, ht.DNDarray) self.assertEqual(like_zeros_split.shape, (2, 3)) self.assertLessEqual(like_zeros_split.lshape[0], 2) self.assertEqual(like_zeros_split.lshape[1], 3) self.assertEqual(like_zeros_split.split, 0) self.assertEqual(like_zeros_split.dtype, ht.float32) self.assertTrue( ht.allclose(like_zeros_split, ht.float32(6, device=ht_device))) # exceptions with self.assertRaises(TypeError): ht.ones_like(zeros, dtype="abc", device=ht_device) with self.assertRaises(TypeError): ht.ones_like(zeros, split="axis", device=ht_device)
def test_ones_like(self): # scalar like_int = ht.ones_like(3, device=ht_device) self.assertIsInstance(like_int, ht.DNDarray) self.assertEqual(like_int.shape, (1, )) self.assertEqual(like_int.lshape, (1, )) self.assertEqual(like_int.split, None) self.assertEqual(like_int.dtype, ht.int32) self.assertEqual((like_int._DNDarray__array == 1).all().item(), 1) # sequence like_str = ht.ones_like("abc", device=ht_device) self.assertIsInstance(like_str, ht.DNDarray) self.assertEqual(like_str.shape, (3, )) self.assertEqual(like_str.lshape, (3, )) self.assertEqual(like_str.split, None) self.assertEqual(like_str.dtype, ht.float32) self.assertEqual((like_str._DNDarray__array == 1).all().item(), 1) # elaborate tensor zeros = ht.zeros((2, 3), dtype=ht.uint8, device=ht_device) like_zeros = ht.ones_like(zeros, device=ht_device) self.assertIsInstance(like_zeros, ht.DNDarray) self.assertEqual(like_zeros.shape, (2, 3)) self.assertEqual(like_zeros.lshape, (2, 3)) self.assertEqual(like_zeros.split, None) self.assertEqual(like_zeros.dtype, ht.uint8) self.assertEqual((like_zeros._DNDarray__array == 1).all().item(), 1) # elaborate tensor with split zeros_split = ht.zeros((2, 3), dtype=ht.uint8, split=0, device=ht_device) like_zeros_split = ht.ones_like(zeros_split, device=ht_device) self.assertIsInstance(like_zeros_split, ht.DNDarray) self.assertEqual(like_zeros_split.shape, (2, 3)) self.assertLessEqual(like_zeros_split.lshape[0], 2) self.assertEqual(like_zeros_split.lshape[1], 3) self.assertEqual(like_zeros_split.split, 0) self.assertEqual(like_zeros_split.dtype, ht.uint8) self.assertEqual((like_zeros_split._DNDarray__array == 1).all().item(), 1) # exceptions with self.assertRaises(TypeError): ht.ones_like(zeros, dtype="abc", device=ht_device) with self.assertRaises(TypeError): ht.ones_like(zeros, split="axis", device=ht_device)