def test_3d_purturb3(self): I = random.randint(3,5) #random dimensions J = random.randint(3,5) K = random.randint(3,5) r = 2 regParam = 0 sparsity = .2 ctf.random.seed(10) # generate factor matrices U = ctf.random.random((I,r)) V= ctf.random.random((J,r)) W= ctf.random.random((K,r)) T = ctf.tensor((I,J,K)) T.i("ijk") << U.i("iu")*V.i("ju")*W.i("ku") omega = updateOmega(I,J,K,sparsity) # purturb the first and second factor matrix #U += crandom.random((I,r))*.01 W += crandom.random((K,r))*.01 # call updateU function nW = updateW(T,U,V,W,regParam,omega,I,J,K,r) #nU = updateU(T,U,V,W,regParam,omega,I,J,K,r) #nV = updateV(T,U,V,W,regParam,omega,I,J,K,r) nT = ctf.tensor((I,J,K)) nT.i("ijk") << U.i("iu")*V.i("ju")*nW.i("ku") assert(ctf.all(ctf.abs(nT - T < 1e-10))) print("passed test: test_3d_purturb3")
def test_Solve_Factor_mat(self): R = 10 for N in range(3, 6): mats = [] num = numpy.random.randint(N) lens = numpy.random.randint(10, 20, N) regu = 1e-04 for i in range(N): if i != num: mats.append(ctf.random.random([lens[i], R])) else: mats.append(ctf.tensor([lens[i], R])) RHS = ctf.random.random([lens[num], R]) A = ctf.tensor(lens, sp=1) A.fill_sp_random(1., 1., 0.5) lst_mat = [] T_inds = "".join([chr(ord('a') + i) for i in range(A.ndim)]) einstr = "" for i in range(N): if i != num: einstr += chr(ord('a') + i) + 'r' + ',' lst_mat.append(mats[i].to_nparray()) einstr += chr(ord('a') + i) + 'z' + ',' lst_mat.append(mats[i].to_nparray()) einstr += T_inds + "->" + chr(ord('a') + num) + 'rz' lst_mat.append(A.to_nparray()) lhs_np = numpy.einsum(einstr, *lst_mat, optimize=True) rhs_np = RHS.to_nparray() ans = numpy.zeros_like(rhs_np) for i in range(mats[num].shape[0]): ans[i, :] = la.solve(lhs_np[i] + regu * np.eye(R), rhs_np[i, :]) ctf.Solve_Factor(A, mats, RHS, num, regu) self.assertTrue(numpy.allclose(ans, mats[num].to_nparray()))
def sparse_update(T, factors, Lambda, sizes, rank, stepSize, sample_rate, times): starting_time = time.time() t_go = ctf.timer("SGD_getOmega") t_go.start() omega = getOmega(T) t_go.stop() dimension = len(sizes) indexes = INDEX_STRING[:dimension] R = ctf.tensor(copy=T) #ctf.tensor(tuple(sizes), sp = True) times[2] += time.time() - starting_time for i in range(dimension): tup_list = [factors[i].i(indexes[i] + "r") for i in range(dimension)] #R.i(indexes) << T.i(indexes) - omega.i(indexes) * reduce(lambda x, y: x * y, tup_list) starting_time = time.time() R.i(indexes) << -1. * ctf.TTTP(omega, factors).i(indexes) times[3] += time.time() - starting_time starting_time = time.time() #H = ctf.tensor(tuple((sizes[:i] + sizes[i + 1:] + [rank]))) times[4] += time.time() - starting_time starting_time = time.time() #H.i(indexes[:i] + indexes[i + 1:] + "r") << Hterm = reduce(lambda x, y: x * y, tup_list[:i] + tup_list[i + 1:]) times[5] += time.time() - starting_time starting_time = time.time() t_ctr = ctf.timer("SGD_main_contraction") t_ctr.start() (1 - stepSize * 2 * Lambda * sample_rate ) * factors[i].i(indexes[i] + "r") << stepSize * Hterm * R.i(indexes) t_ctr.stop() times[6] += time.time() - starting_time if i < dimension - 1: R = ctf.tensor(copy=T)
def sparse_update(T, factors, Lambda, sizes, rank, stepSize, sample_rate, times, use_MTTKRP): starting_time = time.time() t_go = ctf.timer("SGD_getOmega") t_go.start() omega = getOmega(T) t_go.stop() dimension = len(sizes) indexes = INDEX_STRING[:dimension] R = ctf.tensor(copy=T) #ctf.tensor(tuple(sizes), sp = True) times[2] += time.time() - starting_time for i in range(dimension): starting_time = time.time() R.i(indexes) << -1.* ctf.TTTP(omega, factors).i(indexes) times[3] += time.time() - starting_time starting_time = time.time() times[4] += time.time() - starting_time starting_time = time.time() times[5] += time.time() - starting_time starting_time = time.time() t_ctr = ctf.timer("SGD_main_contraction") t_ctr.start() if use_MTTKRP: new_fi = (1- stepSize * 2 * Lambda * sample_rate)*factors[i] ctf.MTTKRP(R, factors, i) stepSize*factors[i].i("ir") << new_fi.i("ir") else: tup_list = [factors[i].i(indexes[i] + "r") for i in range(dimension)] Hterm = reduce(lambda x, y: x * y, tup_list[:i] + tup_list[i + 1:]) (1- stepSize * 2 * Lambda * sample_rate)*factors[i].i(indexes[i] + "r") << stepSize * Hterm * R.i(indexes) t_ctr.stop() times[6] += time.time() - starting_time if i < dimension - 1: R = ctf.tensor(copy=T)
def function_tensor(I, J, K, sparsity): # N = 5 # n = 51 # L = 100 # nsample = 10*N*n*L #10nNL = 255000 T = ctf.tensor((I, J, K), sp=True) T2 = ctf.tensor((I, J, K), sp=True) T.fill_sp_random(1, 1, sparsity) # T = ctf.exp(-1 * ctf.power(ctf.power(T,2),0.5)) # x = exp(-sqrt(x^2)) sizes = [I, J, K] index = ["i", "j", "k"] for i in range(3): n = sizes[i] v = np.linspace(-1, 1, n) # v = np.arange(1,n+1) v = ctf.astensor(v**2) v2 = ctf.tensor(n, sp=True) v2 = v T2.i("ijk") << T.i("ijk") * v2.i(index[i]) # T2 = ctf.power(T2, 0.5) [inds, data] = T2.read_local_nnz() data[:] **= .5 data[:] *= -1. T2 = ctf.tensor(T2.shape, sp=True) T2.write(inds, data) return T2
def read_frostt_tensor(file_name, I, J, K, use_sp_rep): unzipped_file_name = file_name + '.tns' exists = os.path.isfile(unzipped_file_name) if not exists: if glob_comm.rank() == 0: print('Creating ' + unzipped_file_name) with gzip.open(file_name + '.tns.gz', 'r') as f_in: with open(unzipped_file_name, 'w') as f_out: shutil.copyfileobj(f_in, f_out) T_start = ctf.tensor((I + 1, J + 1, K + 1), sp=use_sp_rep) if glob_comm.rank() == 0: print('T_start initialized') T_start.read_from_file(unzipped_file_name) if glob_comm.rank() == 0: print('T_start read from file') T = ctf.tensor((I, J, K), sp=use_sp_rep) if glob_comm.rank() == 0: print('T initialized') T[:, :, :] = T_start[1:, 1:, 1:] if glob_comm.rank() == 0: print('T filled by shifting from T_start') #T.write_to_file(unzipped_file_name) return T
def function_tensor(I, J, K, sparsity): # N = 5 # n = 51 # L = 100 # nsample = 10*N*n*L #10nNL = 255000 T = ctf.tensor((I, J, K)) T2 = ctf.tensor((I, J, K)) T.fill_sp_random(1, 1, sparsity) # T = ctf.exp(-1 * ctf.power(ctf.power(T,2),0.5)) # x = exp(-sqrt(x^2)) sizes = [I, J, K] index = ["i", "j", "k"] for i in range(3): n = sizes[i] v = np.linspace(-1, 1, n) # v = np.arange(1,n+1) v = ctf.astensor(v**2) v2 = ctf.tensor(n) v2 = v T2.i("ijk") << T.i("ijk") * v2.i(index[i]) T2 = ctf.power(T2, 0.5) T2 = (-1.0) * T2 # T2 = ctf.exp(T2) return T2
def test_sp_reshape(self): a1 = ctf.tensor((2, 3), sp=True) a1.fill_sp_random(0., 1., .5) a0 = a1.to_nparray() self.assertTrue(ctf.all(ctf.reshape(a1, (2, 3)) == a0.reshape(2, 3))) a1 = ctf.tensor((2, 3, 4, 5), sp=True) a1.fill_sp_random(0., 1., .5) a0 = a1.to_nparray() self.assertTrue( ctf.all(ctf.reshape(a1, (2, 3, 4, 5)) == a0.reshape(2, 3, 4, 5))) self.assertTrue(ctf.all(ctf.reshape(a1, (6, 20)) == a0.reshape(6, 20))) self.assertTrue( ctf.all(ctf.reshape(a1, (6, 5, 4)) == a0.reshape(6, 5, 4))) self.assertTrue( ctf.all(ctf.reshape(a1, (3, 10, 4)) == a0.reshape(3, 10, 4))) self.assertTrue(ctf.all(ctf.reshape(a1, (6, -1)) == a0.reshape(6, -1))) self.assertTrue( ctf.all(ctf.reshape(a1, (-1, 20)) == a0.reshape(-1, 20))) self.assertTrue( ctf.all(ctf.reshape(a1, (6, -1, 4)) == a0.reshape(6, -1, 4))) self.assertTrue( ctf.all(ctf.reshape(a1, (3, -1, 2)) == a0.reshape(3, -1, 2))) self.assertTrue(ctf.all(a1.reshape(6, 20) == a0.reshape(6, 20))) self.assertTrue(ctf.all(a1.reshape(6, 5, 4) == a0.reshape(6, 5, 4))) self.assertTrue( ctf.all(a1.reshape((3, 10, 4)) == a0.reshape(3, 10, 4))) self.assertTrue(ctf.all(a1.reshape((6, -1)) == a0.reshape(6, -1))) self.assertTrue(ctf.all(a1.reshape(-1, 20) == a0.reshape(-1, 20))) self.assertTrue(ctf.all(a1.reshape(6, -1, 4) == a0.reshape(6, -1, 4))) self.assertTrue( ctf.all(a1.reshape((3, -1, 2)) == a0.reshape(3, -1, 2))) with self.assertRaises(ValueError): a1.reshape((1, 2))
def normalize(Z,r): norms = ctf.tensor(r) norms.i("u") << Z.i("pu")*Z.i("pu") norms = 1./norms**.5 X = ctf.tensor(copy=Z) Z.set_zero() Z.i("pu") << X.i("pu")*norms.i("u") return 1./norms
def test_hypersparse_dot(self): A = ctf.tensor((37, 513), sp=True) A.fill_sp_random(0., 1., 1. / 2047) B = ctf.tensor((513, 17)) C = ctf.tensor((37, 17), sp=True) C.i("ij") << A.i("ik") * B.i("kj") C2 = ctf.tensor((37, 17)) C2 = ctf.dot(A, B) self.assertTrue(allclose(C, C2))
def main(): size_lb = 40 size_ub = 60 r = 20 sparsity = .1 regParam = 0.1 stepSize = 0.0001 sample_rate = 0.0001 I = random.randint(size_lb, size_ub) J = random.randint(size_lb, size_ub) K = random.randint(size_lb, size_ub) target_tensor = ctf.tensor((I, J, K)) target_tensor.fill_random(0, 1) ctf.random.seed(42) target_U = ctf.random.random((I, r)) target_V = ctf.random.random((J, r)) target_W = ctf.random.random((K, r)) dense_SGD(target_tensor, target_U, target_V, target_W, regParam, I, J, K, r, stepSize, sample_rate) #T = ctf.tensor((I,J,K),sp=True) T = ctf.tensor((I, J, K)) T.i("ijk") << target_U.i("iu") * target_V.i("ju") * target_W.i("ku") #T.fill_sp_random(0,1,sparsity) rand = ctf.tensor((I, J, K)) rand.fill_random(0, 1) rand = ((rand < sparsity) * ctf.astensor(1.)) T = T * rand omega = getOmega(T) U = ctf.random.random((I, r)) V = ctf.random.random((J, r)) W = ctf.random.random((K, r)) stepSize = 0.0005 t = time.time() #sparse_GD(T,target_U,target_V,target_W,regParam,omega,I,J,K,r,stepSize) plt.plot(sparse_GD(T, U, V, W, regParam, omega, I, J, K, r, stepSize), label="GD") print("GD total time = ", np.round_(time.time() - t, 4)) U = ctf.random.random((I, r)) V = ctf.random.random((J, r)) W = ctf.random.random((K, r)) t = time.time() plt.plot(sparse_SGD(T, U, V, W, regParam, omega, I, J, K, r, stepSize, sample_rate), label="SGD") plt.legend() plt.title("tensor dimension: " + str(I) + "," + str(J) + "," + str(K) + " rank: " + str(r) + " sparsity: " + str(sparsity)) plt.show() print("SGD total time = ", np.round_(time.time() - t, 4))
def sparse_SGD(T, U, V, W, Lambda, omega, I, J, K, r, stepSize, sample_rate): iteration_count = 0 E = ctf.tensor((I, J, K)) R = ctf.tensor((I, J, K)) R.i("ijk" ) << T.i("ijk") - omega.i("ijk") * U.i("iu") * V.i("ju") * W.i("ku") sampled_T = ctf.tensor((I, J, K)) curr_err_norm = ctf.vecnorm(R) + (ctf.vecnorm(U) + ctf.vecnorm(V) + ctf.vecnorm(W)) * Lambda norm = [curr_err_norm] while True: random = ctf.tensor((I, J, K)) random.fill_random(0, 1) random = ((random > sample_rate) * ctf.astensor(1.)) sampled_T = T * random #sampled_T.sample(sample_rate) sampled_omega = getOmega(sampled_T) E.i("ijk") << sampled_T.i( "ijk") - sampled_omega.i("ijk") * U.i("iu") * V.i("ju") * W.i("ku") U = sparse_updateU(sampled_T, U, V, W, Lambda, sampled_omega, I, J, K, r, E, stepSize) E.set_zero() E.i("ijk") << sampled_T.i( "ijk") - sampled_omega.i("ijk") * U.i("iu") * V.i("ju") * W.i("ku") V = sparse_updateV(sampled_T, U, V, W, Lambda, sampled_omega, I, J, K, r, E, stepSize) E.set_zero() E.i("ijk") << sampled_T.i( "ijk") - sampled_omega.i("ijk") * U.i("iu") * V.i("ju") * W.i("ku") W = sparse_updateW(sampled_T, U, V, W, Lambda, sampled_omega, I, J, K, r, E, stepSize) E.set_zero() sampled_T.set_zero() if iteration_count % 5 == 0: R.set_zero() R.i("ijk") << T.i( "ijk") - omega.i("ijk") * U.i("iu") * V.i("ju") * W.i("ku") diff_norm = ctf.vecnorm(R) next_err_norm = diff_norm + (ctf.vecnorm(U) + ctf.vecnorm(V) + ctf.vecnorm(W)) * Lambda print(curr_err_norm, next_err_norm) print(diff_norm) if abs(curr_err_norm - next_err_norm) < .01 or iteration_count > 100: break curr_err_norm = next_err_norm norm.append(curr_err_norm) iteration_count += 1 print("Number of iterations: ", iteration_count) return norm
def CG(Z,Tbar,r,regParam): x0 = ctf.random.random(r) Ax0 = ctf.tensor((r)) Ax0.i("i") << Z.i("ti") * Z.i("tj") * x0.i("j") # LHS; ATA using matrix-vector multiplication Ax0 += regParam*x0 b = ctf.tensor((r)) b.i("r") << Z.i("tr") * Tbar.i("t") #b = ctf.dot(Z.transpose(),Tbar) # RHS; ATb print("b",b) return LS_CG(Ax0,b,Z,x0,r,regParam)
def updateFactor_SVD(T, U, V, W, regParam, omega, I, J, K, r, string): if (string == "U"): M1 = ctf.tensor((J, K, r)) M1.i("jku") << V.i("ju") * W.i("ku") for i in range(I): num_nonzero, dense_omega = getDenseOmega(T, U, V, W, regParam, omega, I, J, K, r, i, "i") Z = ctf.tensor((num_nonzero, r)) Z.i("tr") << dense_omega.i("jkt") * M1.i("jkr") Tbar = ctf.tensor((num_nonzero)) Tbar.i("t") << dense_omega.i("jkt") * T[i, :, :].i("jk") U[i, :].set_zero() U[i, :] = LS_SVD(Z, U[i, :], r, Tbar, regParam) return U if (string == 'V'): M2 = ctf.tensor((I, K, r)) M2.i("iku") << U.i("iu") * W.i("ku") for j in range(J): num_nonzero, dense_omega = getDenseOmega(T, U, V, W, regParam, omega, I, J, K, r, j, "j") Z = ctf.tensor((num_nonzero, r)) Z.i("tr") << dense_omega.i("ikt") * M2.i("ikr") Tbar = ctf.tensor((num_nonzero)) Tbar.i("t") << dense_omega.i("ikt") * T[:, j, :].i("ik") V[j, :].set_zero() V[j, :] = LS_SVD(Z, V[j, :], r, Tbar, regParam) return V if (string == 'W'): M3 = ctf.tensor((I, J, r)) M3.i("iju") << U.i("iu") * V.i("ju") for k in range(K): num_nonzero, dense_omega = getDenseOmega(T, U, V, W, regParam, omega, I, J, K, r, k, "k") Z = ctf.tensor((num_nonzero, r)) Z.i("tr") << dense_omega.i("ijt") * M3.i("ijr") Tbar = ctf.tensor((num_nonzero)) Tbar.i("t") << dense_omega.i("ijt") * T[:, :, k].i("ij") W[k, :].set_zero() W[k, :] = LS_SVD(Z, W[k, :], r, Tbar, regParam) return W
def main(): """Starts the program""" ut = UnitTests() ut.runAllTests() m = 40 n = 30 k = 8 sparsity = .2 learningRate = .1 regParamALS = 2 regParamSGD = .1 width = 1 #how many random i,j we're going to get convNo = .00002 # When we'll stop converging X = getDenseMtx(m, k, 0, 1) Y = getDenseMtx(n, k, 0, 1) A = ctf.tensor((m, n)) B = ctf.tensor((m, n), sp=True) B.fill_sp_random(1, 1, 1) A = (X @ Y.T()) X += crandom.random((m, k)) * .001 Y += crandom.random((n, k)) * .001 H = getDenseMtx(n, k, 0, 1) W = getDenseMtx(m, k, 0, 1) omega = updateOmega(m, n, sparsity) ''' t = time.time() getALSCtf(A,X,Y,regParamALS,omega,m,n,k) print("Done with ALS! Time = ",np.round_(time.time()-t,4)) ''' t = time.time() #getSGDCtf(A,X,Y,learningRate,regParamSGD,width,convNo) dense_time = np.round_(time.time() - t, 4) print(dense_time, " seconds to convergence") X = getDenseMtx(m, k, 0, 1) Y = getDenseMtx(n, k, 0, 1) A = (X @ Y.T()) X = crandom.random((m, k)) Y = crandom.random((n, k)) A = A * omega print(A) t = time.time() getSGDSparse(A, X, Y, omega, learningRate, regParamSGD, width, convNo) sparse_time = np.round_(time.time() - t, 4) print(sparse_time, "seconds to convergence for sparse, vs.", dense_time, "for dense")
def updateFactor_CG(T,U,V,W,regParam,omega,I,J,K,r,string): if (string == "U"): M1 = ctf.tensor((J,K,r)) M1.i("jku") << V.i("ju")*W.i("ku") for i in range(I): num_nonzero, dense_omega = getDenseOmega(T,U,V,W,regParam,omega,I,J,K,r,i,"i") Z = ctf.tensor((num_nonzero,r)) Z.i("tr") << dense_omega.i("jkt")*M1.i("jkr") Tbar = ctf.tensor((num_nonzero)) Tbar.i("t") << dense_omega.i("jkt") *T[i,:,:].i("jk") U[i,:].set_zero() U[i,:] = CG(Z,Tbar,r,regParam) #U[i,:] = la.lstsq(ctf.to_nparray(Z), ctf.to_nparray(Tbar))[0] return U if (string == "V"): M2 = ctf.tensor((I,K,r)) M2.i("iku") << U.i("iu")*W.i("ku") for j in range(J): num_nonzero, dense_omega = getDenseOmega(T,U,V,W,regParam,omega,I,J,K,r,j,"j") Z = ctf.tensor((num_nonzero,r)) Z.i("tr") << dense_omega.i("ikt")*M2.i("ikr") Tbar = ctf.tensor((num_nonzero)) Tbar.i("t") << dense_omega.i("ikt") *T[:,j,:].i("ik") V[j,:].set_zero() V[j,:] = CG(Z,Tbar,r,regParam) #V[j,:] = la.lstsq(ctf.to_nparray(Z), ctf.to_nparray(Tbar))[0] return V if (string == "W"): M3 = ctf.tensor((I,J,r)) M3.i("iju") << U.i("iu")*V.i("ju") for k in range(K): num_nonzero, dense_omega = getDenseOmega(T,U,V,W,regParam,omega,I,J,K,r,k,"k") Z = ctf.tensor((num_nonzero,r)) Z.i("tr") << dense_omega.i("ijt")*M3.i("ijr") Tbar = ctf.tensor((num_nonzero)) Tbar.i("t") << dense_omega.i("ijt") *T[:,:,k].i("ij") W[k,:].set_zero() W[k,:] = CG(Z,Tbar,r,regParam) #W[k,:] = la.lstsq(ctf.to_nparray(Z), ctf.to_nparray(Tbar))[0] return W
def test_noslice_sym_3d(self): n = 5 SY = ctf.SYM.SY NS = ctf.SYM.NS a0 = ctf.tensor([n,n,n], sym=[NS,SY,NS]) ctf.random.seed(1) a0.fill_random() mo = ctf.tensor([n,n]) mo.fill_random() dat = ctf.einsum('qrs,ri,sj->qij', a0, mo, mo) a1 = ctf.tensor(a0.shape, sym=[NS,NS,NS]) a1.i('ijkl') << a0.i('ijkl') ref = ctf.einsum('qrs,ri,sj->qij', a1, mo, mo) self.assertTrue(allclose(ref, dat))
def test_noslice_sym_3d(self): n = 5 SY = ctf.SYM.SY NS = ctf.SYM.NS a0 = ctf.tensor([n, n, n], sym=[NS, SY, NS]) ctf.random.seed(1) a0.fill_random() mo = ctf.tensor([n, n]) mo.fill_random() dat = ctf.einsum('qrs,ri,sj->qij', a0, mo, mo) a1 = ctf.tensor(a0.shape, sym=[NS, NS, NS]) a1.i('ijkl') << a0.i('ijkl') ref = ctf.einsum('qrs,ri,sj->qij', a1, mo, mo) self.assertTrue(allclose(ref, dat))
def sparse_GD(T, U, V, W, Lambda, omega, I, J, K, r, stepSize): iteration_count = 0 E = ctf.tensor((I, J, K)) E.i("ijk" ) << T.i("ijk") - omega.i("ijk") * U.i("iu") * V.i("ju") * W.i("ku") curr_err_norm = ctf.vecnorm(E) + (ctf.vecnorm(U) + ctf.vecnorm(V) + ctf.vecnorm(W)) * Lambda norm = [curr_err_norm] while True: U = sparse_updateU(T, U, V, W, Lambda, omega, I, J, K, r, E, stepSize) E.set_zero() E.i("ijk") << T.i( "ijk") - omega.i("ijk") * U.i("iu") * V.i("ju") * W.i("ku") V = sparse_updateV(T, U, V, W, Lambda, omega, I, J, K, r, E, stepSize) E.set_zero() E.i("ijk") << T.i( "ijk") - omega.i("ijk") * U.i("iu") * V.i("ju") * W.i("ku") W = sparse_updateW(T, U, V, W, Lambda, omega, I, J, K, r, E, stepSize) E.set_zero() E.i("ijk") << T.i( "ijk") - omega.i("ijk") * U.i("iu") * V.i("ju") * W.i("ku") next_err_norm = ctf.vecnorm(E) + (ctf.vecnorm(U) + ctf.vecnorm(V) + ctf.vecnorm(W)) * Lambda if abs(curr_err_norm - next_err_norm) < .01 or iteration_count > 100: break print(curr_err_norm, next_err_norm, ctf.vecnorm(E)) curr_err_norm = next_err_norm if iteration_count % 5 == 0: norm.append(curr_err_norm) iteration_count += 1 print("Number of iterations: ", iteration_count) return norm
def test_partition(self): AA = ctf.tensor((4, 4), sym=[ctf.SYM.SY, ctf.SYM.NS]) AA.fill_random() idx, prl, blk = AA.get_distribution() BB = ctf.tensor((4, 4), idx=idx, prl=prl.get_idx_partition(idx[:1]), blk=blk) BB += AA CC = ctf.tensor((4, 4), idx=idx, prl=prl.get_idx_partition(idx[1:2]), blk=blk) CC += AA self.assertTrue(allclose(AA, BB)) self.assertTrue(allclose(BB, CC))
def getALS_SVD(T, U, V, W, regParam, omega, I, J, K, r): it = 0 E = ctf.tensor((I, J, K)) E.i("ijk" ) << T.i("ijk") - omega.i("ijk") * U.i("iu") * V.i("ju") * W.i("ku") curr_err_norm = ctf.vecnorm(E) + (ctf.vecnorm(U) + ctf.vecnorm(V) + ctf.vecnorm(W)) * regParam while True: U = updateFactor_SVD(T, U, V, W, regParam, omega, I, J, K, r, "U") V = updateFactor_SVD(T, U, V, W, regParam, omega, I, J, K, r, "V") W = updateFactor_SVD(T, U, V, W, regParam, omega, I, J, K, r, "W") E.set_zero() E.i("ijk") << T.i( "ijk") - omega.i("ijk") * U.i("iu") * V.i("ju") * W.i("ku") next_err_norm = ctf.vecnorm(E) + (ctf.vecnorm(U) + ctf.vecnorm(V) + ctf.vecnorm(W)) * regParam if ctf.comm().rank() == 0: print(curr_err_norm, next_err_norm) if abs(curr_err_norm - next_err_norm) < .001 or it > 20: break curr_err_norm = next_err_norm it += 1 if ctf.comm().rank() == 0: print("Number of iterations: ", it) return U, V, W
def getALS_CG(T,U,V,W,regParam,omega,I,J,K,r): ''' Same thing as above, but CTF ''' it = 0 E = ctf.tensor((I,J,K)) E.i("ijk") << T.i("ijk") - omega.i("ijk")*U.i("iu")*V.i("ju")*W.i("ku") curr_err_norm = ctf.vecnorm(E) + (ctf.vecnorm(U) + ctf.vecnorm(V) + ctf.vecnorm(W))*regParam while True: U = updateFactor_CG(T,U,V,W,regParam,omega,I,J,K,r,"U") V = updateFactor_CG(T,U,V,W,regParam,omega,I,J,K,r,"V") W = updateFactor_CG(T,U,V,W,regParam,omega,I,J,K,r,"W") E.set_zero() E.i("ijk") << T.i("ijk") - omega.i("ijk")*U.i("iu")*V.i("ju")*W.i("ku") next_err_norm = ctf.vecnorm(E) + (ctf.vecnorm(U) + ctf.vecnorm(V) + ctf.vecnorm(W))*regParam print(curr_err_norm, next_err_norm) if abs(curr_err_norm - next_err_norm) < .001 or it > 20: break #print(next_err_norm/curr_err_norm) curr_err_norm = next_err_norm it += 1 print("Number of iterations: ", it) return U,V,W
def elementwise_log(T): [inds, data] = T.read_local_nnz() new_data = np.log(data) new_tensor = ctf.tensor(T.shape, sp=T.sp) new_tensor.write(inds, new_data) return new_tensor
def dense_GD(T, U, V, W, Lambda, I, J, K, r, stepSize): iteration_count = 0 E = ctf.tensor((I, J, K)) E.i("ijk") << T.i("ijk") - U.i("iu") * V.i("ju") * W.i("ku") curr_err_norm = ctf.vecnorm(E) + (ctf.vecnorm(U) + ctf.vecnorm(V) + ctf.vecnorm(W)) * Lambda while True: U = dense_updateU(T, U, V, W, Lambda, I, J, K, r, E, stepSize) V = dense_updateV(T, U, V, W, Lambda, I, J, K, r, E, stepSize) W = dense_updateW(T, U, V, W, Lambda, I, J, K, r, E, stepSize) E.set_zero() E.i("ijk") << T.i("ijk") - U.i("iu") * V.i("ju") * W.i("ku") next_err_norm = ctf.vecnorm(E) + (ctf.vecnorm(U) + ctf.vecnorm(V) + ctf.vecnorm(W)) * Lambda if abs(curr_err_norm - next_err_norm) < .001 or iteration_count > 100: break print(curr_err_norm, next_err_norm) curr_err_norm = next_err_norm iteration_count += 1 print("Number of iterations: ", iteration_count) return U, V, W
def main(): ut = UnitTests() ut.runAllTests() I = random.randint(30,50) J = random.randint(30,50) K = random.randint(30,50) r = 2 sparsity = .2 regParam = 10 ctf.random.seed(42) U = ctf.random.random((I,r)) V= ctf.random.random((J,r)) W= ctf.random.random((K,r)) # 3rd-order tensor T = ctf.tensor((I,J,K)) #T.fill_random(0,1) T.i("ijk") << U.i("iu")*V.i("ju")*W.i("ku") U = ctf.random.random((I,r)) V= ctf.random.random((J,r)) W= ctf.random.random((K,r)) omega = updateOmega(I,J,K,sparsity) t = time.time() getALSCtf(T,U,V,W,regParam,omega,I,J,K,r) print("ALS costs time = ",np.round_(time.time()- t,4))
def getALSCtf(T,U,V,W,regParam,omega,I,J,K,r): ''' Same thing as above, but CTF ''' it = 0 E = ctf.tensor((I,J,K)) E.i("ijk") << T.i("ijk") - U.i("iu")*V.i("ju")*W.i("ku") curr_err_norm = ctf.vecnorm(E) + (ctf.vecnorm(U) + ctf.vecnorm(V) + ctf.vecnorm(W))*regParam while True: U = updateU(T,U,V,W,regParam,omega,I,J,K,r) V = updateV(T,U,V,W,regParam,omega,I,J,K,r) W = updateW(T,U,V,W,regParam,omega,I,J,K,r) E.set_zero() E.i("ijk") << T.i("ijk") - U.i("iu")*V.i("ju")*W.i("ku") next_err_norm = ctf.vecnorm(E) + (ctf.vecnorm(U) + ctf.vecnorm(V) + ctf.vecnorm(W))*regParam if abs(curr_err_norm - next_err_norm) < .001 or it > 100: break print(curr_err_norm, next_err_norm) curr_err_norm = next_err_norm it += 1 print("Number of iterations: ", it) return U,V,W
def create_lowr_tensor(I, J, K, r, sp_frac, use_sp_rep): U = ctf.random.random((I, r)) V = ctf.random.random((J, r)) W = ctf.random.random((K, r)) T = ctf.tensor((I, J, K), sp=use_sp_rep) T.fill_sp_random(1, 1, sp_frac) T = ctf.TTTP(T, [U, V, W]) return T
def createOmega(I,J,K,sparsity): print(I,J,K) #Actf = ctf.tensor((I,J,K),sp=True) #Actf.fill_sp_random(0,1,sparsity) Actf = ctf.tensor((I,J,K)) Actf.fill_random(0,1) omegactf = ((Actf > 0)*ctf.astensor(1.)) return omegactf
def creat_perfect_tensor(I, J, K, r, sparsity): U = ctf.random.random((I, r)) V = ctf.random.random((J, r)) W = ctf.random.random((K, r)) T = ctf.tensor((I, J, K), sp=True) T.i("ijk") << U.i("ir") * V.i("jr") * W.i("kr") T.sample(sparsity) return T
def subtract_sparse(T, M): [inds, data] = T.read_local_nnz() [inds, data2] = M.read_local_nnz() new_data = data - data2 new_tensor = ctf.tensor(T.shape, sp=T.sp) new_tensor.write(inds, new_data) return new_tensor
def updateOmega(I,J,K,sparsity): ''' Gets a random subset of rows for each U,V,W iteration ''' Actf = ctf.tensor((I,J,K),sp=True) Actf.fill_sp_random(0,1,sparsity) omegactf = ((Actf > 0)*ctf.astensor(1.)) return omegactf
def test_qr(self): m = 8 n = 4 for dt in [numpy.float32, numpy.float64]: A = ctf.random.random((m,n)) A = ctf.astensor(A,dtype=dt) [Q,R]=ctf.qr(A) self.assertTrue(allclose(A, ctf.dot(Q,R))) self.assertTrue(allclose(ctf.eye(n), ctf.dot(Q.T(), Q))) A = ctf.tensor((m,n),dtype=numpy.complex64) rA = ctf.tensor((m,n),dtype=numpy.float32) rA.fill_random() A.real(rA) iA = ctf.tensor((m,n),dtype=numpy.float32) iA.fill_random() A.imag(iA) [Q,R]=ctf.qr(A) self.assertTrue(allclose(A, ctf.dot(Q,R))) self.assertTrue(allclose(ctf.eye(n,dtype=numpy.complex64), ctf.dot(ctf.conj(Q.T()), Q))) A = ctf.tensor((m,n),dtype=numpy.complex128) rA = ctf.tensor((m,n),dtype=numpy.float64) rA.fill_random() A.real(rA) iA = ctf.tensor((m,n),dtype=numpy.float64) iA.fill_random() A.imag(iA) [Q,R]=ctf.qr(A) self.assertTrue(allclose(A, ctf.dot(Q,R))) self.assertTrue(allclose(ctf.eye(n,dtype=numpy.complex128), ctf.dot(ctf.conj(Q.T()), Q)))
def test_svd(self): m = 9 n = 5 k = 5 for dt in [numpy.float32, numpy.float64]: A = ctf.random.random((m,n)) A = ctf.astensor(A,dtype=dt) [U,S,VT]=ctf.svd(A,k) [U1,S1,VT1]=la.svd(ctf.to_nparray(A),full_matrices=False) self.assertTrue(allclose(A, ctf.dot(U,ctf.dot(ctf.diag(S),VT)))) self.assertTrue(allclose(ctf.eye(k), ctf.dot(U.T(), U))) self.assertTrue(allclose(ctf.eye(k), ctf.dot(VT, VT.T()))) A = ctf.tensor((m,n),dtype=numpy.complex64) rA = ctf.tensor((m,n),dtype=numpy.float32) rA.fill_random() A.real(rA) iA = ctf.tensor((m,n),dtype=numpy.float32) iA.fill_random() A.imag(iA) [U,S,VT]=ctf.svd(A,k) self.assertTrue(allclose(A, ctf.dot(U,ctf.dot(ctf.diag(S),VT)))) self.assertTrue(allclose(ctf.eye(k,dtype=numpy.complex64), ctf.dot(ctf.conj(U.T()), U))) self.assertTrue(allclose(ctf.eye(k,dtype=numpy.complex64), ctf.dot(VT, ctf.conj(VT.T())))) A = ctf.tensor((m,n),dtype=numpy.complex128) rA = ctf.tensor((m,n),dtype=numpy.float64) rA.fill_random() A.real(rA) iA = ctf.tensor((m,n),dtype=numpy.float64) iA.fill_random() A.imag(iA) [U,S,VT]=ctf.svd(A,k) self.assertTrue(allclose(A, ctf.dot(U,ctf.dot(ctf.diag(S),VT)))) self.assertTrue(allclose(ctf.eye(k,dtype=numpy.complex128), ctf.dot(ctf.conj(U.T()), U))) self.assertTrue(allclose(ctf.eye(k,dtype=numpy.complex128), ctf.dot(VT, ctf.conj(VT.T()))))