當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。