def test_shapes(self): # test shape tuple shape1 = dc.vectorSizeT([2, 3, 4, 5]) shape2 = dc.vectorSizeT([5, 4, 3, 2]) a = dc.random(2, 3, 4, 5).asTypeInt() assert a.rank() == 4 assert a.shape() == (2, 3, 4, 5) # reshape to new dimensions a.reshape(shape2) assert a.shape() == (5, 4, 3, 2) # return a new tensor with flattened dimensions. b = a.flatten() assert a.shape() == (5, 4, 3, 2) assert b.shape() == (120, ) # flatten the same tensor a.flatteninplace() assert a.shape() == (120, ) shape3 = dc.vectorSizeT([8, 15, 1, 1]) # new shape a.reshape(shape3) # confirm new shape assert a.shape() == (8, 15, 1, 1) # dnnc method to reshape. a = dc.random(2, 3, 4, 5) dc.reshape(a, (8, 15, 1, 1)) assert a.shape() == (8, 15, 1, 1) dc.reshape(a, (120, )) assert a.shape() == (120, )
def test_dtypes(self): a = dc.random(2, 3) assert a.dtype() == 'float' # transform datatype to int. aint = a.asTypeInt() assert aint.dtype() == 'int32_t' # transform datatype to double. adbl = a.asTypeDouble() assert adbl.dtype() == 'double' # transform datatype to double. abool = a.asTypeBool() assert abool.dtype() == 'bool'
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 __init__(self, n): self.N = n self.dc_a = dc.random(n,n); self.dc_b = dc.random(n,n);