* Wire eta and mu parameters up for neural net

This commit is contained in:
Matthew Honnibal 2015-06-29 07:10:33 +02:00
parent fc34e1b6e4
commit 894cbef8ba

View File

@ -10,12 +10,13 @@ from os import path
cdef class TheanoModel(Model): cdef class TheanoModel(Model):
def __init__(self, n_classes, input_spec, train_func, predict_func, model_loc=None, def __init__(self, n_classes, input_spec, train_func, predict_func, model_loc=None,
eta=0.001, mu=0.9,
debug=None): debug=None):
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.eta = 0.001 self.eta = eta
self.mu = 0.9 self.mu = mu
self.t = 1 self.t = 1
initializer = lambda: 0.2 * numpy.random.uniform(-1.0, 1.0) initializer = lambda: 0.2 * numpy.random.uniform(-1.0, 1.0)
self.input_layer = InputLayer(input_spec, initializer) self.input_layer = InputLayer(input_spec, initializer)
@ -28,22 +29,24 @@ cdef class TheanoModel(Model):
self.model_loc = model_loc self.model_loc = model_loc
def predict(self, Example eg): def predict(self, Example eg):
self.input_layer.fill(eg.embeddings, eg.atoms, use_avg=True) self.input_layer.fill(eg.embeddings, eg.atoms, use_avg=False)
theano_scores = self.predict_func(eg.embeddings)[0] theano_scores = self.predict_func(eg.embeddings)[0]
cdef int i cdef int i
for i in range(self.n_classes): for i in range(self.n_classes):
eg.scores[i] = theano_scores[i] eg.c.scores[i] = theano_scores[i]
eg.guess = arg_max_if_true(eg.c.scores, eg.c.is_valid, self.n_classes) 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.input_layer.fill(eg.embeddings, eg.atoms, use_avg=False) self.input_layer.fill(eg.embeddings, eg.atoms, use_avg=False)
theano_scores, update, y = self.train_func(eg.embeddings, eg.costs, self.eta) theano_scores, update, y, loss = self.train_func(eg.embeddings, eg.costs,
self.eta, self.mu)
self.input_layer.update(update, eg.atoms, self.t, self.eta, self.mu) self.input_layer.update(update, eg.atoms, self.t, self.eta, self.mu)
for i in range(self.n_classes): for i in range(self.n_classes):
eg.c.scores[i] = theano_scores[i] eg.c.scores[i] = theano_scores[i]
eg.guess = arg_max_if_true(eg.c.scores, eg.c.is_valid, self.n_classes) eg.c.guess = arg_max_if_true(eg.c.scores, eg.c.is_valid, self.n_classes)
eg.best = arg_max_if_zero(eg.c.scores, eg.c.costs, self.n_classes) eg.c.best = arg_max_if_zero(eg.c.scores, eg.c.costs, self.n_classes)
eg.cost = eg.c.costs[eg.guess] eg.c.cost = eg.c.costs[eg.c.guess]
eg.c.loss = loss
self.t += 1 self.t += 1
def end_training(self): def end_training(self):