* Clean up 'guess_cache' idea, which didnt work well enough

This commit is contained in:
Matthew Honnibal 2014-12-20 03:48:51 +11:00
parent 9d3ca13909
commit ff252dd535
2 changed files with 4 additions and 28 deletions

View File

@ -6,7 +6,6 @@ from .arc_eager cimport TransitionSystem
from ..tokens cimport Tokens, TokenC from ..tokens cimport Tokens, TokenC
from ._state cimport State from ._state cimport State
from ..index cimport DecisionMemory
cdef class GreedyParser: cdef class GreedyParser:
@ -14,6 +13,5 @@ cdef class GreedyParser:
cdef Extractor extractor cdef Extractor extractor
cdef readonly LinearModel model cdef readonly LinearModel model
cdef TransitionSystem moves cdef TransitionSystem moves
cdef readonly DecisionMemory guess_cache
cpdef int parse(self, Tokens tokens) except -1 cpdef int parse(self, Tokens tokens) except -1

View File

@ -65,15 +65,8 @@ cdef class GreedyParser:
self.extractor = Extractor(get_templates(self.cfg.features)) self.extractor = Extractor(get_templates(self.cfg.features))
self.moves = TransitionSystem(self.cfg.left_labels, self.cfg.right_labels) self.moves = TransitionSystem(self.cfg.left_labels, self.cfg.right_labels)
self.model = LinearModel(self.moves.n_moves, self.extractor.n_templ) self.model = LinearModel(self.moves.n_moves, self.extractor.n_templ)
# Classes for decision memory
classes = ['S', 'D']
classes += ['L-%s' % label for label in self.cfg.left_labels]
classes += ['R-%s' % label for label in self.cfg.right_labels]
self.guess_cache = DecisionMemory(classes)
if os.path.exists(pjoin(model_dir, 'model')): if os.path.exists(pjoin(model_dir, 'model')):
self.model.load(pjoin(model_dir, 'model')) self.model.load(pjoin(model_dir, 'model'))
if os.path.exists(pjoin(model_dir, 'guess_cache')):
self.guess_cache.load(pjoin(model_dir, 'guess_cache'))
cpdef int parse(self, Tokens tokens) except -1: cpdef int parse(self, Tokens tokens) except -1:
cdef: cdef:
@ -86,18 +79,11 @@ cdef class GreedyParser:
cdef int n_feats cdef int n_feats
cdef Pool mem = Pool() cdef Pool mem = Pool()
cdef State* state = init_state(mem, tokens.data, tokens.length) cdef State* state = init_state(mem, tokens.data, tokens.length)
cdef int guess_clas
while not is_final(state): while not is_final(state):
state_key = _approx_hash_state(state)
guess_clas = self.guess_cache.get(state_key)
if guess_clas == -1:
fill_context(context, state) fill_context(context, state)
feats = self.extractor.get_feats(context, &n_feats) feats = self.extractor.get_feats(context, &n_feats)
scores = self.model.get_scores(feats, n_feats) scores = self.model.get_scores(feats, n_feats)
guess = self.moves.best_valid(scores, state) guess = self.moves.best_valid(scores, state)
self.guess_cache.inc(state_key, guess.clas, 1)
else:
guess = self.moves._moves[guess_clas]
self.moves.transition(state, &guess) self.moves.transition(state, &guess)
return 0 return 0
@ -134,14 +120,6 @@ cdef class GreedyParser:
return n_corr return n_corr
cdef inline uint64_t _approx_hash_state(const State* state) nogil:
cdef int[3] context
context[0] = get_s0(state).lex.sic
context[1] = get_n0(state).lex.sic
context[2] = get_n1(state).pos if state.i < (state.sent_len - 1) else 0
return hash64(context, sizeof(int) * 3, 0)
cdef dict _get_counts(int guess, int best, const Feature* feats, const int n_feats, cdef dict _get_counts(int guess, int best, const Feature* feats, const int n_feats,
int inc): int inc):
if guess == best: if guess == best: