def test_data(self): # confirm type as class tuple. a = dc.zeros(2, 3).asTypeInt() adata = a.data() assert type(adata) == type((1, )) # load new data new_data_list = [10, 11, 12, 13, 14, 15] a.load(dc.vectorInt(new_data_list)) assert a[0] == 10 # load one element with flat index a[0] = 777 assert a[0] == 777 # reshape, fetch and load with multi indices a = dc.arange(12).asTypeInt() a.reshape(dc.vectorSizeT([2, 2, 3])) assert a[0, 1, 1] == 4 a[1, 1, 1] = 200 assert a[1, 1, 1] == 200 # negative test try: # This throws ValueError print(a[0, 0, 9, 9, 9]) except ValueError as e: assert e
def setUp(self): self.nullT = dc.array(0) self.zeros = dc.zeros(2, 3).asTypeInt() self.ones = dc.ones(2, 3).asTypeInt() self.f0_4 = dc.arange(5) self.f5_9 = dc.arange(10, 5) self.np_f0_4 = np.arange(5) self.np_f5_9 = np.arange(10, 5) self.i0_4 = self.f0_4.asTypeInt() self.i5_9 = self.f5_9.asTypeInt() self.np_i0_4 = self.np_f0_4.astype(np.int) self.np_i5_9 = self.np_f5_9.astype(np.int) self.b0_4 = self.f0_4.asTypeBool() self.b5_9 = self.f5_9.asTypeBool()
def test_non_binary(): t1 = dc.array(2, 3) t2 = dc.array(3, 2) mul = dc.matmul(t1, t2) #print ("multiplication : " , mul.to_string()) t3 = dc.array(2, 3, 4) #print("old shape", t1.shape()) new_shape = dc.vectorSizeT([2, 12]) t3.reshape(new_shape) t3.reshape(4, 6) t3.reshape((4, 6)) t3.reshape([4, 6]) #print("new shape", t1.shape()) py_list = list(t3) # convert tensor to python list py_tuple = tuple(t3) # convert tensor to python tuple np_ary = t3.numpy() # convert to numpy array #t4 = dc.thresholded_relu(t1); #print("relu", t4.to_string()) #replace first few values in tensor with new values. data = dc.vectorFloat([1.0, 2.0, 3.0, 4.0]) t3.load(data) #print(t3.to_string()) arr = dc.array([1, 2]) #print(arr) arr2D = dc.array([[1, 2], [10, 20]]).asTypeInt() #print(arr2D) arrRand = dc.random(2, 3) #print(arrRand) empty = dc.empty(3, 2) #print(empty) zeros = dc.zeros(2, 2) #print(zeros); ones = dc.ones(2, 2) #print(ones) ranges = dc.arange(15, 3, 2) #print(ranges) dc.reshape(arr2D, (1, 4)) #3D MatMul Test1 a = dc.array(2, 2, 2) b = dc.array(2, 2, 2) adata = dc.vectorFloat([1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0]) bdata = dc.vectorFloat([8.0, 7.0, 6.0, 5.0, 4.0, 3.0, 2.0, 1.0]) a.load(adata) b.load(bdata) test_multiply(a, b) #3D MatMul Test2 a = dc.array(2, 2, 3) b = dc.array(2, 3, 2) adata = dc.vectorFloat( [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0]) bdata = dc.vectorFloat( [12.0, 11.0, 10.0, 9.0, 8.0, 7.0, 6.0, 5.0, 4.0, 3.0, 2.0, 1.0]) a.load(adata) b.load(bdata) test_multiply(a, b)
def test_create(self): # null tensor test a = dc.array(0) assert a.isnull() == True assert a.empty() == True # test assignment is shallow copy of memory b = a assert a.sameas(b) == True assert a.identifier() == b.identifier() # tensor without initiliaztion a = dc.array(2, 3, 4, 5) assert a.length() == 120 # tensor random initiliaztion a = dc.random(2, 3, 4, 5) assert a.length() == 120 # tensor without initiliaztion a = dc.empty(2, 3, 4, 5) assert a.length() == 120 # zero tensor a = dc.zeros(2, 3, 4, 5) assert np.array(list(a.data())).sum().astype(np.int) == 0 # one tensor a = dc.ones(2, 3, 4, 5) assert np.array(list(a.data())).sum().astype(np.int) == 120 # tensor from python list l1D = [1, 3, 5] a = dc.array(l1D).asTypeInt() np.testing.assert_equal(np.array(l1D), np.array(list(a.data()))) # tensor from python list of lists l2D = [[1, 3, 5], [2, 4, 6]] a = dc.array(l2D).asTypeInt() assert a.rank() == 2 assert a.shape() == (2, 3) np.testing.assert_equal(np.array(l2D).flatten(), \ np.array(list(a.data()))) # copy tensor b = a.copy() assert a.sameas(b) == False assert a.identifier() != b.identifier() # arange a = dc.arange(10) assert a.length() == 10 # add start and step a = dc.arange(10, 5, 3).asTypeInt() assert a.data() == (5, 8) # swap start and stop. a = dc.arange(5, 10, 3).asTypeInt() assert a.data() == (5, 8)
def test_assignments(self): assert not self.nullT # The truth value of an array with more than one element is ambiguous. Use dc.any() or dc.all() # assert self.zeros # assert self.ones # Add temp_zeros = self.zeros.copy() temp_zeros += self.ones dnnc_testing.utils.assert_equal(temp_zeros, self.ones) temp_zeros = self.zeros.copy() temp_zeros += 1 dnnc_testing.utils.assert_equal(temp_zeros, self.ones) # Sub temp_ones = self.ones.copy() temp_ones -= self.ones dnnc_testing.utils.assert_equal(temp_ones, self.zeros) temp = self.f5_9 temp -= dc.array([5]) dnnc_testing.utils.assert_allclose(temp, self.f0_4) temp = self.i5_9 temp -= dc.array([5]).asTypeInt() dnnc_testing.utils.assert_equal(temp, self.i0_4) temp = self.b5_9 temp -= dc.array([5]).asTypeBool() dnnc_testing.utils.assert_equal(temp, dc.zeros(5).asTypeBool()) # Mul temp_zeros = self.zeros.copy() temp_zeros *= self.ones dnnc_testing.utils.assert_equal(temp_zeros, self.zeros) temp_ones = self.ones.copy() temp_ones *= 0 dnnc_testing.utils.assert_equal(temp_ones, self.zeros) # TrueDiv temp_zeros = self.zeros.copy().asTypeFloat() temp_zeros /= self.ones.asTypeFloat() dnnc_testing.utils.assert_equal(temp_zeros.asTypeFloat(), self.zeros.asTypeFloat()) temp_zeros = self.zeros.copy() temp_zeros /= 1 dnnc_testing.utils.assert_equal(temp_zeros.asTypeFloat(), self.zeros.asTypeFloat()) # FloorDiv temp_zeros = self.zeros.copy() temp_zeros //= self.ones dnnc_testing.utils.assert_equal(temp_zeros, self.zeros) temp_zeros = self.zeros.copy() temp_zeros //= 1 dnnc_testing.utils.assert_equal(temp_zeros, self.zeros) # Pow temp_zeros = self.zeros.copy() temp_zeros **= self.ones dnnc_testing.utils.assert_equal(temp_zeros, self.zeros) temp_ones = self.ones.copy() temp_ones **= 0 dnnc_testing.utils.assert_equal(temp_ones, self.ones) # Mod temp_zeros = self.zeros.copy() temp_zeros %= self.ones dnnc_testing.utils.assert_equal(temp_zeros, self.zeros) temp_zeros = self.zeros.copy() temp_zeros %= 1 dnnc_testing.utils.assert_equal(temp_zeros, self.zeros) # Left Shift temp_zeros = self.zeros.copy() temp_zeros <<= self.ones dnnc_testing.utils.assert_equal(temp_zeros, self.zeros << self.ones) temp_ones = self.ones.copy() temp_ones <<= 0 dnnc_testing.utils.assert_equal(temp_ones, self.ones << 0) # Right Shift temp_zeros = self.zeros.copy() temp_zeros <<= self.ones dnnc_testing.utils.assert_equal(temp_zeros, self.zeros >> self.ones) temp_ones = self.ones.copy() temp_ones <<= 0 dnnc_testing.utils.assert_equal(temp_ones, self.ones >> 0) # And temp_zeros = self.zeros.copy() temp_zeros &= self.ones dnnc_testing.utils.assert_equal(temp_zeros, self.zeros) temp_ones = self.ones.copy() temp_ones &= 0 dnnc_testing.utils.assert_equal(temp_ones, self.zeros) # Or temp_zeros = self.zeros.copy() temp_zeros |= self.ones dnnc_testing.utils.assert_equal(temp_zeros, self.ones) temp_ones = self.ones.copy() temp_ones |= 0 dnnc_testing.utils.assert_equal(temp_ones, self.ones) # Xor temp_zeros = self.zeros.copy() temp_zeros ^= self.ones dnnc_testing.utils.assert_equal(temp_zeros, self.ones) temp_ones = self.ones.copy() temp_ones ^= 1 dnnc_testing.utils.assert_equal(temp_ones, self.zeros)
def setUp(self): ## random testcase self.channels = 1 self.featuremaps = 1 self.batchsize = 1 #self.oneK = 1024 self.oneK = 50 self.X_h = self.oneK + np.random.randint(self.oneK * 3) self.X_w = self.oneK + np.random.randint(self.oneK * 3) self.K_h = 3 + np.random.randint(97) self.K_w = 3 + np.random.randint(97) self.np_strides = np.zeros(2).astype(np.float32) self.np_strides[0] = 1 + np.random.randint(self.K_w - 1) self.np_strides[1] = 1 + np.random.randint(self.K_h - 1) self.np_B = np.zeros(self.featuremaps).astype(np.float32) self.np_X_data = np.random.randn(self.X_w * self.X_h).astype( np.float32) self.np_K_data = np.random.randn(self.K_w * self.K_h).astype( np.float32) self.np_X = np.reshape(self.np_X_data, (self.X_h, self.X_w)) self.np_K = np.reshape(self.np_K_data, (self.K_h, self.K_w)) self.dc_X = dc.reshape( dc.array(list(self.np_X_data)), (self.batchsize, self.channels, self.X_h, self.X_w)).asTypeFloat() self.dc_K = dc.reshape(dc.array(list(self.np_K_data)), (self.featuremaps, self.channels, self.K_h, self.K_w)).asTypeFloat() self.dc_B = dc.zeros(self.featuremaps).asTypeFloat() self.dc_strides = dc.reshape(dc.array(list(self.np_strides)), (2)).asTypeInt() self.dc_nullT = dc.array(0) ## onnx conv example testcase self.onnx_dc_X = dc.reshape( dc.array([ 0., 1., 2., 3., 4., 5., 6., 7., 8., 9., 10., 11., 12., 13., 14., 15., 16., 17., 18., 19., 20., 21., 22., 23., 24. ]), (1, 1, 5, 5)) self.onnx_dc_X2 = dc.reshape( dc.array([ 0., 1., 2., 3., 4., 5., 6., 7., 8., 9., 10., 11., 12., 13., 14., 15., 16., 17., 18., 19., 20., 21., 22., 23., 24., 25., 26., 27., 28., 29., 30., 31., 32., 33., 34. ]), (1, 1, 7, 5)) self.onnx_dc_W = dc.reshape( dc.array([1., 1., 1., 1., 1., 1., 1., 1., 1.]), (1, 1, 3, 3)) self.onnx_npr_su = np.array([ 12., 21., 27., 33., 24., 33., 54., 63., 72., 51., 63., 99., 108., 117., 81., 93., 144., 153., 162., 111., 72., 111., 117., 123., 84. ]) self.onnx_npr_vl = np.array( [54., 63., 72., 99., 108., 117., 144., 153., 162.]) self.onnx_npr_vl_s2 = np.array([54., 72., 144., 162., 234., 252.]) self.onnx_npr_sp_s2 = np.array([ 12., 27., 24., 63., 108., 81., 123., 198., 141., 112., 177., 124. ]) self.onnx_npr_ap_s2 = np.array( [21., 33., 99., 117., 189., 207., 171., 183.]) self.onnx_dc_BIGW = dc.reshape(dc.array(list(np.ones(900))), (1, 1, 30, 30))