当前位置: 首页>>代码示例>>Python>>正文


Python SVMLight.get_alphas方法代码示例

本文整理汇总了Python中shogun.Classifier.SVMLight.get_alphas方法的典型用法代码示例。如果您正苦于以下问题:Python SVMLight.get_alphas方法的具体用法?Python SVMLight.get_alphas怎么用?Python SVMLight.get_alphas使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在shogun.Classifier.SVMLight的用法示例。


在下文中一共展示了SVMLight.get_alphas方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_data

# 需要导入模块: from shogun.Classifier import SVMLight [as 别名]
# 或者: from shogun.Classifier.SVMLight import get_alphas [as 别名]

#.........这里部分代码省略.........
    taxonomy = shogun_factory.create_taxonomy(mss.taxonomy.data)
    
    
    support = numpy.linspace(0, 100, 4)
    
    
    distances = [[0, 1, 2, 2], [1, 0, 2, 2], [2, 2, 0, 1], [2, 2, 1, 0]]
    
    # create tree normalizer 
    tree_normalizer = MultitaskKernelPlifNormalizer(support, data.task_vector_names)
    
    
    
    
    task_names = data.get_task_names()
    
    
    FACTOR = 1.0
    
    
    # init gamma matrix
    gammas = numpy.zeros((data.get_num_tasks(), data.get_num_tasks()))
    
    for t1_name in task_names:
        for t2_name in task_names:
            
            similarity = taxonomy.compute_node_similarity(taxonomy.get_id(t1_name), taxonomy.get_id(t2_name))        
            gammas[data.name_to_id(t1_name), data.name_to_id(t2_name)] = similarity
    
    helper.save("/tmp/gammas", gammas)
    
    
    gammas = gammas * FACTOR
    
    cost = param.cost * numpy.sqrt(FACTOR) 
    
    print gammas
    
    
    ##########
    # regular normalizer
    
    normalizer = MultitaskKernelNormalizer(data.task_vector_nums)
    
    for t1_name in task_names:
        for t2_name in task_names:
                    
            similarity = gammas[data.name_to_id(t1_name), data.name_to_id(t2_name)]
            normalizer.set_task_similarity(data.name_to_id(t1_name), data.name_to_id(t2_name), similarity)
    
                
    ##################################################################
    # Train SVMs
    ##################################################################
    
    # create shogun objects
    wdk_tree = shogun_factory.create_kernel(data.examples, param)
    lab = shogun_factory.create_labels(data.labels)
    
    wdk_tree.set_normalizer(tree_normalizer)
    wdk_tree.init_normalizer()
    
    print "--->",wdk_tree.get_normalizer().get_name()
    
    svm_tree = SVMLight(cost, wdk_tree, lab)
    svm_tree.set_linadd_enabled(False)
    svm_tree.set_batch_computation_enabled(False)
    
    svm_tree.train()
    
    del wdk_tree
    del tree_normalizer
    
    print "finished training tree-norm SVM:", svm_tree.get_objective()
    
    
    wdk = shogun_factory.create_kernel(data.examples, param)
    wdk.set_normalizer(normalizer)
    wdk.init_normalizer()
    
    print "--->",wdk.get_normalizer().get_name()
    
    svm = SVMLight(cost, wdk, lab)
    svm.set_linadd_enabled(False)
    svm.set_batch_computation_enabled(False)
    
    svm.train()
    
    print "finished training manually set SVM:", svm.get_objective()
    
    
    alphas_tree = svm_tree.get_alphas()
    alphas = svm.get_alphas()
    
    assert(len(alphas_tree)==len(alphas))
    
    for i in xrange(len(alphas)):
        assert(abs(alphas_tree[i] - alphas[i]) < 0.0001)
        
    print "success: all alphas are the same"
开发者ID:cwidmer,项目名称:multitask,代码行数:104,代码来源:debug_multitask_kernel_plif.py

示例2: SVMLight

# 需要导入模块: from shogun.Classifier import SVMLight [as 别名]
# 或者: from shogun.Classifier.SVMLight import get_alphas [as 别名]
del tree_normalizer

print "finished training tree-norm SVM:", svm_tree.get_objective()


wdk = shogun_factory.create_kernel(data.examples, param)
wdk.set_normalizer(normalizer)
wdk.init_normalizer()

print "--->",wdk.get_normalizer().get_name()

svm = SVMLight(cost, wdk, lab)
svm.set_linadd_enabled(False)
svm.set_batch_computation_enabled(False)

svm.train()

print "finished training manually set SVM:", svm.get_objective()


alphas_tree = svm_tree.get_alphas()
alphas = svm.get_alphas()

assert(len(alphas_tree)==len(alphas))

for i in xrange(len(alphas)):
    assert(abs(alphas_tree[i] - alphas[i]) < 0.0001)
    
print "success: all alphas are the same"

开发者ID:cwidmer,项目名称:multitask,代码行数:31,代码来源:debug_multitask_kernel_tree.py

示例3: xrange

# 需要导入模块: from shogun.Classifier import SVMLight [as 别名]
# 或者: from shogun.Classifier.SVMLight import get_alphas [as 别名]
for idx in xrange(10):

    #f = (-numpy.ones(N)-2)*numpy.random.randn()


    print "############################"
    print "############################"
    print ""
    print "f:", f
    print "\n"

    svm.set_linear_term(numpy.double(f))
    svm.train()

    sv_idx = svm.get_support_vectors()
    alphas = svm.get_alphas()

    alphas_full = numpy.zeros(N)
    alphas_full[sv_idx] = alphas

    alphas_full = alphas_full * y

    print "svmlight objective:", svm.get_objective()
    print "svmlight alphas:", numpy.array(alphas_full[0:5])

    external_objective = 0.0

    for j in xrange(N):

        external_objective += alphas_full[j] * f[j]
开发者ID:cwidmer,项目名称:multitask,代码行数:32,代码来源:debug_shogun_trunk.py


注:本文中的shogun.Classifier.SVMLight.get_alphas方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。