From f8004c5f6561894f900fa706b6d62ce1c861f5ef Mon Sep 17 00:00:00 2001 From: Matthew Honnibal Date: Thu, 5 Nov 2015 03:53:03 +1100 Subject: [PATCH] * Begin upgrading to improved thinc API --- spacy/_ml.pxd | 10 ++------ spacy/_ml.pyx | 64 ++++++++++++++------------------------------------- 2 files changed, 19 insertions(+), 55 deletions(-) diff --git a/spacy/_ml.pxd b/spacy/_ml.pxd index b9a190b67..59e5c3fef 100644 --- a/spacy/_ml.pxd +++ b/spacy/_ml.pxd @@ -5,20 +5,13 @@ from cymem.cymem cimport Pool from thinc.learner cimport LinearModel from thinc.features cimport Extractor, Feature from thinc.typedefs cimport atom_t, feat_t, weight_t, class_t -from thinc.api cimport ExampleC +from thinc.api cimport Example, ExampleC from preshed.maps cimport PreshMapArray from .typedefs cimport hash_t -cdef int arg_max(const weight_t* scores, const int n_classes) nogil - -cdef int arg_max_if_true(const weight_t* scores, const int* is_valid, int n_classes) nogil - -cdef int arg_max_if_zero(const weight_t* scores, const int* costs, int n_classes) nogil - - cdef class Model: cdef readonly int n_classes cdef readonly int n_feats @@ -31,4 +24,5 @@ cdef class Model: cdef object model_loc cdef object _templates cdef Extractor _extractor + cdef Example _eg cdef LinearModel _model diff --git a/spacy/_ml.pyx b/spacy/_ml.pyx index 2f27badbf..2fc7b646c 100644 --- a/spacy/_ml.pyx +++ b/spacy/_ml.pyx @@ -13,40 +13,7 @@ import numpy.random from thinc.features cimport Feature, count_feats from thinc.api cimport Example - -cdef int arg_max(const weight_t* scores, const int n_classes) nogil: - cdef int i - cdef int best = 0 - cdef weight_t mode = scores[0] - for i in range(1, n_classes): - if scores[i] > mode: - mode = scores[i] - best = i - return best - - -cdef int arg_max_if_true(const weight_t* scores, const int* is_valid, - const int n_classes) nogil: - cdef int i - cdef int best = 0 - cdef weight_t mode = -900000 - for i in range(n_classes): - if is_valid[i] and scores[i] > mode: - mode = scores[i] - best = i - return best - - -cdef int arg_max_if_zero(const weight_t* scores, const int* costs, - const int n_classes) nogil: - cdef int i - cdef int best = 0 - cdef weight_t mode = -900000 - for i in range(n_classes): - if costs[i] == 0 and scores[i] > mode: - mode = scores[i] - best = i - return best +from thinc.learner cimport arg_max, arg_max_if_true, arg_max_if_zero cdef class Model: @@ -54,10 +21,12 @@ cdef class Model: if model_loc is not None and path.isdir(model_loc): model_loc = path.join(model_loc, 'model') self._templates = templates + n_atoms = max([max(templ) for templ in templates]) + 1 self.n_classes = n_classes self._extractor = Extractor(templates) self.n_feats = self._extractor.n_templ - self._model = LinearModel(n_classes, self._extractor.n_templ) + self._model = LinearModel(n_classes, self._extractor) + self._eg = Example(n_classes, n_atoms, self._extractor.n_templ, self._extractor.n_templ) self.model_loc = model_loc if self.model_loc and path.exists(self.model_loc): self._model.load(self.model_loc, freq_thresh=0) @@ -75,24 +44,25 @@ cdef class Model: None, None) def predict(self, Example eg): - self.set_scores(eg.c.scores, eg.c.atoms) - eg.c.guess = arg_max_if_true(eg.c.scores, eg.c.is_valid, self.n_classes) + self._model(eg) def train(self, Example eg): - self.predict(eg) - eg.c.best = arg_max_if_zero(eg.c.scores, eg.c.costs, self.n_classes) - eg.c.cost = eg.c.costs[eg.c.guess] - self.update(eg.c.atoms, eg.c.guess, eg.c.best, eg.c.cost) + self._model.train(eg) cdef const weight_t* score(self, atom_t* context) except NULL: - cdef int n_feats - feats = self._extractor.get_feats(context, &n_feats) - return self._model.get_scores(feats, n_feats) + memcpy(self._eg.c.atoms, context, self._eg.c.nr_atom * sizeof(context[0])) + self._model(self._eg) + return self._eg.scores cdef int set_scores(self, weight_t* scores, atom_t* context) nogil: - cdef int n_feats - feats = self._extractor.get_feats(context, &n_feats) - self._model.set_scores(scores, feats, n_feats) + cdef int nr_feat = self._model.extractor.set_feats(self._eg.features, context) + + self._model.set_scores( + scores, + self._model.weights.c_map, + self._eg.c.features, + nr_feat + ) cdef int update(self, atom_t* context, class_t guess, class_t gold, int cost) except -1: cdef int n_feats