Exemplo n.º 1
0
def kernel_evaluation(K):
    kernel_results_dict = {}
    K = kernel_normalization(K)  # normalize the kernel K (useless in the case of HPK computed on normalized data)
    kernel_results_dict['score_margin'] = margin(K,
                                                 Ytr)  # the distance between the positive and negative classes in the kernel space
    kernel_results_dict['score_radius'] = radius(
        K)  # the radius of the Einimum Enclosing Ball containing data in the kernel space
    kernel_results_dict['score_ratio'] = ratio(K,
                                               Ytr)  # the radius/margin ratio defined as (radius**2/margin**2)/n_examples
    kernel_results_dict['score_froben'] = frobenius(K)  # the Frobenius norm of a kernel matrix
    kernel_results_dict['score_trace'] = trace(K)  # the trace of the kernel matrix
    return kernel_results_dict
Exemplo n.º 2
0
Arquivo: RMKL.py Projeto: sspeng/MKLpy
    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)
Exemplo n.º 3
0
print ('computing Homogeneous Polynomial Kernels...', end='')
from MKLpy.metrics import pairwise
KLtr = [pairwise.homogeneous_polynomial_kernel(Xtr, degree=d) for d in range(11)]
KLte = [pairwise.homogeneous_polynomial_kernel(Xte,Xtr, degree=d) for d in range(11)]
print ('done')


#evaluate kernels in terms of margin, radius etc...
print ('evaluating metrics...', end='')
from MKLpy.metrics import margin, radius, ratio, trace, frobenius
from MKLpy.preprocessing import kernel_normalization
deg = 5
K = KLtr[deg]					#the HPK with degree 5
K = kernel_normalization(K)		#normalize the kernel K (useless in the case of HPK computed on normalized data)
score_margin = margin(K,Ytr)	#the distance between the positive and negative classes in the kernel space
score_radius = radius(K)		#the radius of the Einimum Enclosing Ball containing data in the kernel space
score_ratio  = ratio (K,Ytr)	#the radius/margin ratio defined as (radius**2/margin**2)/n_examples
#the ratio can be also computed as score_radius**2/score_margin**2/len(Ytr)
score_trace  = trace (K)		#the trace of the kernel matrix
score_froben = frobenius(K)		#the Frobenius norm of a kernel matrix
print ('done')
print ('results of the %d-degree HP kernel:' % deg)
print ('margin: %.4f, radius: %.4f, radiu-margin ratio: %.4f,' % (score_margin, score_radius, score_ratio))
print ('trace: %.4f, frobenius norm: %.4f' % (score_trace, score_froben))


#evaluate the empirical complexity of the kernel matrix, i.e. the Spectral Ratio
# Michele Donini, Fabio Aiolli: "Learning deep kernels in the space of dot-product polynomials". Machine Learning (2017)
# Ivano Lauriola, Mirko Polato, Fabio Aiolli: "The Minimum Effort Maximum Output principle applied to Multiple Kernel Learning". ESANN (2018)
print ('computing Spectral Ratio...', end='')
from MKLpy.metrics import spectral_ratio
Exemplo n.º 4
0
 def test_radius(self):
     self.assertRaises(SquaredKernelError, metrics.radius, self.X)
     self.assertAlmostEqual(metrics.radius(self.K), 5**.5 / 2, 5)