mirror of
https://github.com/explosion/spaCy.git
synced 2025-05-30 02:33:07 +03:00
* Begin upgrading to improved thinc API
This commit is contained in:
parent
adc7bbd6cf
commit
f8004c5f65
|
@ -5,20 +5,13 @@ from cymem.cymem cimport Pool
|
||||||
from thinc.learner cimport LinearModel
|
from thinc.learner cimport LinearModel
|
||||||
from thinc.features cimport Extractor, Feature
|
from thinc.features cimport Extractor, Feature
|
||||||
from thinc.typedefs cimport atom_t, feat_t, weight_t, class_t
|
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 preshed.maps cimport PreshMapArray
|
||||||
|
|
||||||
from .typedefs cimport hash_t
|
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 class Model:
|
||||||
cdef readonly int n_classes
|
cdef readonly int n_classes
|
||||||
cdef readonly int n_feats
|
cdef readonly int n_feats
|
||||||
|
@ -31,4 +24,5 @@ cdef class Model:
|
||||||
cdef object model_loc
|
cdef object model_loc
|
||||||
cdef object _templates
|
cdef object _templates
|
||||||
cdef Extractor _extractor
|
cdef Extractor _extractor
|
||||||
|
cdef Example _eg
|
||||||
cdef LinearModel _model
|
cdef LinearModel _model
|
||||||
|
|
|
@ -13,40 +13,7 @@ import numpy.random
|
||||||
from thinc.features cimport Feature, count_feats
|
from thinc.features cimport Feature, count_feats
|
||||||
from thinc.api cimport Example
|
from thinc.api cimport Example
|
||||||
|
|
||||||
|
from thinc.learner cimport arg_max, arg_max_if_true, arg_max_if_zero
|
||||||
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
|
|
||||||
|
|
||||||
|
|
||||||
cdef class Model:
|
cdef class Model:
|
||||||
|
@ -54,10 +21,12 @@ cdef class Model:
|
||||||
if model_loc is not None and path.isdir(model_loc):
|
if model_loc is not None and path.isdir(model_loc):
|
||||||
model_loc = path.join(model_loc, 'model')
|
model_loc = path.join(model_loc, 'model')
|
||||||
self._templates = templates
|
self._templates = templates
|
||||||
|
n_atoms = max([max(templ) for templ in templates]) + 1
|
||||||
self.n_classes = n_classes
|
self.n_classes = n_classes
|
||||||
self._extractor = Extractor(templates)
|
self._extractor = Extractor(templates)
|
||||||
self.n_feats = self._extractor.n_templ
|
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
|
self.model_loc = model_loc
|
||||||
if self.model_loc and path.exists(self.model_loc):
|
if self.model_loc and path.exists(self.model_loc):
|
||||||
self._model.load(self.model_loc, freq_thresh=0)
|
self._model.load(self.model_loc, freq_thresh=0)
|
||||||
|
@ -75,24 +44,25 @@ cdef class Model:
|
||||||
None, None)
|
None, None)
|
||||||
|
|
||||||
def predict(self, Example eg):
|
def predict(self, Example eg):
|
||||||
self.set_scores(eg.c.scores, eg.c.atoms)
|
self._model(eg)
|
||||||
eg.c.guess = arg_max_if_true(eg.c.scores, eg.c.is_valid, self.n_classes)
|
|
||||||
|
|
||||||
def train(self, Example eg):
|
def train(self, Example eg):
|
||||||
self.predict(eg)
|
self._model.train(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)
|
|
||||||
|
|
||||||
cdef const weight_t* score(self, atom_t* context) except NULL:
|
cdef const weight_t* score(self, atom_t* context) except NULL:
|
||||||
cdef int n_feats
|
memcpy(self._eg.c.atoms, context, self._eg.c.nr_atom * sizeof(context[0]))
|
||||||
feats = self._extractor.get_feats(context, &n_feats)
|
self._model(self._eg)
|
||||||
return self._model.get_scores(feats, n_feats)
|
return self._eg.scores
|
||||||
|
|
||||||
cdef int set_scores(self, weight_t* scores, atom_t* context) nogil:
|
cdef int set_scores(self, weight_t* scores, atom_t* context) nogil:
|
||||||
cdef int n_feats
|
cdef int nr_feat = self._model.extractor.set_feats(self._eg.features, context)
|
||||||
feats = self._extractor.get_feats(context, &n_feats)
|
|
||||||
self._model.set_scores(scores, feats, n_feats)
|
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 update(self, atom_t* context, class_t guess, class_t gold, int cost) except -1:
|
||||||
cdef int n_feats
|
cdef int n_feats
|
||||||
|
|
Loading…
Reference in New Issue
Block a user