def unified_k3_m3(tensor_trans, tensor_begin): #conteract first 3 dim tensor_trans[9,10,11] tensor_begin[6,7,8] #join tensor_trans[3,4,5,6,7,8] tensor_begin[0,1,2,3,4,5] #result= join[]+tensor_trans[0,1,2] result = np.empty(tensor_begin.shape) shape = tensor_trans.shape for i in range(shape[0]): for j in range(shape[1]): for k in range(shape[2]): for m in range(shape[3]): for n in range(shape[4]): for x in range(shape[5]): for q in range(shape[6]): for w in range(shape[7]): for e in range(shape[8]): result[ i, j, k, m, n, x, q, w, e] = np.sum( tensor_trans.data[i, j, k, m, n, x, q, w, e:, :, :] * tensor_begin.data[m, n, x, q, w, e, :, :, :]) return tensor.tensor(result)
def ctor(verbose): dat = numpy.arange(24).reshape([2,3,4]); t = tensor.tensor(dat); print (t); if (verbose): obj = tenmat.tenmat(t, [1,0]); print (obj); print (obj.copy()); dat = dat.reshape([4,6]); t = tensor.tensor(dat); if (verbose): obj = tenmat.tenmat(t, [0], [1], [4,6]); print (obj);
def totensorTests(verbose): dat = numpy.arange(24).reshape([2,3,4]); t = tensor.tensor(dat); obj = tenmat.tenmat(t,[2,1]); if(verbose): print (obj); print (obj.totensor());
def ctorTests(verbose): arr = numpy.arange(24).reshape([2, 3, 4]) A = numpy.array([[1, 2], [3, 4], [5, 6]]) B = numpy.array([[1, 2, 3], [4, 5, 6]]) C = numpy.array([[1, 2, 3, 4]]) obj = ttensor.ttensor(tensor.tensor(arr), [A, B, C]) print(obj) print(obj.shape)
def tosptensor(verbose): dat = numpy.array( [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 0, 0, 13, 14, 15, 16, 17, 18]) siz = numpy.array([3, 3, 2]) obj = tensor.tensor(dat, siz) if (verbose == 1): print(obj) print(obj.tosptensor())
def test2(): # A = numpy.array([[1, 4, 7, 10], [2, 5, 8, 11], [3, 6, 9, 12]]); # A = numpy.arange(12).reshape([4,3]) + 1; A = numpy.arange(1000).reshape([10,10,10])+1; (ans1, ans2) = DTA.DTA(tensor.tensor(A), [2,2,2]); print (ans1); for i in range(0, len(ans2)): print ("{0} th array\n {1}".format(i, ans2[i])); print (ans1.totensor());
def ttmTest(verbose): dat = numpy.arange(24).reshape([2, 3, 4]) siz = numpy.array([2, 3, 4]) obj = tensor.tensor(dat, siz) A = numpy.arange(18).reshape([6, 3]) print(obj.ttm(A, 1)) B = numpy.arange(12).reshape([3, 4]) print(obj.ttm([A, B], [1, 2])) print("a") print(obj.ttm([A.transpose(), B.transpose()], [1, 2], 't'))
def mathlogicops(verbose): dat = numpy.arange(24).reshape([2, 3, 4]) siz = numpy.array([2, 3, 4]) obj = tensor.tensor(dat, siz) print(obj + 100) print(obj - 100) print(obj + (obj + 10)) print(obj * 1.5) print(obj < 10) print(obj > 10) print(obj == 10) print(obj == obj)
def ctor(verbose): dat = numpy.arange(24) dat[10] = 100 dat[16] = -1 siz = numpy.array([4, 3, 2]) obj = tensor.tensor(dat, siz) if (verbose == 1): print(obj) print(obj.ndims()) obj2 = tensor.tensor(dat.reshape([2, 3, 4]), siz) if (verbose == 1): print(obj) print(obj.shape) dat = numpy.array( [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 0, 0, 13, 14, 15, 16, 17, 18]) obj2 = tensor.tensor(dat) if (verbose == 1): print(obj2) print(obj2.shape)
def permutetest(verbose): dat = numpy.arange(24).reshape([2, 3, 4]) siz = numpy.array([2, 3, 4]) obj = tensor.tensor(dat, siz) if (verbose == 1): print(obj) print("permute by {0}".format("[2,0,1]")) print(obj.permute([2, 0, 1])) print("permute by {0}".format("[1,2,0]")) print(obj.permute([1, 2, 0])) print("ipermuted by {0}".format("[1,2,0]")) print(obj.ipermute([1, 2, 0])) print(obj.permute([1, 2, 0]).ipermute([1, 2, 0]))
def test1(): A = ttensor.ttensor(tensor.tenrands([2,3,4]), [numpy.random.random([10,2]), numpy.random.random([30,3]), numpy.random.random([40,4])]).totensor(); [a,b] = DTA.DTA(A, [1,2,3]); #print a; #print b; Core = numpy.arange(24).reshape([2,3,4]); #Core = numpy.array([[1,3,5],[2,4,6]] , [[7,9,11],[8,10,12]]) u1 = numpy.array([[1,2],[3,4]]); u2 = numpy.array([[0,1,0],[1,0,1],[1,1,1]]); u3 = numpy.array([[1,1,1,1],[1,2,3,4],[1,1,1,1]]); tt = ttensor.ttensor(tensor.tensor(Core), [u1,u2,u3]); print (tt); [a,b] = DTA.DTA(tt.totensor(), [1,2,3]); print (a); print (a.totensor()); print (b);
return np.linalg.norm(tensor_a.data - tensor_b.data) def pow_approch(tensor_trans, tensor_begin, error): resultx = unified_k3_m3(tensor_trans, tensor_begin) #print(resultx) resulty = unified_k3_m3(tensor_trans, resultx) i = 0 while (checkError(resultx, resulty) > error and i < MAX_TIMES): resultx = resulty resulty = unified_k3_m3(tensor_trans, resulty) return resulty if __name__ == '__main__': a = np.array(range(13824 * 2 * 3 * 4), dtype=np.float64).reshape( [2, 3, 4, 2, 3, 4, 2, 3, 4, 2, 3, 4]) b = np.array(range(576 * 24), dtype=np.float64).reshape([2, 3, 4, 2, 3, 4, 2, 3, 4]) tensor_trans = tensor.tensor(a) tensor_begin = tensor.tensor(b) #print(tensor_begin) print('tensor_tran') print(tensor_trans.data[0, 0, 0, 0, 0, 0, 0, 0, 0, :, :, :]) print('tensor_begin') print(tensor_begin.data[0, 0, 0, 0, 0, 0, :, :, :]) result = unified_k3_m3(tensor_trans, tensor_begin) print(result.data[0, 0, 0, 0, 0, 0, 0, 0, 0]) #error=10