def kernel_director_linear_modular (fm_train_real=traindat,fm_test_real=testdat,scale=1.2):

	from shogun.Kernel import LinearKernel, AvgDiagKernelNormalizer
	from modshogun import Time

	feats_train=RealFeatures(fm_train_real)
	feats_train.io.set_loglevel(0)
	feats_train.parallel.set_num_threads(1)
	feats_test=RealFeatures(fm_test_real)
	 
	kernel=LinearKernel()
	kernel.set_normalizer(AvgDiagKernelNormalizer(scale))
	kernel.init(feats_train, feats_train)

	dkernel=DirectorLinearKernel()
	dkernel.set_normalizer(AvgDiagKernelNormalizer(scale))
	dkernel.init(feats_train, feats_train)

	print  "km_train"
	t=Time()
	km_train=kernel.get_kernel_matrix()
	t1=t.cur_time_diff(True)

	print  "dkm_train"
	t=Time()
	dkm_train=dkernel.get_kernel_matrix()
	t2=t.cur_time_diff(True)	

	print "km_train", km_train
	print "dkm_train", dkm_train

	return km_train, dkm_train
Example #2
0
def kernel_director_linear_modular(fm_train_real=traindat,
                                   fm_test_real=testdat,
                                   scale=1.2):

    from shogun.Kernel import LinearKernel, AvgDiagKernelNormalizer
    from modshogun import Time

    feats_train = RealFeatures(fm_train_real)
    feats_train.io.set_loglevel(0)
    feats_train.parallel.set_num_threads(1)
    feats_test = RealFeatures(fm_test_real)

    kernel = LinearKernel()
    kernel.set_normalizer(AvgDiagKernelNormalizer(scale))
    kernel.init(feats_train, feats_train)

    dkernel = DirectorLinearKernel()
    dkernel.set_normalizer(AvgDiagKernelNormalizer(scale))
    dkernel.init(feats_train, feats_train)

    print "km_train"
    t = Time()
    km_train = kernel.get_kernel_matrix()
    t1 = t.cur_time_diff(True)

    print "dkm_train"
    t = Time()
    dkm_train = dkernel.get_kernel_matrix()
    t2 = t.cur_time_diff(True)

    print "km_train", km_train
    print "dkm_train", dkm_train

    return km_train, dkm_train
def kernel_sparse_linear_modular(fm_train_real=traindat, fm_test_real=testdat, scale=1.1):
    from shogun.Features import SparseRealFeatures
    from shogun.Kernel import LinearKernel, AvgDiagKernelNormalizer

    feats_train = SparseRealFeatures(fm_train_real)
    feats_test = SparseRealFeatures(fm_test_real)

    kernel = LinearKernel()
    kernel.set_normalizer(AvgDiagKernelNormalizer(scale))
    kernel.init(feats_train, feats_train)
    km_train = kernel.get_kernel_matrix()

    kernel.init(feats_train, feats_test)
    km_test = kernel.get_kernel_matrix()
    return km_train, km_test, kernel
Example #4
0
def kernel_sparse_linear_modular (fm_train_real=traindat,fm_test_real=testdat,scale=1.1):
	from shogun.Features import SparseRealFeatures
	from shogun.Kernel import LinearKernel, AvgDiagKernelNormalizer

	feats_train=SparseRealFeatures(fm_train_real)
	feats_test=SparseRealFeatures(fm_test_real)

	kernel=LinearKernel()
	kernel.set_normalizer(AvgDiagKernelNormalizer(scale))
	kernel.init(feats_train, feats_train)
	km_train=kernel.get_kernel_matrix()

	kernel.init(feats_train, feats_test)
	km_test=kernel.get_kernel_matrix()
	return km_train,km_test,kernel
Example #5
0
def kernel_director_linear_modular(fm_train_real=traindat,
                                   fm_test_real=testdat,
                                   scale=1.2):
    try:
        from shogun.Kernel import DirectorKernel
    except ImportError:
        print "recompile shogun with --enable-swig-directors"
        return

    class DirectorLinearKernel(DirectorKernel):
        def __init__(self):
            DirectorKernel.__init__(self, True)

        def kernel_function(self, idx_a, idx_b):
            seq1 = self.get_lhs().get_feature_vector(idx_a)
            seq2 = self.get_rhs().get_feature_vector(idx_b)
            return numpy.dot(seq1, seq2)

    from shogun.Kernel import LinearKernel, AvgDiagKernelNormalizer
    from modshogun import Time

    feats_train = RealFeatures(fm_train_real)
    #feats_train.io.set_loglevel(MSG_DEBUG)
    feats_train.parallel.set_num_threads(1)
    feats_test = RealFeatures(fm_test_real)

    kernel = LinearKernel()
    kernel.set_normalizer(AvgDiagKernelNormalizer(scale))
    kernel.init(feats_train, feats_train)

    dkernel = DirectorLinearKernel()
    dkernel.set_normalizer(AvgDiagKernelNormalizer(scale))
    dkernel.init(feats_train, feats_train)

    #print  "km_train"
    t = Time()
    km_train = kernel.get_kernel_matrix()
    #t1=t.cur_time_diff(True)

    #print  "dkm_train"
    t = Time()
    dkm_train = dkernel.get_kernel_matrix()
    #t2=t.cur_time_diff(True)

    #print "km_train", km_train
    #print "dkm_train", dkm_train

    return km_train, dkm_train
def kernel_linear_word_modular(fm_train_word=traindat, fm_test_word=testdat, scale=1.2):

    from shogun.Kernel import LinearKernel, AvgDiagKernelNormalizer
    from shogun.Features import WordFeatures

    feats_train = WordFeatures(fm_train_word)
    feats_test = WordFeatures(fm_test_word)

    kernel = LinearKernel(feats_train, feats_train)
    kernel.set_normalizer(AvgDiagKernelNormalizer(scale))
    kernel.init(feats_train, feats_train)

    km_train = kernel.get_kernel_matrix()
    kernel.init(feats_train, feats_test)
    km_test = kernel.get_kernel_matrix()
    return kernel
Example #7
0
def kernel_linear_word_modular (fm_train_word=traindat,fm_test_word=testdat,scale=1.2):
	
	from shogun.Kernel import LinearKernel, AvgDiagKernelNormalizer
	from shogun.Features import WordFeatures

	feats_train=WordFeatures(fm_train_word)
	feats_test=WordFeatures(fm_test_word)

	kernel=LinearKernel(feats_train, feats_train)
	kernel.set_normalizer(AvgDiagKernelNormalizer(scale))
	kernel.init(feats_train, feats_train)

	km_train=kernel.get_kernel_matrix()
	kernel.init(feats_train, feats_test)
	km_test=kernel.get_kernel_matrix()
	return kernel
def linear ():
	print 'Linear'
	from shogun.Features import RealFeatures
	from shogun.Kernel import LinearKernel, AvgDiagKernelNormalizer

	feats_train=RealFeatures(fm_train_real)
	feats_test=RealFeatures(fm_test_real)
	scale=1.2

	kernel=LinearKernel()
	kernel.set_normalizer(AvgDiagKernelNormalizer(scale))
	kernel.init(feats_train, feats_train)

	km_train=kernel.get_kernel_matrix()
	kernel.init(feats_train, feats_test)
	km_test=kernel.get_kernel_matrix()
def kernel_director_linear_modular (fm_train_real=traindat,fm_test_real=testdat,scale=1.2):
	try:
		from shogun.Kernel import DirectorKernel
	except ImportError:
		print "recompile shogun with --enable-swig-directors"
		return

	class DirectorLinearKernel(DirectorKernel):
		def __init__(self):
			DirectorKernel.__init__(self, True)
		def kernel_function(self, idx_a, idx_b):
			seq1 = self.get_lhs().get_feature_vector(idx_a)
			seq2 = self.get_rhs().get_feature_vector(idx_b)
			return numpy.dot(seq1, seq2)


	from shogun.Kernel import LinearKernel, AvgDiagKernelNormalizer
	from modshogun import Time

	feats_train=RealFeatures(fm_train_real)
	#feats_train.io.set_loglevel(MSG_DEBUG)
	feats_train.parallel.set_num_threads(1)
	feats_test=RealFeatures(fm_test_real)
	 
	kernel=LinearKernel()
	kernel.set_normalizer(AvgDiagKernelNormalizer(scale))
	kernel.init(feats_train, feats_train)

	dkernel=DirectorLinearKernel()
	dkernel.set_normalizer(AvgDiagKernelNormalizer(scale))
	dkernel.init(feats_train, feats_train)

	#print  "km_train"
	t=Time()
	km_train=kernel.get_kernel_matrix()
	#t1=t.cur_time_diff(True)

	#print  "dkm_train"
	t=Time()
	dkm_train=dkernel.get_kernel_matrix()
	#t2=t.cur_time_diff(True)

	#print "km_train", km_train
	#print "dkm_train", dkm_train

	return km_train, dkm_train