本文整理汇总了Python中extractor.Extractor.tree_score方法的典型用法代码示例。如果您正苦于以下问题:Python Extractor.tree_score方法的具体用法?Python Extractor.tree_score怎么用?Python Extractor.tree_score使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类extractor.Extractor
的用法示例。
在下文中一共展示了Extractor.tree_score方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from extractor import Extractor [as 别名]
# 或者: from extractor.Extractor import tree_score [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
#.........这里部分代码省略.........