def mult_lists(list_A, list_B): l = [A * B for (A, B) in zip(list_A, list_B)] s = 0 for i in range(len(l)): s += ctf.sum(l[i]) return s
def list_vecnorm(list_A): l = [i**2 for i in list_A] s = 0 for i in range(len(l)): s += ctf.sum(l[i]) return s**0.5
def list_vecnormsq(list_A): """ Vector Norm square of the list """ l = [i**2 for i in list_A] s = 0 for i in range(len(l)): s += ctf.sum(l[i]) return s
def sparse_SGD(T, U, V, W, Lambda, omega, I, J, K, r, stepSize, sample_rate, num_iter, errThresh, time_limit, work_cycle, use_MTTKRP): times = [0 for i in range(7)] iteration_count = 0 total_count = 0 R = ctf.tensor((I, J, K), sp=T.sp) if T.sp == True: nnz_tot = T.nnz_tot else: nnz_tot = ctf.sum(omega) start_time = time.time() starting_time = time.time() dtime = 0 R.i("ijk") << T.i("ijk") - ctf.TTTP(omega, [U, V, W]).i("ijk") curr_err_norm = ctf.vecnorm(R) + (ctf.vecnorm(U) + ctf.vecnorm(V) + ctf.vecnorm(W)) * Lambda times[0] += time.time() - starting_time norm = [curr_err_norm] step = stepSize * 0.5 t_obj_calc = 0. while iteration_count < num_iter and time.time() - start_time - t_obj_calc < time_limit: iteration_count += 1 starting_time = time.time() sampled_T = T.copy() sampled_T.sample(sample_rate) times[1] += time.time() - starting_time sparse_update(sampled_T, [U, V, W], Lambda, [I, J, K], r, stepSize * 0.5 + step, sample_rate, times, use_MTTKRP) #step *= 0.99 sampled_T.set_zero() if iteration_count % work_cycle == 0: duration = time.time() - start_time - t_obj_calc t_b_obj = time.time() total_count += 1 R.set_zero() R.i("ijk") << T.i("ijk") - ctf.TTTP(omega, [U, V, W]).i("ijk") diff_norm = ctf.vecnorm(R) RMSE = diff_norm/(nnz_tot**.5) next_err_norm = diff_norm + (ctf.vecnorm(U) + ctf.vecnorm(V) + ctf.vecnorm(W)) * Lambda if glob_comm.rank() == 0: print('Objective after',duration,'seconds (',iteration_count,'iterations) is: {}'.format(next_err_norm)) print('RMSE after',duration,'seconds (',iteration_count,'iterations) is: {}'.format(RMSE)) t_obj_calc += time.time() - t_b_obj if abs(curr_err_norm - next_err_norm) < errThresh: break curr_err_norm = next_err_norm norm.append(curr_err_norm) duration = time.time() - start_time - t_obj_calc if ctf.comm().rank() == 0: print('SGD amortized seconds per sweep: {}'.format(duration/(iteration_count*sample_rate))) print("Time/SGD iteration: {}".format(duration/iteration_count)) return norm
def test_sum_axis(self): a0 = numpy.ones((2, 3, 4)) a1 = ctf.from_nparray(a0) self.assertEqual(a1.sum(axis=0).shape, (3, 4)) self.assertEqual(a1.sum(axis=1).shape, (2, 4)) self.assertEqual(a1.sum(axis=-1).shape, (2, 3)) self.assertEqual(ctf.sum(a1, axis=2).shape, (2, 3)) self.assertEqual(ctf.sum(a1.transpose(2, 1, 0), axis=2).shape, (4, 3)) self.assertEqual(ctf.sum(a1, axis=(1, 2)).shape, (2, )) self.assertEqual(ctf.sum(a1, axis=(0, 2)).shape, (3, )) self.assertEqual(ctf.sum(a1, axis=(2, 0)).shape, (3, )) self.assertEqual(ctf.sum(a1, axis=(0, -1)).shape, (3, )) self.assertEqual(ctf.sum(a1, axis=(-1, -2)).shape, (2, ))
def test_sum_axis(self): a0 = numpy.ones((2,3,4)) a1 = ctf.from_nparray(a0) self.assertEqual(a1.sum(axis=0).shape, (3,4)) self.assertEqual(a1.sum(axis=1).shape, (2,4)) self.assertEqual(a1.sum(axis=-1).shape, (2,3)) self.assertEqual(ctf.sum(a1, axis=2).shape, (2,3)) self.assertEqual(ctf.sum(a1.transpose(2,1,0), axis=2).shape, (4,3)) self.assertEqual(ctf.sum(a1, axis=(1,2)).shape, (2,)) self.assertEqual(ctf.sum(a1, axis=(0,2)).shape, (3,)) self.assertEqual(ctf.sum(a1, axis=(2,0)).shape, (3,)) self.assertEqual(ctf.sum(a1, axis=(0,-1)).shape, (3,)) self.assertEqual(ctf.sum(a1, axis=(-1,-2)).shape, (2,))
def get_objective(T,U,V,W,omega,regParam): t_obj = ctf.timer("ccd_get_objective") t_obj.start() L = ctf.tensor(T.shape, sp=T.sp) t0 = time.time() L.i("ijk") << T.i("ijk") - ctf.TTTP(omega, [U,V,W]).i("ijk") t1 = time.time() normL = ctf.vecnorm(L) if T.sp == True: RMSE = normL/(T.nnz_tot**.5) else: nnz_tot = ctf.sum(omega) RMSE = normL/(nnz_tot**.5) objective = normL + (ctf.vecnorm(U) + ctf.vecnorm(V) + ctf.vecnorm(W)) * regParam t2 = time.time() if glob_comm.rank() == 0 and status_prints == True: print('generate L takes {}'.format(t1 - t0)) print('calc objective takes {}'.format(t2 - t1)) t_obj.stop() return [objective, RMSE]
def test_sum(self): a0 = numpy.arange(4.) a1 = ctf.from_nparray(a0) self.assertAlmostEqual(ctf.sum(a1), a1.sum(), 9)
def test_sum(self): a0 = numpy.arange(4.) a1 = ctf.from_nparray(a0) self.assertAlmostEqual(ctf.sum(a1), a1.sum(), 9)
def sum(A, axes=None): return ctf.sum(A, axes)
def getPCPGN(tenpy, T_in, T, O, X, reg_GN, num_iter_GN,tol,csv_file): opt = Poisson_CP_GN_Completer(tenpy, T_in, O, X) if tenpy.name() == 'ctf': nnz_tot = T_in.nnz_tot else: nnz_tot = np.sum(O) regu = reg_GN tenpy.printf("--------------------------------Poisson GN WIth CG-----------------------------") t_ALS = ctf.timer_epoch("Poisson_GN") start= time.time() # T_in = backend.einsum('ijk,ijk->ijk',T,O) it = 0 time_all = 0 P = T_in.copy() ctf.Sparse_log(P) ctf.Sparse_mul(P,T_in) ctf.Sparse_add(P,T_in,beta=-1) val2 = ctf.sum(P) #val2 = ctf.sum(subtract_sparse(elementwise_prod(T_in,elementwise_log(T_in)),T_in)) M = tenpy.TTTP(O,X) #val = ctf.sum(subtract_sparse(ctf.exp(M),elementwise_prod(T_in,M) )) P = M.copy() ctf.Sparse_mul(P,T_in) ctf.Sparse_exp(M) #rmse_lsq = tenpy.vecnorm(T_in-M)/(nnz_tot)**0.5 #tenpy.printf("least square RMSE is",rmse_lsq) ctf.Sparse_add(M,P,beta=-1) val = ctf.sum(M) P.set_zero() M.set_zero() rmse = (val+val2)/nnz_tot P.set_zero() if tenpy.is_master_proc(): tenpy.printf("After " + str(it) + " iterations,") tenpy.printf("RMSE is",rmse) if csv_file is not None: csv_writer = csv.writer( csv_file, delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL) for i in range(num_iter_GN): it+=1 s = time.time() t_ALS.begin() X = opt.step(regu) t_ALS.end() e = time.time() time_all+= e- s #rmse = tenpy.vecnorm(tenpy.TTTP(O,[U,V,W])-T_in)/(nnz_tot)**0.5 M = tenpy.TTTP(O,X) #val = ctf.sum(subtract_sparse(ctf.exp(M),elementwise_prod(T_in,M) )) P = M.copy() ctf.Sparse_mul(P,T_in) ctf.Sparse_exp(M) #rmse_lsq = tenpy.vecnorm(T_in-M)/(nnz_tot)**0.5 #tenpy.printf("least square RMSE is",rmse_lsq) ctf.Sparse_add(M,P,beta=-1) val = ctf.sum(M) P.set_zero() M.set_zero() rmse = (val+val2)/nnz_tot regu = regu/2 if tenpy.is_master_proc(): tenpy.printf("After " + str(it) + " iterations,") tenpy.printf("RMSE is",rmse) #print("Full Tensor Objective",(tenpy.norm(tenpy.einsum('ir,jr,kr->ijk',U,V,W)-T))) if csv_file is not None: csv_writer.writerow([i,time_all , rmse, i,'PGN']) csv_file.flush() if abs(rmse) < tol: tenpy.printf("Ending algo due to tolerance") break end= time.time() end= time.time() tenpy.printf('Poisson_GN time taken is ',end - start) return X
def sgd_poisson(tenpy, T_in, T, O, U, V, W, reg_als, I, J, K, R, num_iter_als, tol, csv_file): step_size = 0.03 opt = Poisson_sgd_Completer(tenpy, T_in, O, [U, V, W], step_size) #if T_in.sp == True: # nnz_tot = T_in.nnz_tot #else: # nnz_tot = ctf.sum(omega) if tenpy.name() == 'ctf': nnz_tot = T_in.nnz_tot else: nnz_tot = np.sum(O) t_ALS = ctf.timer_epoch("poisson_sgd") regu = reg_als tenpy.printf( "--------------------------------Poisson_sgd-----------------------") start = time.time() # T_in = backend.einsum('ijk,ijk->ijk',T,O) it = 0 time_all = 0 #val2 = ctf.sum(subtract_sparse(elementwise_prod(T_in,elementwise_log(T_in)),T_in)) P = T_in.copy() ctf.Sparse_log(P) ctf.Sparse_mul(P, T_in) ctf.Sparse_add(P, T_in, beta=-1) val2 = ctf.sum(P) P.set_zero() if csv_file is not None: csv_writer = csv.writer(csv_file, delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL) for i in range(num_iter_als): it += 1 s = time.time() #t_ALS.begin() [U, V, W] = opt.step(regu) #t_ALS.end() e = time.time() time_all += e - s #rmse = tenpy.vecnorm(tenpy.TTTP(O,[U,V,W])-T_in)/(nnz_tot)**0.5 if it % 20 == 0: M = tenpy.TTTP(O, [U, V, W]) #val = ctf.sum(subtract_sparse(ctf.exp(M),elementwise_prod(T_in,M) )) P = M.copy() ctf.Sparse_mul(P, T_in) ctf.Sparse_exp(M) ctf.Sparse_add(M, P, beta=-1) val = ctf.sum(M) P.set_zero() M.set_zero() rmse = (val + val2) / nnz_tot if tenpy.is_master_proc(): tenpy.printf("After " + str(it) + " iterations, and time is", time_all) tenpy.printf("RMSE is", rmse) #print("Full Tensor Objective",(tenpy.norm(tenpy.einsum('ir,jr,kr->ijk',U,V,W)-T))) if csv_file is not None: csv_writer.writerow([i, time_all, rmse, i, 'PALS']) csv_file.flush() if abs(rmse) < tol: tenpy.printf("Ending algo due to tolerance") break end = time.time() tenpy.printf('Poisson sgd time taken is ', end - start) return [U, V, W]
def getALS_CG(T, U, V, W, regParam, omega, I, J, K, r, block_size, num_iter=100, err_thresh=.001, time_limit=600, use_implicit=True): if use_implicit == True: t_ALS_CG = ctf.timer_epoch("als_CG_implicit") if ctf.comm().rank() == 0: print( "--------------------------------ALS with implicit CG------------------------" ) else: t_ALS_CG = ctf.timer_epoch("als_CG_explicit") if ctf.comm().rank() == 0: print( "--------------------------------ALS with explicit CG------------------------" ) if T.sp == True: nnz_tot = T.nnz_tot else: nnz_tot = ctf.sum(omega) t_ALS_CG.begin() it = 0 if block_size <= 0: block_size = max(I, J, K) t_init_error_norm = ctf.timer("ALS_init_error_tensor_norm") t_init_error_norm.start() t0 = time.time() E = ctf.tensor((I, J, K), sp=T.sp) #E.i("ijk") << T.i("ijk") - omega.i("ijk")*U.i("iu")*V.i("ju")*W.i("ku") E.i("ijk") << T.i("ijk") - ctf.TTTP(omega, [U, V, W]).i("ijk") t1 = time.time() curr_err_norm = ctf.vecnorm(E) + (ctf.vecnorm(U) + ctf.vecnorm(V) + ctf.vecnorm(W)) * regParam t2 = time.time() t_init_error_norm.stop() if ctf.comm().rank() == 0 and status_prints == True: print('ctf.TTTP() takes {}'.format(t1 - t0)) print('ctf.vecnorm {}'.format(t2 - t1)) t_before_loop = time.time() t_obj_calc = 0. ctf.random.seed(42) while True: t_upd_cg = ctf.timer("ALS_upd_cg") t_upd_cg.start() U = updateFactor(T, U, V, W, regParam, omega, I, J, K, r, block_size, "U", use_implicit) V = updateFactor(T, U, V, W, regParam, omega, I, J, K, r, block_size, "V", use_implicit) W = updateFactor(T, U, V, W, regParam, omega, I, J, K, r, block_size, "W", use_implicit) duration = time.time() - t_before_loop - t_obj_calc t_b_obj = time.time() E.set_zero() #E.i("ijk") << T.i("ijk") - omega.i("ijk")*U.i("iu")*V.i("ju")*W.i("ku") E.i("ijk") << T.i("ijk") - ctf.TTTP(omega, [U, V, W]).i("ijk") diff_norm = ctf.vecnorm(E) RMSE = diff_norm / (nnz_tot**.5) next_err_norm = diff_norm + (ctf.vecnorm(U) + ctf.vecnorm(V) + ctf.vecnorm(W)) * regParam t_obj_calc += time.time() - t_b_obj t_upd_cg.stop() it += 1 if ctf.comm().rank() == 0: #print("Last residual:",curr_err_norm,"New residual",next_err_norm) print('Objective after', duration, 'seconds (', it, 'iterations) is: {}'.format(next_err_norm)) print('RMSE after', duration, 'seconds (', it, 'iterations) is: {}'.format(RMSE)) if abs(curr_err_norm - next_err_norm ) < err_thresh or it >= num_iter or duration > time_limit: break curr_err_norm = next_err_norm t_ALS_CG.end() duration = time.time() - t_before_loop - t_obj_calc if glob_comm.rank() == 0: print('ALS (implicit =', use_implicit, ') time per sweep: {}'.format(duration / it))
def mult_lists(list_A, list_B): s = 0 for t in [A * B for (A, B) in zip(list_A, list_B)]: s += ctf.sum(t) return s
def list_vecnormsq(list_A): s = 0 for t in [i**2 for i in list_A]: s += ctf.sum(t) return s