def setUp(self): self.np_1D = np.arange(12).astype(np.int) self.dc_1D = dc.arange(12).asTypeInt() self.np_2D = np.arange(12).reshape(4, 3).astype(np.int) self.dc_2D = dc.arange(12).reshape(4, 3).asTypeInt() self.np_3D = np.arange(48).reshape(4, 3, 4).astype(np.int) self.dc_3D = dc.arange(48).reshape(4, 3, 4).asTypeInt() self.np_4D = np.arange(96).reshape(4, 3, 2, 4).astype(np.int) self.dc_4D = dc.arange(96).reshape(4, 3, 2, 4).asTypeInt()
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_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 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_Slices(self): a = dc.arange(12).reshape([3, 4]).asTypeInt() self.isEqual("0, 0:\n", a.slice(0, 0), a) self.isEqual("0, 1:\n", a.slice(0, 1), dc.array([[4, 5, 6, 7], [8, 9, 10, 11]]).asTypeInt()) self.isEqual("0, 2:\n", a.slice(0, 2), dc.array([[8, 9, 10, 11]]).asTypeInt()) self.isEqual("1, 0:\n", a.slice(1, 0), a) self.isEqual("1, 1:\n", a.slice(1, 1), dc.array([[1, 2, 3], [5, 6, 7], [9, 10, 11]]).asTypeInt()) self.isEqual("1, 2:\n", a.slice(1, 2), dc.array([[2, 3], [6, 7], [10, 11]]).asTypeInt()) self.isEqual("1, 3:\n", a.slice(1, 3), dc.array([[3], [7], [11]]).asTypeInt()) self.isEqual("1, 1, -1, 2:\n", a.slice(1, 1, -1, 2), dc.array([[1, 3], [5, 7], [9, 11]]).asTypeInt()) self.isEqual("1, 0, -1, 3:\n", a.slice(1, 0, -1, 3), dc.array([[0, 3], [4, 7], [8, 11]]).asTypeInt()) b = dc.arange(24).reshape([2, 3, 4]).asTypeInt() self.isEqual("0, 0:\n", b.slice(0, 0), b) self.isEqual( "0, 1:\n", b.slice(0, 1), dc.array([[12, 13, 14, 15], [16, 17, 18, 19], [20, 21, 22, 23]]).asTypeInt()) self.isEqual("1, 0:\n", b.slice(1, 0), b) self.isEqual("1, 1:\n", b.slice(1, 1)[0], dc.array([[4, 5, 6, 7], [8, 9, 10, 11]]).asTypeInt()) self.isEqual( "1, 1:\n", b.slice(1, 1)[1], dc.array([[16, 17, 18, 19], [20, 21, 22, 23]]).asTypeInt()) self.isEqual("2, 0:\n", b.slice(2, 0), b) self.isEqual("2, 1:\n", b.slice(2, 1)[0], dc.array([[1, 2, 3], [5, 6, 7], [9, 10, 11]]).asTypeInt()) self.isEqual( "2, 1:\n", b.slice(2, 1)[1], dc.array([[13, 14, 15], [17, 18, 19], [21, 22, 23]]).asTypeInt()) self.isEqual("2, 2:\n", b.slice(2, 2)[0], dc.array([[2, 3], [6, 7], [10, 11]]).asTypeInt()) self.isEqual("2, 2:\n", b.slice(2, 2)[1], dc.array([[14, 15], [18, 19], [22, 23]]).asTypeInt()) self.isEqual("2, 3:\n", b.slice(2, 3)[0], dc.array([[3], [7], [11]]).asTypeInt()) self.isEqual("2, 3:\n", b.slice(2, 3)[1], dc.array([[15], [19], [23]]).asTypeInt()) self.isEqual("1, 0, -1, 2:\n", b.slice(1, 0, -1, 2)[0], dc.array([[0, 1, 2, 3], [8, 9, 10, 11]]).asTypeInt()) self.isEqual( "1, 0, -1, 2:\n", b.slice(1, 0, -1, 2)[1], dc.array([[12, 13, 14, 15], [20, 21, 22, 23]]).asTypeInt()) self.isEqual("1, 0, -1, 3:\n", b.slice(1, 0, -1, 3)[0], dc.array([[0, 1, 2, 3]]).asTypeInt()) self.isEqual("1, 0, -1, 3:\n", b.slice(1, 0, -1, 3)[1], dc.array([[12, 13, 14, 15]]).asTypeInt())