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


Python Extractor.all_exp_scores方法代码示例

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


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

示例1: __init__

# 需要导入模块: from extractor import Extractor [as 别名]
# 或者: from extractor.Extractor import all_exp_scores [as 别名]
class Parser:
    def __init__(self, model, temp, c):
        self.model = model
        self.extractor = Extractor(model)
        self.sampler = Sampler(model, self.extractor, temp, c) # model may be  redundant


    def decode(self, sent, num = 30):
        trees = defaultdict(int)
        exp_scores = self.extractor.all_exp_scores(sent)
        seed = self.sampler.random_tree(sent, exp_scores)
        optimal = seed
        slw = self.extractor.tree_local_weights(seed)
        sls = self.extractor.tree_score(slw)
        sgw = self.extractor.tree_global_weights(seed)
        sgs = self.extractor.tree_score(sgw)
        ss = sls + sgs

        for i in xrange(num): # or converge
            tree = self.sampler.propose(sent, seed, exp_scores)
            tlw = self.extractor.tree_local_weights(tree)
            tls = self.extractor.tree_score(tlw)
            tgw = self.extractor.tree_global_weights(tree)
            tgs = self.extractor.tree_score(tgw)
            ts = tls + tgs
            if ts > ss or random() < self.sampler.trans_prob(sgs, tgs):
                seed, ss = tree, ts
        return optimal


    def learn(self, instances, epochs = 10, num = 10):
        model = self.model
        sampler = self.sampler
        extractor = self.extractor
        for e in xrange(epochs):
            print 'iteration: %d' % e
            for i, sent in enumerate(instances):
                if i % 100 == 0:
                    print i
                gold = sent.get_gold_tree()
                gw = extractor.tree_local_weights(gold) + extractor.tree_global_weights(gold)

                seed = sampler.random_tree(sent)
                sw = extractor.tree_local_weights(seed) + extractor.tree_global_weights(seed)
                se = seed.error(gold)

                for i in xrange(num):
                    tree = sampler.propose(sent, seed)
                    tw = extractor.tree_local_weights(tree) + extractor.tree_global_weights(tree)
                    te = tree.error(gold)

                    gs = self.extractor.tree_score(gw)
                    ts = self.extractor.tree_score(tw)
                    ss = self.extractor.tree_score(sw)

                    
                    # C1
                    if gs - ts < te:
                        model.update(gw, tw)

                    # C2
                    if te < se: # tree > seed
                        if ts - ss < (se - te):
                            model.update(tw, sw) # se - te) margin
                    else: # tree <= seed
                        if ss - ts < (te - se):
                            model.update(sw, tw)

                    # accept or reject, should be probablistic, deterministic for now
                    if te < se:
                        seed, sw, se = tree, tw, te

                    model.qadd()
        model.average()
        # model.show()


    def decode_show(self, sent, num = 30000):

        o = open('samples.conll06', 'w')

        statistics = {d: {h: 0 for h in sent if h is not d} for d in sent[1:]}
        trees = defaultdict(int)

        exp_scores = self.extractor.all_exp_scores(sent)
        seed = self.sampler.random_tree(sent, exp_scores)
        optimal = seed
        slw = self.extractor.tree_local_weights(seed)
        sls = self.extractor.tree_score(slw)
        sgw = self.extractor.tree_global_weights(seed)
        sgs = self.extractor.tree_score(sgw)
        ss = sls + sgs

        for i in xrange(num): # or converge
            tree = self.sampler.propose(sent, seed, exp_scores)
            tlw = self.extractor.tree_local_weights(tree)
            tls = self.extractor.tree_score(tlw)
            tgw = self.extractor.tree_global_weights(tree)
            tgs = self.extractor.tree_score(tgw)
            ts = tls + tgs
#.........这里部分代码省略.........
开发者ID:EggplantElf,项目名称:mcmc_parser,代码行数:103,代码来源:parser.py


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