2015-06-28 20:36:03 +00:00
|
|
|
from thinc.api cimport Example, ExampleC
|
2015-06-26 11:51:39 +00:00
|
|
|
from thinc.typedefs cimport weight_t
|
|
|
|
|
|
|
|
from ._ml cimport arg_max_if_true
|
|
|
|
from ._ml cimport arg_max_if_zero
|
|
|
|
|
|
|
|
import numpy
|
|
|
|
from os import path
|
2015-06-23 20:55:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
cdef class TheanoModel(Model):
|
2015-06-28 09:36:11 +00:00
|
|
|
def __init__(self, n_classes, input_spec, train_func, predict_func, model_loc=None,
|
2015-06-29 05:10:33 +00:00
|
|
|
eta=0.001, mu=0.9,
|
2015-06-28 09:36:11 +00:00
|
|
|
debug=None):
|
2015-06-23 20:55:58 +00:00
|
|
|
if model_loc is not None and path.isdir(model_loc):
|
|
|
|
model_loc = path.join(model_loc, 'model')
|
|
|
|
|
2015-06-29 05:10:33 +00:00
|
|
|
self.eta = eta
|
|
|
|
self.mu = mu
|
2015-06-26 11:51:39 +00:00
|
|
|
self.t = 1
|
|
|
|
initializer = lambda: 0.2 * numpy.random.uniform(-1.0, 1.0)
|
|
|
|
self.input_layer = InputLayer(input_spec, initializer)
|
2015-06-23 20:55:58 +00:00
|
|
|
self.train_func = train_func
|
|
|
|
self.predict_func = predict_func
|
2015-06-28 09:36:11 +00:00
|
|
|
self.debug = debug
|
2015-06-23 20:55:58 +00:00
|
|
|
|
2015-06-26 11:51:39 +00:00
|
|
|
self.n_classes = n_classes
|
|
|
|
self.n_feats = len(self.input_layer)
|
2015-06-23 20:55:58 +00:00
|
|
|
self.model_loc = model_loc
|
2015-06-26 11:51:39 +00:00
|
|
|
|
|
|
|
def predict(self, Example eg):
|
2015-06-29 05:10:33 +00:00
|
|
|
self.input_layer.fill(eg.embeddings, eg.atoms, use_avg=False)
|
2015-06-28 09:36:11 +00:00
|
|
|
theano_scores = self.predict_func(eg.embeddings)[0]
|
2015-06-26 11:51:39 +00:00
|
|
|
cdef int i
|
2015-06-23 20:55:58 +00:00
|
|
|
for i in range(self.n_classes):
|
2015-06-29 05:10:33 +00:00
|
|
|
eg.c.scores[i] = theano_scores[i]
|
|
|
|
eg.c.guess = arg_max_if_true(eg.c.scores, eg.c.is_valid, self.n_classes)
|
2015-06-26 11:51:39 +00:00
|
|
|
|
|
|
|
def train(self, Example eg):
|
2015-06-27 02:18:47 +00:00
|
|
|
self.input_layer.fill(eg.embeddings, eg.atoms, use_avg=False)
|
2015-06-29 05:10:33 +00:00
|
|
|
theano_scores, update, y, loss = self.train_func(eg.embeddings, eg.costs,
|
|
|
|
self.eta, self.mu)
|
2015-06-28 09:36:11 +00:00
|
|
|
self.input_layer.update(update, eg.atoms, self.t, self.eta, self.mu)
|
|
|
|
for i in range(self.n_classes):
|
2015-06-28 20:36:03 +00:00
|
|
|
eg.c.scores[i] = theano_scores[i]
|
2015-06-29 05:10:33 +00:00
|
|
|
eg.c.guess = arg_max_if_true(eg.c.scores, eg.c.is_valid, self.n_classes)
|
|
|
|
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]
|
|
|
|
eg.c.loss = loss
|
2015-06-26 11:51:39 +00:00
|
|
|
self.t += 1
|
2015-06-28 09:36:11 +00:00
|
|
|
|
|
|
|
def end_training(self):
|
|
|
|
pass
|