def kernel_weighted_degree_string_modular (fm_train_dna=traindat,fm_test_dna=testdat,degree=20): from shogun.Features import StringCharFeatures, DNA from shogun.Kernel import WeightedDegreeStringKernel, MSG_DEBUG feats_train=StringCharFeatures(fm_train_dna, DNA) #feats_train.io.set_loglevel(MSG_DEBUG) feats_test=StringCharFeatures(fm_test_dna, DNA) kernel=WeightedDegreeStringKernel(feats_train, feats_train, degree) from numpy import arange,double weights=arange(1,degree+1,dtype=double)[::-1]/ \ sum(arange(1,degree+1,dtype=double)) kernel.set_wd_weights(weights) #from numpy import ones,float64,int32 #kernel.set_position_weights(ones(len(fm_train_dna[0]), dtype=float64)) km_train=kernel.get_kernel_matrix() kernel.init(feats_train, feats_test) km_test=kernel.get_kernel_matrix() #this is how to serializate the kernel #import pickle #pickle.dump(kernel, file('kernel_obj.dump','w'), protocol=2) #k=pickle.load(file('kernel_obj.dump','r')) return km_train, km_test, kernel
def weighted_degree_string (): print 'WeightedDegreeString' from shogun.Features import StringCharFeatures, DNA from shogun.Kernel import WeightedDegreeStringKernel feats_train=StringCharFeatures(fm_train_dna, DNA) feats_test=StringCharFeatures(fm_test_dna, DNA) degree=20 kernel=WeightedDegreeStringKernel(feats_train, feats_train, degree) #weights=arange(1,degree+1,dtype=double)[::-1]/ \ # sum(arange(1,degree+1,dtype=double)) #kernel.set_wd_weights(weights) km_train=kernel.get_kernel_matrix() kernel.init(feats_train, feats_test) km_test=kernel.get_kernel_matrix()
normalizer = MultitaskKernelNormalizer(task_vector) #wdk.set_task_vector(task_vector) #, task_vector) for i in xrange(2): for j in xrange(2): if i==j: normalizer.set_task_similarity(i,j, 4.0) else: normalizer.set_task_similarity(i,j, 1.0) base_wdk.set_normalizer(normalizer) print base_wdk.get_kernel_matrix() print "--->",base_wdk.get_normalizer().get_name() wdk = WeightedDegreeStringKernel(feat, feat, 1) normalizer = IdentityKernelNormalizer() wdk.set_normalizer(normalizer) ################################################################## # external modification ################################################################## km = wdk.get_kernel_matrix() for i in xrange(N):
normalizer = MultitaskKernelNormalizer(task_vector) #wdk.set_task_vector(task_vector) #, task_vector) for i in xrange(2): for j in xrange(2): if i == j: normalizer.set_task_similarity(i, j, 4.0) else: normalizer.set_task_similarity(i, j, 1.0) base_wdk.set_normalizer(normalizer) print base_wdk.get_kernel_matrix() print "--->", base_wdk.get_normalizer().get_name() wdk = WeightedDegreeStringKernel(feat, feat, 1) normalizer = IdentityKernelNormalizer() wdk.set_normalizer(normalizer) ################################################################## # external modification ################################################################## km = wdk.get_kernel_matrix() for i in xrange(N): for j in xrange(N):
for j in xrange(N): external_objective += alphas_full[j] * f[j] for k in xrange(N): external_objective += 0.5 * alphas_full[j] * alphas_full[k] * y[j] * y[k] * wdk.kernel(j,k) print "svmlight external objective:", external_objective #precompute kernel matrix using shogun K = wdk.get_kernel_matrix() K = numpy.transpose(y.flatten() * (K*y.flatten()).transpose()) C = 1.0 # Important!! QP does not accept ndarray as a type, it must be an array p = QP(K, f, Aeq=y, beq=0, lb=numpy.zeros(N), ub=C*numpy.ones(N)) r = p.solve('cvxopt_qp', iprint = 0) print "cvxopt objective:", r.ff print "cvxopt alphas:", numpy.array(r.xf[0:5]) external_objective = 0.0 for j in xrange(N):