def compute_kernel_row(self): K = np.zeros((self.nrow, self.nrow)) drow = {} for i in range(self.nrow): iA = np.where(self.xdata[i] > 0)[0] nsub = len(iA) if nsub > 0: A = self.base_vectors[iA] AAT = self.kernel_col[iA.reshape((nsub, 1)), iA] AATI = sp_lin.pinvh(AAT) drow[i] = (np.dot(AATI, A), iA) else: drow[i] = (None, np.array([])) for i in range(self.nrow): (BBTI, iB) = drow[i] if len(iB) > 0: for j in range(i + 1): (AATI, iA) = drow[j] ## = trace(Vhi.T,Vhj)=\braket{Vhi,Vhj}_{Frobenius} ## (BB^T)^{-1}BA^T(AA^T)^{-1} if len(iA) > 0: BAT = self.kernel_col[iB.reshape((len(iB), 1)), iA] ## xP=np.diag(np.dot(BBTI.T,np.dot(BAT,AATI))) xP = np.dot(BBTI, AATI.T) * BAT K[i, j] = np.sum(xP) K[j, i] = K[i, j] ## if i%1000==0: ## print(i) d1 = np.diag(K) d2 = d1 self.kernel_row = kernel_eval_nl(K, d1, d2, self.param_row) print('Modular kernel done') return
def compute_kernel_row(self): K=np.zeros((self.nrow,self.nrow)) drow={} for i in range(self.nrow): iA=np.where(self.xdata[i]>0)[0] nsub=len(iA) if nsub>0: A=self.base_vectors[iA] AAT=self.kernel_col[iA.reshape((nsub,1)),iA] AATI=sp_lin.pinvh(AAT) drow[i]=(np.dot(AATI,A),iA) else: drow[i]=(None,np.array([])) for i in range(self.nrow): (BBTI,iB)=drow[i] if len(iB)>0: for j in range(i+1): (AATI,iA)=drow[j] ## = trace(Vhi.T,Vhj)=\braket{Vhi,Vhj}_{Frobenius} ## (BB^T)^{-1}BA^T(AA^T)^{-1} if len(iA)>0: BAT=self.kernel_col[iB.reshape((len(iB),1)),iA] ## xP=np.diag(np.dot(BBTI.T,np.dot(BAT,AATI))) xP=np.dot(BBTI,AATI.T)*BAT K[i,j]=np.sum(xP) K[j,i]=K[i,j] ## if i%1000==0: ## print(i) d1=np.diag(K) d2=d1 self.kernel_row=kernel_eval_nl(K,d1,d2,self.param_row) print('Modular kernel done') return
def compute_kernel_kernel_row(self, itrates=0): K = np.zeros((self.nrow, self.nrow_test)) drow = {} for i in range(self.nrow): iA = np.where(self.xdata[i] > 0)[0] nsub = len(iA) if nsub > 0: AAT = self.kernel_col[iA.reshape((nsub, 1)), iA] AATI = sp_lin.pinvh(AAT) drow[i] = (AATI, iA) else: drow[i] = (None, None) drow_test = {} if itrates == 1: for i in range(self.nrow_test): iA = np.where(self.xdata_test[i] > 0)[0] nsub = len(iA) if nsub > 0: AAT = self.kernel_col[iA.reshape((nsub, 1)), iA] AATI = sp_lin.pinvh(AAT) drow_test[i] = (AATI, iA) else: drow_test[i] = (None, None) else: drow_test = drow for i in range(self.nrow): (BBTI, iB) = drow[i] if iB is not None: if itrates == 0: ## to avoid double computation of symmetric kernel for j in range(i + 1): (AATI, iA) = drow[j] if iA is not None: KBA = self.kernel_col[iB.reshape((len(iB), 1)), iA] xP = np.dot(KBA.T, np.dot(BBTI, np.dot(KBA, AATI))) K[i, j] = np.sum(xP) K[j, i] = K[i, j] elif itrates == 1: for j in range(self.nrow_test): (AATI, iA) = drow_test[j] if iA is not None: KBA = self.kernel_col[iB.reshape((len(iB), 1)), iA] xP = np.dot(KBA.T, np.dot(BBTI, np.dot(KBA, AATI))) K[i, j] = np.sum(xP) ## if i%1000==0: ## print(i) dK = np.sqrt(np.diag(K)) idK = np.where(dK == 0)[0] for i in idK: K[i, i] = 1.0 dK[i] = 1.0 K = K / np.outer(dK, dK) d1 = np.diag(K) if itrates == 0: d2 = d1 elif itrates == 1: d2 = np.zeros(self.nrow_test) for i in range(self.nrow_test): (AATI, iA) = drow_test[i] if iA is not None: KBA = self.kernel_col[iA.reshape((len(iA), 1)), iA] xP = np.dot(KBA.T, np.dot(AATI, np.dot(KBA, AATI))) d2[i] = np.sum(xP) if itrates == 0: self.kernel_row = kernel_eval_nl(K, d1, d2, self.param_row) elif itrates == 1: self.kernel_row_test=kernel_eval_nl(K,d1,d2, \ self.param_row) self.kernel_row = kernel_center(self.kernel_row) dK = np.diag(self.kernel_row) idK = np.where(dK < 0)[0] for i in idK: self.kernel_row[i, i] = 0 dK = np.sqrt(np.diag(self.kernel_row)) idK = np.where(dK == 0)[0] for i in idK: self.kernel_row[i, i] = 1.0 dK[i] = 1.0 self.kernel_row = self.kernel_row / np.outer(dK, dK) ## print('Modular kernel done') return
def compute_kernel_kernel_row(self,itrates=0): K=np.zeros((self.nrow,self.nrow_test)) drow={} for i in range(self.nrow): iA=np.where(self.xdata[i]>0)[0] nsub=len(iA) if nsub>0: AAT=self.kernel_col[iA.reshape((nsub,1)),iA] AATI=sp_lin.pinvh(AAT) drow[i]=(AATI,iA) else: drow[i]=(None,None) drow_test={} if itrates==1: for i in range(self.nrow_test): iA=np.where(self.xdata_test[i]>0)[0] nsub=len(iA) if nsub>0: AAT=self.kernel_col[iA.reshape((nsub,1)),iA] AATI=sp_lin.pinvh(AAT) drow_test[i]=(AATI,iA) else: drow_test[i]=(None,None) else: drow_test=drow for i in range(self.nrow): (BBTI,iB)=drow[i] if iB is not None: if itrates==0: ## to avoid double computation of symmetric kernel for j in range(i+1): (AATI,iA)=drow[j] if iA is not None: KBA=self.kernel_col[iB.reshape((len(iB),1)),iA] xP=np.dot(KBA.T,np.dot(BBTI,np.dot(KBA,AATI))) K[i,j]=np.sum(xP) K[j,i]=K[i,j] elif itrates==1: for j in range(self.nrow_test): (AATI,iA)=drow_test[j] if iA is not None: KBA=self.kernel_col[iB.reshape((len(iB),1)),iA] xP=np.dot(KBA.T,np.dot(BBTI,np.dot(KBA,AATI))) K[i,j]=np.sum(xP) ## if i%1000==0: ## print(i) dK=np.sqrt(np.diag(K)) idK=np.where(dK==0)[0] for i in idK: K[i,i]=1.0 dK[i]=1.0 K=K/np.outer(dK,dK) d1=np.diag(K) if itrates==0: d2=d1 elif itrates==1: d2=np.zeros(self.nrow_test) for i in range(self.nrow_test): (AATI,iA)=drow_test[i] if iA is not None: KBA=self.kernel_col[iA.reshape((len(iA),1)),iA] xP=np.dot(KBA.T,np.dot(AATI,np.dot(KBA,AATI))) d2[i]=np.sum(xP) if itrates==0: self.kernel_row=kernel_eval_nl(K,d1,d2,self.param_row) elif itrates==1: self.kernel_row_test=kernel_eval_nl(K,d1,d2, \ self.param_row) self.kernel_row=kernel_center(self.kernel_row) dK=np.diag(self.kernel_row) idK=np.where(dK<0)[0] for i in idK: self.kernel_row[i,i]=0 dK=np.sqrt(np.diag(self.kernel_row)) idK=np.where(dK==0)[0] for i in idK: self.kernel_row[i,i]=1.0 dK[i]=1.0 self.kernel_row=self.kernel_row/np.outer(dK,dK) ## print('Modular kernel done') return