From e20106fdff782e1c0de8e777af2f7e6df1ba63db Mon Sep 17 00:00:00 2001 From: Matthew Honnibal Date: Tue, 30 Jun 2015 14:26:32 +0200 Subject: [PATCH] * Begin reorganizing neuralnet work --- spacy/_ml.pxd | 4 ++-- spacy/_theano.pyx | 3 +-- spacy/syntax/parser.pxd | 6 +++--- spacy/syntax/parser.pyx | 20 +++++++++++++------- 4 files changed, 19 insertions(+), 14 deletions(-) diff --git a/spacy/_ml.pxd b/spacy/_ml.pxd index 40281cad2..c695e48aa 100644 --- a/spacy/_ml.pxd +++ b/spacy/_ml.pxd @@ -21,8 +21,8 @@ cdef int arg_max_if_zero(const weight_t* scores, const int* costs, int n_classes cdef class Model: - cdef int n_classes - cdef int n_feats + cdef readonly int n_classes + cdef readonly int n_feats cdef const weight_t* score(self, atom_t* context) except NULL cdef int set_scores(self, weight_t* scores, atom_t* context) except -1 diff --git a/spacy/_theano.pyx b/spacy/_theano.pyx index 4231266c3..cc6886321 100644 --- a/spacy/_theano.pyx +++ b/spacy/_theano.pyx @@ -10,8 +10,7 @@ from os import path cdef class TheanoModel(Model): def __init__(self, n_classes, input_spec, train_func, predict_func, model_loc=None, - eta=0.001, mu=0.9, - debug=None): + eta=0.001, mu=0.9, debug=None): if model_loc is not None and path.isdir(model_loc): model_loc = path.join(model_loc, 'model') diff --git a/spacy/syntax/parser.pxd b/spacy/syntax/parser.pxd index 11ac6bbb8..7e2b3b083 100644 --- a/spacy/syntax/parser.pxd +++ b/spacy/syntax/parser.pxd @@ -8,6 +8,6 @@ from ..tokens cimport Tokens, TokenC cdef class Parser: - cdef readonly object cfg - cdef readonly Model model - cdef readonly TransitionSystem moves + cdef public object cfg + cdef public Model model + cdef public TransitionSystem moves diff --git a/spacy/syntax/parser.pyx b/spacy/syntax/parser.pyx index 2ea60b149..c3ed0b464 100644 --- a/spacy/syntax/parser.pyx +++ b/spacy/syntax/parser.pyx @@ -59,13 +59,11 @@ def get_templates(name): cdef class Parser: - def __init__(self, StringStore strings, model_dir, transition_system, - get_model=Model): + def __init__(self, StringStore strings, model_dir, transition_system): assert os.path.exists(model_dir) and os.path.isdir(model_dir) self.cfg = Config.read(model_dir, 'config') self.moves = transition_system(strings, self.cfg.labels) - templates = get_templates(self.cfg.features) - self.model = get_model(self.moves.n_moves, templates, model_dir) + self.model = Model(self.moves.n_moves, self.cfg.templates, model_dir) def __call__(self, Tokens tokens): cdef StateClass stcls = StateClass.init(tokens.data, tokens.length) @@ -73,6 +71,8 @@ cdef class Parser: cdef Example eg = Example(self.model.n_classes, CONTEXT_SIZE, self.model.n_feats, self.model.n_feats) + eg.scores[0] = 10 + assert eg.c.scores[0] == 10 while not stcls.is_final(): memset(eg.c.scores, 0, eg.c.nr_class * sizeof(weight_t)) @@ -91,7 +91,9 @@ cdef class Parser: self.moves.initialize_state(stcls) cdef Example eg = Example(self.model.n_classes, CONTEXT_SIZE, self.model.n_feats, self.model.n_feats) - cdef int cost = 0 + cdef weight_t loss = 0 + words = [w.orth_ for w in tokens] + cdef Transition G while not stcls.is_final(): memset(eg.c.scores, 0, eg.c.nr_class * sizeof(weight_t)) @@ -101,9 +103,13 @@ cdef class Parser: self.model.train(eg) + G = self.moves.c[eg.c.guess] + + #if eg.c.cost != 0: + # print self.moves.move_name(G.move, G.label), stcls.print_state(words) self.moves.c[eg.c.guess].do(stcls, self.moves.c[eg.c.guess].label) - cost += eg.c.cost - return cost + loss += eg.c.loss + return loss