2015-06-02 16:37:10 +00:00
|
|
|
# cython: profile=True
|
2014-12-30 10:20:15 +00:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
from __future__ import division
|
|
|
|
|
|
|
|
from os import path
|
2015-10-12 08:33:11 +00:00
|
|
|
import tempfile
|
2014-12-30 10:20:15 +00:00
|
|
|
import os
|
|
|
|
import shutil
|
|
|
|
import json
|
|
|
|
import cython
|
2015-05-26 20:17:15 +00:00
|
|
|
import numpy.random
|
2014-12-30 10:20:15 +00:00
|
|
|
|
|
|
|
from thinc.features cimport Feature, count_feats
|
2015-06-26 04:25:36 +00:00
|
|
|
from thinc.api cimport Example
|
2014-12-30 10:20:15 +00:00
|
|
|
|
|
|
|
|
2014-12-31 08:40:59 +00:00
|
|
|
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
|
2014-12-30 10:20:15 +00:00
|
|
|
|
|
|
|
|
2015-06-26 11:51:39 +00:00
|
|
|
cdef int arg_max_if_true(const weight_t* scores, const int* is_valid,
|
2015-06-26 04:25:36 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
|
2014-12-30 10:20:15 +00:00
|
|
|
cdef class Model:
|
2014-12-30 14:16:47 +00:00
|
|
|
def __init__(self, n_classes, templates, model_loc=None):
|
2014-12-31 08:40:59 +00:00
|
|
|
if model_loc is not None and path.isdir(model_loc):
|
|
|
|
model_loc = path.join(model_loc, 'model')
|
2015-10-12 08:33:11 +00:00
|
|
|
self._templates = templates
|
2014-12-31 08:40:59 +00:00
|
|
|
self.n_classes = n_classes
|
2014-12-30 10:20:15 +00:00
|
|
|
self._extractor = Extractor(templates)
|
2015-06-26 11:51:39 +00:00
|
|
|
self.n_feats = self._extractor.n_templ
|
2014-12-30 10:20:15 +00:00
|
|
|
self._model = LinearModel(n_classes, self._extractor.n_templ)
|
2014-12-30 14:16:47 +00:00
|
|
|
self.model_loc = model_loc
|
2014-12-30 10:20:15 +00:00
|
|
|
if self.model_loc and path.exists(self.model_loc):
|
|
|
|
self._model.load(self.model_loc, freq_thresh=0)
|
|
|
|
|
2015-10-12 08:33:11 +00:00
|
|
|
def __reduce__(self):
|
2015-10-13 04:10:04 +00:00
|
|
|
_, model_loc = tempfile.mkstemp()
|
2015-10-12 08:33:11 +00:00
|
|
|
# TODO: This is a potentially buggy implementation. We're not really
|
|
|
|
# given a good guarantee that all internal state is saved correctly here,
|
|
|
|
# since there are learning parameters for e.g. the model averaging in
|
|
|
|
# averaged perceptron, the gradient calculations in AdaGrad, etc
|
|
|
|
# that aren't necessarily saved. So, if we're part way through training
|
|
|
|
# the model, and then we pickle it, we won't recover the state correctly.
|
|
|
|
self._model.dump(model_loc)
|
|
|
|
return (Model, (self.n_classes, self.templates, model_loc),
|
|
|
|
None, None)
|
|
|
|
|
2015-06-26 04:25:36 +00:00
|
|
|
def predict(self, Example eg):
|
2015-06-28 20:36:03 +00:00
|
|
|
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)
|
2015-06-26 04:25:36 +00:00
|
|
|
|
|
|
|
def train(self, Example eg):
|
2015-06-28 20:36:03 +00:00
|
|
|
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)
|
2015-06-26 04:25:36 +00:00
|
|
|
|
2015-06-01 22:27:07 +00:00
|
|
|
cdef const weight_t* score(self, atom_t* context) except NULL:
|
2015-05-31 16:48:05 +00:00
|
|
|
cdef int n_feats
|
|
|
|
feats = self._extractor.get_feats(context, &n_feats)
|
|
|
|
return self._model.get_scores(feats, n_feats)
|
|
|
|
|
2015-07-14 21:47:03 +00:00
|
|
|
cdef int set_scores(self, weight_t* scores, atom_t* context) nogil:
|
2015-06-02 16:37:10 +00:00
|
|
|
cdef int n_feats
|
|
|
|
feats = self._extractor.get_feats(context, &n_feats)
|
|
|
|
self._model.set_scores(scores, feats, n_feats)
|
|
|
|
|
2014-12-31 08:40:59 +00:00
|
|
|
cdef int update(self, atom_t* context, class_t guess, class_t gold, int cost) except -1:
|
2014-12-30 10:20:15 +00:00
|
|
|
cdef int n_feats
|
|
|
|
if cost == 0:
|
|
|
|
self._model.update({})
|
2014-12-31 08:40:59 +00:00
|
|
|
else:
|
|
|
|
feats = self._extractor.get_feats(context, &n_feats)
|
|
|
|
counts = {gold: {}, guess: {}}
|
|
|
|
count_feats(counts[gold], feats, n_feats, cost)
|
|
|
|
count_feats(counts[guess], feats, n_feats, -cost)
|
|
|
|
self._model.update(counts)
|
2014-12-30 10:20:15 +00:00
|
|
|
|
2015-08-27 07:16:11 +00:00
|
|
|
def end_training(self, model_loc=None):
|
|
|
|
if model_loc is None:
|
|
|
|
model_loc = self.model_loc
|
2014-12-30 10:20:15 +00:00
|
|
|
self._model.end_training()
|
2015-08-27 07:16:11 +00:00
|
|
|
self._model.dump(model_loc, freq_thresh=0)
|