def get_RE(com_rank, duplicate=10): ind = 0 XErList = [] ThetaErList = np.zeros((duplicate, len(missList))) for miss in missList: RETheta = [] REX = [] for dup in range(duplicate): np.random.seed(dup * 4) #每次都用同一份数据去做 data = dat.copy() #观测值:丢失部分数据的 Omega = (np.random.random(siz) > miss) * 1 data[Omega == 0] -= data[Omega == 0] #补全时候用的rank print('missing ratio: {0}'.format(miss)) data = pyten.tenclass.tensor.Tensor(data) self2 = PoissonAirCP(data, omega=Omega, rank=com_rank, max_iter=3000, tol=1e-5, OnlyObs=True, TrueValue=true_data, alpha=[10, 10, 10], lmbda=10, sim_mats=simMat2) self2.run() #这里看对原始数据的补全准不准 [EErr, EReErr1, EReErr2] = tenerror(self2.X, true_data, Omega) REX.append(EReErr1) #这里看对原始theta矩阵的估计准不准 pred2 = pyten.tenclass.Ktensor(np.ones(com_rank), us=self2.U).totensor() ThetaRE = np.linalg.norm(pred2.data - theta.data) / np.linalg.norm( theta.data) RETheta.append(ThetaRE) # print(np.mean(RECP),np.mean(RE1),np.mean(RE2)) XErList.append(np.mean(REX)) ThetaErList[:, ind] = RETheta ind = ind + 1 print(XErList) print(ThetaErList) return ((XErList, ThetaErList))
tol=1e-5, OnlyObs=True, TrueValue=true_data, sim_mats=aux, alpha=para_alpha, lmbda=para_lmbda) self2.run() temp_aux = cons_similarity(self2.X.data) simerror = np.max((np.linalg.norm(aux[0] - temp_aux[0]), np.linalg.norm(aux[1] - temp_aux[1]), np.linalg.norm(aux[2] - temp_aux[2]))) aux = temp_aux Iter = Iter + 1 print('ExpAirCP loop with similarity error: {0}'.format(simerror)) [EEr, EReEr1, EReEr2] = tenerror(self2.X, true_data, Omega) if Iter == 2: RE22.append(EReEr1) print(EReEr1) # 到这里为止 [EErr, EReErr1, EReErr2] = tenerror(self2.X, true_data, Omega) print('ExpAirCP Completion Error: {0}, {1}, {2}'.format( EErr, EReErr1, EReErr2)) RE2.append(EReErr1) finalList22.append(np.mean(RE22)) finalList2.append(np.mean(RE2)) for miss in missList: aux = [
eta=1e-4) self3 = TNCP(data, Omega, rank=com_rank, alpha=[3, 3, 3], lmbda=3) rX1 = falrtc(data, Omega, max_iter=100) try: #print(data.data) self1.run() #print(data.data) self2.run() self3.run() except: continue #这里看对原始数据的补全准不准 [Err, ReErr1, ReErr2] = tenerror(self1.X, true_data, Omega) [EErr, EReErr1, EReErr2] = tenerror(self2.X, true_data, Omega) [ErrTNCP, ReErrTNCP, ReErr2TNCP] = tenerror(self3.X, true_data, Omega) #[Errfal, ReErrfal, ReErr2fal] = tenerror(rX1, true_data, Omega) print('AirCP Completion Error: {0}, {1}, {2}'.format( Err, ReErr1, ReErr2)) print('ExpAirCP Completion Error: {0}, {1}, {2}'.format( EErr, EReErr1, EReErr2)) print('TNCP Completion Error: {0}, {1}, {2}'.format( ErrTNCP, ReErrTNCP, ReErr2TNCP)) #print ('falrtc Completion Error: {0}, {1}, {2}'.format(Errfal, ReErrfal, ReErr2fal)) #这里看对原始theta矩阵的估计准不准 pred2 = pyten.tenclass.Ktensor(np.ones(com_rank), us=self2.U).totensor()
OnlyObs=True, TrueValue=true_data, sim_mats=simMat, alpha=[3, 3, 3], lmbda=3) self3 = TNCP(data, Omega, rank=com_rank, alpha=[3, 3, 3], lmbda=3) #rX1 = falrtc(data, Omega, max_iter=100) self1.run() self2.run() self3.run() #这里看对原始数据的补全准不准 [Err, ReErr1, ReErr2] = tenerror(self1.X, true_data, Omega) [EErr, EReErr1, EReErr2] = tenerror(self2.X, true_data, Omega) [Errcp, ReErrcp, ReErr2cp] = tenerror(self3.X, true_data, Omega) #[Errfal, ReErrfal, ReErr2fal] = tenerror(rX1, true_data, Omega) print('AirCP Completion Error: {0}, {1}, {2}'.format( Err, ReErr1, ReErr2)) print('ExpAirCP Completion Error: {0}, {1}, {2}'.format( EErr, EReErr1, EReErr2)) print('TNCP Completion Error: {0}, {1}, {2}'.format( Errcp, ReErrcp, ReErr2cp)) #print ('falrtc Completion Error: {0}, {1}, {2}'.format(Errfal, ReErrfal, ReErr2fal)) pred2 = pyten.tenclass.Ktensor(np.ones(com_rank), us=self2.U).totensor() print('Theta Error for ExpAirCP: {0}'.format(
failuresR=failuresR2, omega=Omega, rank=com_rank, max_iter=3000, tol=1e-5, OnlyObs=True, TrueValue=true_data, sim_mats=simMat, alpha=[3, 3, 3], lmbda=3) self2.run() self3.run() #这里看对原始数据的补全准不准 [EErr, EReErr1, EReErr2] = tenerror(self2.X, true_data, Omega) [EErr2, EReErr3, EReErr4] = tenerror(self3.X, true_data, Omega) RE2.append(EReErr1) RE3.append(EReErr3) # # # x = self1.X.data-true_data.data # y = self2.X.data-true_data.data # all_data=[x.reshape(np.prod(siz)),y.reshape(np.prod(siz))] # # figure,axes=plt.subplots() #得到画板、轴 # axes.boxplot(all_data) #描点上色 # plt.show() finalList2[:, ind] = RE2
# Tucker-ALS [T2, rX2] = tucker_als(X1, R, Omega1) # if no missing data just omit Omega1 # FalRTC, SiLRTC, HaLRTC rX3 = falrtc(X1, Omega1) rX4 = silrtc(X1, Omega1) rX5 = halrtc(X1, Omega1) # TNCP self1 = TNCP(X1, Omega1, rank=r) self1.run() # Error Testing from pyten.tools import tenerror realX = sol1.totensor() [Err1, ReErr11, ReErr21] = tenerror(rX1, realX, Omega1) [Err2, ReErr12, ReErr22] = tenerror(rX2, realX, Omega1) [Err3, ReErr13, ReErr23] = tenerror(rX3, realX, Omega1) [Err4, ReErr14, ReErr24] = tenerror(rX4, realX, Omega1) [Err5, ReErr15, ReErr25] = tenerror(rX5, realX, Omega1) [Err6, ReErr16, ReErr26] = tenerror(self1.X, realX, Omega1) print '\n', 'The Relative Error of the Six Methods are:', ReErr21, ReErr22, ReErr23, ReErr24, ReErr25, ReErr26 # 2. Real Problem - Image Recovery import matplotlib.image as mpimg # Use it to load image import numpy as np lena = mpimg.imread("./test/testImg.png") im = np.double(np.uint8(lena * 255)) im = im[0:50, 0:50, 0:3]
simerror = 1 Iter = 1 while (simerror > 1e-3 and Iter < 10): self1 = PoissonAirCP(data, omega=Omega, rank=com_rank, max_iter=3000, tol=1e-5, OnlyObs=True, TrueValue=true_data, sim_mats=aux, alpha=para_alpha, lmbda=para_lmbda) self1.run() temp_aux = cons_similarity(self1.X.data) simerror = np.max((np.linalg.norm(aux[0] - temp_aux[0]), np.linalg.norm(aux[1] - temp_aux[1]), np.linalg.norm(aux[2] - temp_aux[2]))) aux = temp_aux Iter = Iter + 1 print(simerror) # 到这里为止 [EEr, EReEr1, EReEr2] = tenerror(self1.X, true_data, Omega) print('ExpAirCP with aux: {0}, {1}, {2}'.format( EEr, EReEr1, EReEr2)) RE1.append(EReEr1) result[i, j, k] = np.mean(RE1) print(result) np.save('GDELTstep5.npy', result)