From bf33598b34d18f2766290cb1163a5c362b63cc9d Mon Sep 17 00:00:00 2001 From: Matthew Honnibal Date: Tue, 23 Jun 2015 22:55:58 +0200 Subject: [PATCH] * Work on a theano-driven model for the parser --- spacy/_theano.pyx | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 spacy/_theano.pyx diff --git a/spacy/_theano.pyx b/spacy/_theano.pyx new file mode 100644 index 000000000..1a1224596 --- /dev/null +++ b/spacy/_theano.pyx @@ -0,0 +1,44 @@ +from thinc.example cimport Example + + +cdef class TheanoModel(Model): + def __init__(self, n_classes, input_layer, train_func, predict_func, model_loc=None): + if model_loc is not None and path.isdir(model_loc): + model_loc = path.join(model_loc, 'model') + self.n_classes = n_classes + + tables = [] + lengths = [] + for window_size, n_dims, vocab_size in input_structure: + tables.append(EmbeddingTable(n_dims, vocab_size, initializer)) + lengths.append(window_size) + + self.input_layer = InputLayer(lengths, tables) + + self.train_func = train_func + self.predict_func = predict_func + + self.model_loc = model_loc + if self.model_loc and path.exists(self.model_loc): + self._model.load(self.model_loc, freq_thresh=0) + + def train(self, Instance eg): + pass + + def predict(self, Instance eg): + + cdef const weight_t* score(self, atom_t* context) except NULL: + self.set_scores(self._scores, context) + return self._scores + + cdef int set_scores(self, weight_t* scores, atom_t* context) except -1: + # TODO f(context) --> Values + self._input_layer.fill(self._x, self._values, use_avg=False) + theano_scores = self._predict(self._x) + for i in range(self.n_classes): + output[i] = theano_scores[i] + + cdef int update(self, atom_t* context, class_t guess, class_t gold, int cost) except -1: + # TODO f(context) --> Values + self._input_layer.fill(self._x, self._values, use_avg=False) +