From c96d76983626ed1edcd4f513318469b7b7e6a191 Mon Sep 17 00:00:00 2001 From: Matthew Honnibal Date: Sat, 12 Aug 2017 18:21:54 -0500 Subject: [PATCH] Fix beam parse. Not sure if working --- spacy/syntax/_beam_utils.pyx | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/spacy/syntax/_beam_utils.pyx b/spacy/syntax/_beam_utils.pyx index 10b5e407c..3fcd322e2 100644 --- a/spacy/syntax/_beam_utils.pyx +++ b/spacy/syntax/_beam_utils.pyx @@ -87,8 +87,9 @@ cdef class ParserBeam(object): def _set_scores(self, Beam beam, scores): for i in range(beam.size): state = beam.at(i) - for j in range(beam.nr_class): - beam.scores[i][j] = scores[i, j] + if not state.is_final(): + for j in range(beam.nr_class): + beam.scores[i][j] = scores[i, j] self.moves.set_valid(beam.is_valid[i], state.c) def _set_costs(self, Beam beam, GoldParse gold, int follow_gold=False): @@ -137,8 +138,8 @@ def update_beam(TransitionSystem moves, int nr_feature, int max_steps, backprops.append((token_ids, bp_vectors, bp_scores)) - p_scores = [scores[indices] for indices in p_indices] - g_scores = [scores[indices] for indices in g_indices] + p_scores = [numpy.ascontiguousarray(scores[indices], dtype='f') for indices in p_indices] + g_scores = [numpy.ascontiguousarray(scores[indices], dtype='f') for indices in g_indices] pbeam.advance(p_scores) gbeam.advance(g_scores, follow_gold=True) @@ -176,8 +177,8 @@ def get_states(pbeams, gbeams, beam_map): beam_map[key] = len(states) states.append(gbeam.at(i)) - p_indices = numpy.asarray(p_indices, dtype='i') - g_indices = numpy.asarray(g_indices, dtype='i') + p_indices = [numpy.asarray(idx, dtype='i') for idx in p_indices] + g_indices = [numpy.asarray(idx, dtype='i') for idx in g_indices] return states, p_indices, g_indices @@ -203,7 +204,9 @@ def get_gradient(nr_class, beam_maps, histories, losses): key = tuple([eg_id]) for j, clas in enumerate(hist): i = beam_maps[j][key] - grads[j][i, clas] = loss + # In step j, at state i action clas + # resulted in loss + grads[j][i, clas] += loss key = key + tuple([clas]) return grads