def _arrange_kernel(self): Y = [2 * int(y == self.classes_[1]) - 1 for y in self.Y] n_sample = len(self.Y) ker_matrix = matrix(summation(self.KL)) YY = spdiag(Y) solvers.options['show_progress'] = False solvers.options['maxiters'] = self.max_iter sol = solvers.qp( 2 * ((1.0 - self.lam) * YY * ker_matrix * YY + spdiag([self.lam] * n_sample)), matrix([0.0] * n_sample), -spdiag([1.0] * n_sample), matrix([0.0] * n_sample, (n_sample, 1)), matrix([[float(y == +1) for y in Y], [float(y == -1) for y in Y]]).T, matrix([[1.0] * 2], (2, 1))) if self.verbose: print('[EasyMKL]') print('optimization finished, #iter = %d' % sol['iterations']) print('status of the solution: %s' % sol['status']) print('objval: %.5f' % sol['primal objective']) yg = sol['x'].T * YY weights = [(yg * matrix(K) * yg.T)[0] for K in self.KL] norm2 = sum(weights) self.weights = np.array([w / norm2 for w in weights]) self.ker_matrix = summation(self.KL, self.weights) return self.ker_matrix
def _arrange_kernel(self): Y = [1 if y==self.classes_[1] else -1 for y in self.Y] n_sample = len(self.Y) ker_matrix = matrix(summation(self.KL)) YY = spdiag(Y) KLL = (1.0-self.lam)*YY*ker_matrix*YY LID = spdiag([self.lam]*n_sample) Q = 2*(KLL+LID) p = matrix([0.0]*n_sample) G = -spdiag([1.0]*n_sample) h = matrix([0.0]*n_sample,(n_sample,1)) A = matrix([[1.0 if lab==+1 else 0 for lab in Y],[1.0 if lab2==-1 else 0 for lab2 in Y]]).T b = matrix([[1.0],[1.0]],(2,1)) solvers.options['show_progress'] = False solvers.options['maxiters'] = self.max_iter sol = solvers.qp(Q,p,G,h,A,b) gamma = sol['x'] if self.verbose: print ('[EasyMKL]') print ('optimization finished, #iter = %d' % sol['iterations']) print ('status of the solution: %s' % sol['status']) print ('objval: %.5f' % sol['primal objective']) yg = gamma.T * YY weights = [(yg*matrix(K)*yg.T)[0] for K in self.KL] norm2 = sum([w for w in weights]) self.weights = np.array([w / norm2 for w in weights]) ker_matrix = summation(self.KL, self.weights) self.ker_matrix = ker_matrix return ker_matrix
def _arrange_kernel(self): Y = [1 if y == self.classes_[1] else -1 for y in self.Y] n_sample = len(self.Y) ker_matrix = matrix(summation(self.KL)) YY = spdiag(Y) KLL = (1.0 - self.lam) * YY * ker_matrix * YY LID = spdiag([self.lam] * n_sample) Q = 2 * (KLL + LID) p = matrix([0.0] * n_sample) G = -spdiag([1.0] * n_sample) h = matrix([0.0] * n_sample, (n_sample, 1)) A = matrix([[1.0 if lab == +1 else 0 for lab in Y], [1.0 if lab2 == -1 else 0 for lab2 in Y]]).T b = matrix([[1.0], [1.0]], (2, 1)) solvers.options['show_progress'] = False solvers.options['maxiters'] = self.max_iter sol = solvers.qp(Q, p, G, h, A, b) gamma = sol['x'] if self.verbose: print('[EasyMKL]') print('optimization finished, #iter = {}', sol['iterations']) print('status of the solution: {}', sol['status']) print('objval: {}', sol['primal objective']) yg = gamma.T * YY weights = [(yg * matrix(K) * yg.T)[0] for K in self.KL] norm2 = sum([w for w in weights]) self.weights = np.array([w / norm2 for w in weights]) ker_matrix = summation(self.KL, self.weights) self.ker_matrix = ker_matrix return ker_matrix
def arrange_kernel(self, X, Y, check=True): if check: self._input_checks(X, Y) #if len(self.classes_) != 2: # raise ValueError("The number of classes has to be 2; got ", len(self.classes_)) self.Y = [1 if y == self.classes_[1] else -1 for y in self.Y] n_sample = len(self.Y) ker_matrix = matrix(summation(self.K)) #ker_matrix = ker_matrix / len(X) YY = matrix(np.diag(list(matrix(self.Y)))) KLL = (1.0 - self.lam) * YY * ker_matrix * YY LID = matrix(np.diag([self.lam] * n_sample)) Q = 2 * (KLL + LID) p = matrix([0.0] * n_sample) G = -matrix(np.diag([1.0] * n_sample)) h = matrix([0.0] * n_sample, (n_sample, 1)) A = matrix([[1.0 if lab == +1 else 0 for lab in self.Y], [1.0 if lab2 == -1 else 0 for lab2 in self.Y]]).T b = matrix([[1.0], [1.0]], (2, 1)) solvers.options['show_progress'] = False solvers.options['maxiters'] = self.max_iter sol = solvers.qp(Q, p, G, h, A, b) self.gamma = sol['x'] if self.verbose: print '[EasyMKL] - first step' print 'optimization finished, #iter = ', sol['iterations'] print 'status of the solution: ', sol['status'] print 'objval: ', sol['primal objective'] # Bias for classification: bias = 0.5 * self.gamma.T * ker_matrix * YY * self.gamma self.bias = bias # Weights evaluation: #yg = mul(self.gamma.T,matrix(self.Y).T) yg = self.gamma.T * YY self.weights = [] for kermat in self.K: b = yg * matrix(kermat) * yg.T self.weights.append(b[0]) norm2 = sum([w for w in self.weights]) #norm2 = sum(self.weights) self.weights = [w / norm2 for w in self.weights] if self.tracenorm: for idx, val in enumerate(self.traces): self.weights[idx] = self.weights[idx] / val ker_matrix = summation(self.K, self.weights) self.ker_matrix = ker_matrix #return matrix(summation(self.K, self.weights)) return ker_matrix
def fit(self,K,Y): Ks = matrix(summation(K)) YY = spdiag(matrix(Y)) n = len(Y) K1 = (1.0-self.D)* YY*Ks*YY + spdiag([self.D] * n) #K2 = self.C * Ks * self.D K2 = (1.0-self.D)* self.C * Ks * self.D + spdiag([self.D] * n) * self.C P = 2*matrix([[K1[i,j] if i<n and j<n else K2[i-n,j-n] if i>=n and j>=n else 0.0 for i in range(2*n)] for j in range(2*n)]) q = matrix([0.0 for i in range(2*n)]) h = matrix([0.0]*(2*n),(2*n,1)) G = -spdiag([1 for i in range(2*n)]) A = matrix([[1.0 if i<n and Y[i]==+1 else 0.0 for i in range(2*n)], [1.0 if i<n and Y[i]==-1 else 0.0 for i in range(2*n)], [1.0 if i>=n else 0 for i in range(2*n)]]).T b = matrix([1.0,1.0,1.0]) solvers.options['show_progress'] = False solvers.options['maxiters'] = 200 sol = solvers.qp(P,q,G,h,A,b) x = sol['x'] gamma = matrix(x[:n]) alpha = matrix(x[n:2*n]) #return weights = [] for k in K: w = (gamma.T*YY*matrix(k)*YY*gamma)[0] + self.C * (alpha.T*matrix(k)*alpha)[0] weights.append(w) norm = sum([w for w in weights]) self.weights = [w / norm for w in weights] #fine primo step Ks = matrix(summation(K,self.weights)) K1 = (1.0-self.D)* YY*Ks*YY + spdiag([self.D] * n) #K2 = self.C * Ks * self.D K2 = (1.0-self.D)* self.C * Ks * self.D + spdiag([self.D] * n) * self.C P = 2*matrix([[K1[i,j] if i<n and j<n else K2[i-n,j-n] if i>=n and j>=n else 0.0 for i in range(2*n)] for j in range(2*n)]) q = matrix([0.0 for i in range(2*n)]) h = matrix([0.0]*(2*n),(2*n,1)) G = -spdiag([1 for i in range(2*n)]) A = matrix([[1.0 if i<n and Y[i]==+1 else 0.0 for i in range(2*n)], [1.0 if i<n and Y[i]==-1 else 0.0 for i in range(2*n)], [1.0 if i>=n else 0 for i in range(2*n)]]).T b = matrix([1.0,1.0,1.0]) solvers.options['show_progress'] = False solvers.options['maxiters'] = 200 sol = solvers.qp(P,q,G,h,A,b) x = sol['x'] self.gamma = matrix(x[:n]) self.alpha = matrix(x[n:2*n]) self.Y=Y return self
def _arrange_kernel(self): Y = [1 if y == self.classes_[1] else -1 for y in self.Y] nn = len(Y) nk = self.n_kernels YY = spdiag(Y) beta = [0.0] * nk mu = np.exp(beta) mu /= mu.sum() #actual_weights = eta[:] actual_ratio = None Q = np.array([[ np.dot(self.KL[r].ravel(), self.KL[s].ravel()) for r in range(nk) ] for s in range(nk)]) Q /= np.sum([frobenius(K)**2 for K in self.KL]) self.sr, self.margin = [], [] self.obj = [] _beta, _mu = None, None cstep = self.step I = np.diag(np.ones(nn)) for i in xrange(self.max_iter): Kc = summation(self.KL, mu) #trovo i gamma clf = KOMD(kernel='precomputed', lam=self.lam).fit(Kc, Y) gamma = clf.gamma _margin = (gamma.T * YY * matrix(Kc) * YY * gamma)[0] #m = (gamma.T * YY * matrix(Kc) * YY * gamma)[0] grad = np.array([(self.C * np.dot(Q[r],mu) + (gamma.T * YY * matrix(self.KL[r]) * YY * gamma)[0]) \ * mu[r] * (1- mu[r]) \ for r in range(nk)]) _beta = beta + cstep * grad _mu = np.exp(_beta) _mu /= _mu.sum() _obj = _margin + (self.C / 2) * np.dot(_mu, np.dot(_mu, Q)) if (self.obj and _obj < self.obj[-1] ) or _margin < 1.e-4: # nel caso di peggioramento cstep /= 2.0 if cstep < 0.00001: break else: self.obj.append(_obj) self.margin.append(_margin) mu = _mu beta = _beta self._steps = i + 1 #print 'steps', self._steps, 'lam',self.lam,'margin',self.margin[-1] self.weights = np.array(mu) self.ker_matrix = summation(self.KL, self.weights) return self.ker_matrix
def decision_function(self, X): if self.is_fitted == False: raise NotFittedError( "This EasyMKL instance is not fitted yet. Call 'fit' with appropriate arguments before using this method." ) if self.kernel == 'precomputed': K = check_KL_Y(X, self.Y) else: X = check_array(X, accept_sparse='csr', dtype=np.float64, order="C") if X.shape[1] != self.n_f: raise ValueError("The number of feature in X not correspond") #K = self.K.set_test(X) K = kernel_list(self.X, X, self.K) if self.multiclass_ == True: return self.cls.decision_function(X) YY = matrix(np.diag(list(matrix(self.Y)))) ker_matrix = matrix(summation(K, self.weights)) z = ker_matrix * YY * self.gamma z = z - self.bias return np.array(list(z))
def decision_function(self, K): YY = spdiag(matrix(self.Y)) ker_matrix = matrix(summation(K, self.weights)) #z = ker_matrix*YY*self.gamma + self.C * ker_matrix*self.alpha #z = ker_matrix*YY*self.gamma + ker_matrix*self.alpha z = ker_matrix*YY*self.gamma return np.array(list(z))
def arrange_kernel(self,X,Y): if len(np.unique(Y)) > 2: raise ArrangeMulticlassError('arrange_kernel does not work in multiclass context') self._input_checks(X,Y) self.Y = [1 if y==self.classes_[1] else -1 for y in self.Y] n_sample = len(self.Y) ker_matrix = matrix(summation(self.KL)) YY = spdiag(self.Y) KLL = (1.0-self.lam)*YY*ker_matrix*YY LID = spdiag([self.lam]*n_sample) Q = 2*(KLL+LID) p = matrix([0.0]*n_sample) G = -spdiag([1.0]*n_sample) h = matrix([0.0]*n_sample,(n_sample,1)) A = matrix([[1.0 if lab==+1 else 0 for lab in self.Y],[1.0 if lab2==-1 else 0 for lab2 in self.Y]]).T b = matrix([[1.0],[1.0]],(2,1)) solvers.options['show_progress'] = False solvers.options['maxiters'] = self.max_iter sol = solvers.qp(Q,p,G,h,A,b) self.gamma = sol['x'] if self.verbose: print('[EasyMKL] - first step') print('optimization finished, #iter = ', sol['iterations']) print('status of the solution: ', sol['status']) print('objval: ', sol['primal objective']) # Bias for classification: #bias = 0.5 * self.gamma.T * ker_matrix * YY * self.gamma #self.bias = bias # Weights evaluation: #yg = mul(self.gamma.T,matrix(self.Y).T) yg = self.gamma.T * YY self.weights = [] for kermat in self.KL: b = yg*matrix(kermat)*yg.T self.weights.append(b[0]) norm2 = sum([w for w in self.weights]) self.weights = [w / norm2 for w in self.weights] ker_matrix = summation(self.KL, self.weights) self.ker_matrix = ker_matrix return ker_matrix
def _heuristics(self): if self.h == 'fixed_log': return self.fixed_log if self.h == 'exp': return self.base if self.h == 'log': return self.log elif self.h == 'linear' or True: return lambda k_list: summation( k_list, [i * self.m + 1 for i in range(self.n_kernels)])
def _arrange_kernel(self): Y = [1 if y == self.classes_[1] else -1 for y in self.Y] n_sample = len(self.Y) ker_matrix = matrix(summation(self.KL)) YY = spdiag(Y) KLL = (1.0 - self.lam) * YY * ker_matrix * YY LID = spdiag([self.lam] * n_sample) Q = 2 * (KLL + LID) p = matrix([0.0] * n_sample) G = -spdiag([1.0] * n_sample) h = matrix([0.0] * n_sample, (n_sample, 1)) A = matrix([[1.0 if lab == +1 else 0 for lab in Y], [1.0 if lab2 == -1 else 0 for lab2 in Y]]).T b = matrix([[1.0], [1.0]], (2, 1)) solvers.options['show_progress'] = False solvers.options['maxiters'] = self.max_iter sol = solvers.qp(Q, p, G, h, A, b) gamma = sol['x'] yg = gamma.T * YY weights_margin = [(yg * matrix(K) * yg.T)[0] for K in self.KL] P = 2 * ker_matrix p = -matrix([ker_matrix[i, i] for i in range(n_sample)]) G = -spdiag([1.0] * n_sample) h = matrix([0.0] * n_sample) A = matrix([1.0] * n_sample).T b = matrix([1.0]) solvers.options['show_progress'] = False sol = solvers.qp(P, p, G, h, A, b) alpha = sol['x'] weights_radius = [(alpha.T * matrix(K) * alpha)[0] for K in self.KL] weights = [a / b for (a, b) in zip(weights_radius, weights_margin)] norm2 = sum([w for w in weights]) self.weights = np.array([w / norm2 for w in weights]) ker_matrix = summation(self.KL, self.weights) self.ker_matrix = ker_matrix return ker_matrix
def _arrange_kernel(self): Y = [1 if y == self.classes_[1] else -1 for y in self.Y] nn = len(Y) nk = self.n_kernels YY = spdiag(Y) beta = [0.0] * nk mu = np.exp(beta) mu /= mu.sum() Kc = summation(self.KL, mu) _r, alpha, _sol_a_old = radius(Kc) _m, gamma, _sol_g_old = margin(Kc, Y) self._ratios = [] cstep = self.step self._converg = False self._steps = 0 while (not self._converg and (self._steps < self.max_iter)): self._steps += 1 eb = np.exp(beta) #calcolo il gradiene a = np.array( [1.0 - (alpha.T * matrix(K) * alpha)[0] for K in self.KL]) b = np.array([(gamma.T * YY * matrix(K) * YY * gamma)[0] for K in self.KL]) den = [np.dot(eb, b)**2] * nk #num = [sum([eta[s]*(a[s]*b[r]-a[r]*b[s]) for s in range(nk)]) for r in range(nk)] num = [ eb[r] * (a[r] * np.dot(eb, b) - b[r] * np.dot(eb, a)) for r in range(nk) ] #calcolo i pesi temporanei _beta = [beta[k] - cstep * (num[k] / den[k]) for k in range(nk)] current_mu = np.exp(_beta) / np.exp(_beta).sum() #testo la nuova soluzione try: Kc = summation(self.KL, current_mu) _r, alpha, _sol_a = radius(Kc, init_sol=_sol_a_old) _m, gamma, _sol_g = margin(Kc, Y, init_sol=_sol_g_old) _sol_a_old = _sol_a.copy() _sol_g_old = _sol_g.copy() except: print '### warning at step %d:' % self._steps print 'current weights:', current_mu cstep /= 2.0 new_ratio = (_r**2 / _m**2) / nn #caso 1: primo passo o soluzione migliorativa if not self._ratios or self._ratios[-1] > new_ratio: #aggiorno lo stato #print 'soluzione migliorativa' beta = _beta mu = current_mu self._ratios.append(new_ratio) #caso 2: soluzione peggiorativa elif self._ratios[-1] <= new_ratio: cstep /= 2.0 #controllo sulla convergenza if cstep <= 1e-10 or \ ( len(self._ratios)>=2 and np.linalg.norm(self._ratios[-1]-self._ratios[-2]) <= 1e-20) : self._converg = True self.weights = np.array(mu) self.ker_matrix = summation(self.KL, self.weights) return self.ker_matrix
def _arrange_kernel(self): print "WARNING: probably not working" Y = [1 if y == self.classes_[1] else -1 for y in self.Y] n = len(self.Y) YY = spdiag(Y) actual_weights = np.ones(self.n_kernels) / (1.0 * self.n_kernels ) #current obj = None #weights = np.ones(self.n_kernels) / (1.0 *self.n_kernels) #current print actual_weights self.objs = [] cstep = self.step self.margin = [] for i in xrange(self.max_iter): Kc = summation(self.KL, actual_weights) #ottimizzo su alpha (posso prenderlo dallo step precedente....) clf = SVC(C=self.C, kernel='precomputed').fit(Kc, Y) alpha = np.zeros(n) alpha[clf.support_] = clf.dual_coef_ alpha = matrix(alpha) #dati gli alpha ottimizzo sui pesi J = (-0.5 * alpha.T * YY * matrix(Kc) * YY * alpha)[0] + np.sum(alpha) grad = [(-0.5 * alpha.T * YY * matrix(_K) * YY * alpha)[0] for _K in self.KL] mu = np.argmax(actual_weights) #all'inizio sono tutti uguali idx = np.where(actual_weights == max(actual_weights)) mu = np.argmax(np.array(grad)[idx]) D = [ 0 if actual_weights[j]==0 and grad[j] - grad[mu] > 0 else \ -grad[j] + grad[mu] if actual_weights[j]>0 and j!=mu else \ np.sum([grad[v]-grad[mu] for v in range(self.n_kernels) if grad[v]>0]) if j==mu else\ 0 for j in range(self.n_kernels)] print 'd', D #aggiorno i pesi dato il gradiente weights = actual_weights + cstep * np.array( D) #originalmente era un + weights = weights.clip(0.0) if weights.sum() == 0.0: print i, 'zero', weights cstep /= 2.0 continue weights = weights / weights.sum() #riottimizzo sugli alfa Kc = summation(self.KL, weights) clf = SVC(C=self.C, kernel='precomputed').fit(Kc, Y) alpha = np.zeros(n) alpha[clf.support_] = clf.dual_coef_ alpha = matrix(alpha) new_obj = (-0.5 * alpha.T * YY * matrix(Kc) * YY * alpha)[0] + np.sum(alpha) if obj and abs(new_obj - obj) / n < self.tol: #completato #print i,'tol' self.objs.append(new_obj) actual_weights = weights print 'terminato', new_obj, obj break elif new_obj <= obj or not obj: #tutto in regola #print i,'new step',new_ratio ma = margin(Kc, Y) if len(self.margin) > 0 and self.margin[-1] > ma: continue self.margin.append(ma) obj = new_obj actual_weights = weights print actual_weights, obj self.objs.append(obj) else: #supero il minimo weights = actual_weights cstep /= 2.0 print i, 'overflow', cstep continue self._steps = i + 1 self.weights = np.array(actual_weights) self.ker_matrix = summation(self.KL, self.weights) return self.ker_matrix
def _arrange_kernel(self): Y = [1 if y==self.classes_[1] else -1 for y in self.Y] n = len(self.Y) R = np.array([radius(K) for K in self.KL]) YY = matrix(np.diag(self.Y)) actual_weights = np.ones(self.n_kernels) / (1.0 *self.n_kernels) #current actual_ratio = None #weights = np.ones(self.n_kernels) / (1.0 *self.n_kernels) #current self.ratios = [] cstep = self.step for i in xrange(self.max_iter): ru2 = np.dot(actual_weights,R**2) C = self.C / ru2 Kc = matrix(summation(self.KL,actual_weights)) clf = SVC(C=C, kernel='precomputed').fit(Kc,Y) alpha = np.zeros(n) alpha[clf.support_] = clf.dual_coef_ alpha = matrix(alpha) Q = Kc + spdiag( [ru2/self.C] * n ) J = (-0.5 * alpha.T * YY * Q * YY * alpha)[0] + np.sum(alpha) grad = [(-0.5 * alpha.T * YY *(Kc+ spdiag([_r**2/self.C]*n)) * YY * alpha)[0] for _r in R] weights = actual_weights + cstep * np.array(grad) #originalmente era un + weights = weights.clip(0.0) if weights.sum() == 0.0: #print i,'zero' cstep /= -2.0 continue weights = weights/weights.sum() Kc = summation(self.KL,weights) new_ratio = radius(Kc)**2 / margin(Kc,Y)**2 if actual_ratio and abs(new_ratio - actual_ratio)/n < self.tol: #completato #print i,'tol' self.ratios.append(new_ratio) actual_weights=weights #break; elif new_ratio <= actual_ratio or not actual_ratio: #tutto in regola #print i,'new step',new_ratio actual_ratio = new_ratio actual_weights = weights self.ratios.append(actual_ratio) else: #supero il minimo weights = actual_weights cstep /= -2.0 #print i,'overflow',cstep continue self._steps = i+1 self.weights = np.array(actual_weights) self.ker_matrix = summation(self.KL,self.weights) return self.ker_matrix return average(self.KL,weights)
def test_summation(self): KLtr_sum, KLte_sum = summation(self.KLtr), summation(self.KLte)
def _arrange_kernel(self): Y = [1 if y == self.classes_[1] else -1 for y in self.Y] nn = len(Y) nk = self.n_kernels YY = spdiag(Y) eta = [1.0 / nk] * nk actual_weights = eta[:] actual_ratio = None Kc = summation(self.KL, eta) _r, alpha = radius(Kc) _m, gamma = margin(Kc, Y) self._ratios = [] cstep = self.step for i in xrange(self.max_iter): #print actual_ratio a = np.array( [1.0 - (alpha.T * matrix(K) * alpha)[0] for K in self.KL]) b = np.array([(gamma.T * YY * matrix(K) * YY * gamma)[0] for K in self.KL]) den = [np.dot(eta, b)**2] * nk num = [ sum([eta[s] * (a[s] * b[r] - a[r] * b[s]) for s in range(nk)]) for r in range(nk) ] eta = [cstep * (num[k] / den[k]) + eta[k] for k in range(nk)] eta = [max(0, v) for v in eta] eta = np.array(eta) / sum(eta) Kc = summation(self.KL, eta) _r, alpha = radius(Kc, init_sol=alpha) _m, gamma = margin(Kc, Y, init_sol=gamma) new_ratio = _r**2 / _m**2 if actual_ratio and abs(new_ratio - actual_ratio) / nn < self.tol: #completato #print i,'tol' self._ratios.append(new_ratio) actual_weights = eta #break; #!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! elif new_ratio <= actual_ratio or not actual_ratio: #tutto in regola actual_ratio = new_ratio self._ratios.append(actual_ratio) #print i,'update',actual_ratio actual_weights = eta else: #print i,'revert' #supero il minimo eta = actual_weights cstep /= 1.50 continue self._steps = i + 1 self.weights = np.array(eta) self.ker_matrix = summation(self.KL, self.weights) return self.ker_matrix
def predict_proba(K): return self.clf.predict_proba(summation(K,self.weights))
def decision_funcion(K): return self.clf.decision_function(summation(K,self.weights))
def arrange_kernel(K,Y): weights = [] for k in K: weights.append(self.beta0 + (self.beta1 * f(k,Y))** self.beta2) self.weights = np.linalg.norm(weights,1) return summation(K,self.weights)
def _arrange_kernel(self): Y = [1 if y == self.classes_[1] else -1 for y in self.Y] YY = spdiag(Y) Y = np.array(Y) nn = len(Y) nk = self.n_kernels idx_e = range(nn) np.random.seed(self.random_state) np.random.shuffle(idx_e) contexts = [{'idx' : idx_e[i::self.n_folds], 'alpha': None, 'gamma': None } for i in range(self.n_folds)] beta = [0.0] * nk mu = np.exp(np.array(beta) - max(beta)) mu /= mu.sum() Kc = summation(self.KL, mu) initial_ratio = [] for context in contexts: idx = context['idx'] context['alpha'], r2 = radius(Kc[idx][:, idx], lam=self.lam) context['gamma'], m2 = margin(Kc[idx][:, idx], Y[idx], lam=self.lam) initial_ratio.append((r2 / m2) / len(context['idx'])) initial_ratio = np.mean(initial_ratio) self._ratios = [initial_ratio] cstep = self.step self._converg = False self._steps = 0 while (not self._converg and (self._steps < self.max_iter)): self._steps += 1 new_beta = beta[:] new_ratio = [] for context in contexts: idx = context['idx'] new_beta = self.update_grad(Kc, YY, new_beta, context, cstep) new_mu = np.exp(new_beta - max(new_beta)) new_mu /= new_mu.sum() new_Kc = summation(self.KL, new_mu) try: new_alpha, r2 = radius(new_Kc[idx][:, idx], lam=self.lam, init_sol=context['alpha'].copy()) new_gamma, m2 = margin(new_Kc[idx][:, idx], Y[idx], lam=self.lam, init_sol=context['gamma'].copy()) except: new_alpha, r2 = radius(Kc[idx][:, idx], lam=self.lam) new_gamma, m2 = margin(Kc[idx][:, idx], Y[idx], lam=self.lam) new_ratio.append((r2 / m2) / len(idx)) new_ratio = np.mean(new_ratio) if not self._ratios or new_ratio < self._ratios[-1]: # or self._steps < 2: beta = new_beta[:] mu = np.exp(beta - max(beta)) mu /= mu.sum() Kc = new_Kc self._ratios.append(new_ratio) print self._steps, new_ratio, 'ok', cstep else: cstep /= 10.0 print self._steps, new_ratio, 'peggiorativo', self._ratios[-1], cstep if cstep < 1e-10: self._converg = True continue self.weights = np.array(mu) self.ker_matrix = summation(self.KL, self.weights) return self.ker_matrix
def decision_function(self, X): KL = self._check_test(X) return self.cls.decision_function(KL) if self.multiclass_ else self.base.decision_function(summation(KL, self.weights))