km = wdk.get_kernel_matrix()

for i in xrange(N):
    for j in xrange(N):
        km[i,j] = km[i,j]*relate_tasks(i,j)
        #km = km*1.0

print km
#precompute kernel matrix using shogun
y = numpy.array(labels)
K = numpy.transpose(y.flatten() * (km*y.flatten()).transpose())
f = -numpy.ones(N)
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 "externally modified kernel. objective:", r.ff

ck = CustomKernel()
ck.set_full_kernel_matrix_from_full(km)
#
svm = LibSVM(1, ck, lab)
svm.train()

print "externally modified kernel. objective:", svm.get_objective()


Ejemplo n.º 2
0

feat_presvm = StringCharFeatures(DNA)
feat_presvm.set_features(examples_presvm)
wdk_presvm = WeightedDegreeStringKernel(feat_presvm, feat_presvm, 1)
lab_presvm = Labels(numpy.array(labels_presvm))


presvm = SVMLight(1, wdk_presvm, lab_presvm)
presvm.train()

presvm2 = LibSVM(1, wdk_presvm, lab_presvm)
presvm2.train()

print "svmlight", presvm.get_objective()
print "libsvm", presvm2.get_objective()

assert(abs(presvm.get_objective() - presvm2.get_objective())<= 0.001)

print "simple svm", presvm.get_objective()

print "len(examples_presvm)", len(examples_presvm)

print "##############"


#############################################
#    compute linear term manually
#############################################

examples = [i.example for i in d[subset_size:subset_size*2]]
##################################################################

km = wdk.get_kernel_matrix()

for i in xrange(N):
    for j in xrange(N):
        km[i, j] = km[i, j] * relate_tasks(i, j)
        #km = km*1.0

print km
#precompute kernel matrix using shogun
y = numpy.array(labels)
K = numpy.transpose(y.flatten() * (km * y.flatten()).transpose())
f = -numpy.ones(N)
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 "externally modified kernel. objective:", r.ff

ck = CustomKernel()
ck.set_full_kernel_matrix_from_full(km)
#
svm = LibSVM(1, ck, lab)
svm.train()

print "externally modified kernel. objective:", svm.get_objective()
Ejemplo n.º 4
0
labels_presvm[8] = 1
labels_presvm[19] = 1

feat_presvm = StringCharFeatures(DNA)
feat_presvm.set_features(examples_presvm)
wdk_presvm = WeightedDegreeStringKernel(feat_presvm, feat_presvm, 1)
lab_presvm = Labels(numpy.array(labels_presvm))

presvm = SVMLight(1, wdk_presvm, lab_presvm)
presvm.train()

presvm2 = LibSVM(1, wdk_presvm, lab_presvm)
presvm2.train()

print "svmlight", presvm.get_objective()
print "libsvm", presvm2.get_objective()

assert (abs(presvm.get_objective() - presvm2.get_objective()) <= 0.001)

print "simple svm", presvm.get_objective()

print "len(examples_presvm)", len(examples_presvm)

print "##############"

#############################################
#    compute linear term manually
#############################################

examples = [i.example for i in d[subset_size:subset_size * 2]]
labels = [i.label for i in d[subset_size:subset_size * 2]]