From 98139f573eb179c8f5a06ba6c8d8883376814ccf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89tienne=20Simon?= Date: Thu, 2 Jul 2015 12:59:15 -0400 Subject: Remove _simple --- model/dest_mlp.py | 32 ++++++++++++++++++ model/dest_mlp_tgtcls.py | 34 +++++++++++++++++++ model/dest_simple_mlp.py | 32 ------------------ model/dest_simple_mlp_tgtcls.py | 34 ------------------- model/joint_mlp_tgtcls.py | 71 ++++++++++++++++++++++++++++++++++++++++ model/joint_simple_mlp_tgtcls.py | 71 ---------------------------------------- model/time_mlp.py | 31 ++++++++++++++++++ model/time_mlp_tgtcls.py | 33 +++++++++++++++++++ model/time_simple_mlp.py | 31 ------------------ model/time_simple_mlp_tgtcls.py | 33 ------------------- 10 files changed, 201 insertions(+), 201 deletions(-) create mode 100644 model/dest_mlp.py create mode 100644 model/dest_mlp_tgtcls.py delete mode 100644 model/dest_simple_mlp.py delete mode 100644 model/dest_simple_mlp_tgtcls.py create mode 100644 model/joint_mlp_tgtcls.py delete mode 100644 model/joint_simple_mlp_tgtcls.py create mode 100644 model/time_mlp.py create mode 100644 model/time_mlp_tgtcls.py delete mode 100644 model/time_simple_mlp.py delete mode 100644 model/time_simple_mlp_tgtcls.py (limited to 'model') diff --git a/model/dest_mlp.py b/model/dest_mlp.py new file mode 100644 index 0000000..78d7131 --- /dev/null +++ b/model/dest_mlp.py @@ -0,0 +1,32 @@ +from theano import tensor +from blocks.bricks import application, Identity + +import data +import error +from model.mlp import FFMLP, Stream + + +class Model(FFMLP): + def __init__(self, config, **kwargs): + super(Model, self).__init__(config, output_layer=Identity, **kwargs) + + @application(outputs=['destination']) + def predict(self, **kwargs): + outputs = super(Model, self).predict(**kwargs) + return data.train_gps_std * outputs + data.train_gps_mean + + @predict.property('inputs') + def predict_inputs(self): + return self.inputs + + @application(outputs=['cost']) + def cost(self, **kwargs): + y_hat = self.predict(**kwargs) + y = tensor.concatenate((kwargs['destination_latitude'][:, None], + kwargs['destination_longitude'][:, None]), axis=1) + + return error.erdist(y_hat, y).mean() + + @cost.property('inputs') + def cost_inputs(self): + return self.inputs + ['destination_latitude', 'destination_longitude'] diff --git a/model/dest_mlp_tgtcls.py b/model/dest_mlp_tgtcls.py new file mode 100644 index 0000000..46fca2b --- /dev/null +++ b/model/dest_mlp_tgtcls.py @@ -0,0 +1,34 @@ +import numpy +import theano +from theano import tensor +from blocks.bricks import application, Softmax + +import error +from model.mlp import FFMLP, Stream + + +class Model(FFMLP): + def __init__(self, config, **kwargs): + super(Model, self).__init__(config, output_layer=Softmax, **kwargs) + self.classes = theano.shared(numpy.array(config.tgtcls, dtype=theano.config.floatX), name='classes') + + @application(outputs=['destination']) + def predict(self, **kwargs): + cls_probas = super(Model, self).predict(**kwargs) + return tensor.dot(cls_probas, self.classes) + + @predict.property('inputs') + def predict_inputs(self): + return self.inputs + + @application(outputs=['cost']) + def cost(self, **kwargs): + y_hat = self.predict(**kwargs) + y = tensor.concatenate((kwargs['destination_latitude'][:, None], + kwargs['destination_longitude'][:, None]), axis=1) + + return error.erdist(y_hat, y).mean() + + @cost.property('inputs') + def cost_inputs(self): + return self.inputs + ['destination_latitude', 'destination_longitude'] diff --git a/model/dest_simple_mlp.py b/model/dest_simple_mlp.py deleted file mode 100644 index 78d7131..0000000 --- a/model/dest_simple_mlp.py +++ /dev/null @@ -1,32 +0,0 @@ -from theano import tensor -from blocks.bricks import application, Identity - -import data -import error -from model.mlp import FFMLP, Stream - - -class Model(FFMLP): - def __init__(self, config, **kwargs): - super(Model, self).__init__(config, output_layer=Identity, **kwargs) - - @application(outputs=['destination']) - def predict(self, **kwargs): - outputs = super(Model, self).predict(**kwargs) - return data.train_gps_std * outputs + data.train_gps_mean - - @predict.property('inputs') - def predict_inputs(self): - return self.inputs - - @application(outputs=['cost']) - def cost(self, **kwargs): - y_hat = self.predict(**kwargs) - y = tensor.concatenate((kwargs['destination_latitude'][:, None], - kwargs['destination_longitude'][:, None]), axis=1) - - return error.erdist(y_hat, y).mean() - - @cost.property('inputs') - def cost_inputs(self): - return self.inputs + ['destination_latitude', 'destination_longitude'] diff --git a/model/dest_simple_mlp_tgtcls.py b/model/dest_simple_mlp_tgtcls.py deleted file mode 100644 index 46fca2b..0000000 --- a/model/dest_simple_mlp_tgtcls.py +++ /dev/null @@ -1,34 +0,0 @@ -import numpy -import theano -from theano import tensor -from blocks.bricks import application, Softmax - -import error -from model.mlp import FFMLP, Stream - - -class Model(FFMLP): - def __init__(self, config, **kwargs): - super(Model, self).__init__(config, output_layer=Softmax, **kwargs) - self.classes = theano.shared(numpy.array(config.tgtcls, dtype=theano.config.floatX), name='classes') - - @application(outputs=['destination']) - def predict(self, **kwargs): - cls_probas = super(Model, self).predict(**kwargs) - return tensor.dot(cls_probas, self.classes) - - @predict.property('inputs') - def predict_inputs(self): - return self.inputs - - @application(outputs=['cost']) - def cost(self, **kwargs): - y_hat = self.predict(**kwargs) - y = tensor.concatenate((kwargs['destination_latitude'][:, None], - kwargs['destination_longitude'][:, None]), axis=1) - - return error.erdist(y_hat, y).mean() - - @cost.property('inputs') - def cost_inputs(self): - return self.inputs + ['destination_latitude', 'destination_longitude'] diff --git a/model/joint_mlp_tgtcls.py b/model/joint_mlp_tgtcls.py new file mode 100644 index 0000000..d6d4e49 --- /dev/null +++ b/model/joint_mlp_tgtcls.py @@ -0,0 +1,71 @@ +import numpy +import theano +from theano import tensor +from blocks import roles +from blocks.bricks import application, MLP, Rectifier, Softmax + +import error +from model.mlp import FFMLP, Stream + + +class Model(FFMLP): + def __init__(self, config, **kwargs): + super(Model, self).__init__(config, **kwargs) + + self.dest_mlp = MLP(activations=[Rectifier() for _ in config.dim_hidden_dest] + [Softmax()], + dims=[config.dim_hidden[-1]] + config.dim_hidden_dest + [config.dim_output_dest], + name='dest_mlp') + self.time_mlp = MLP(activations=[Rectifier() for _ in config.dim_hidden_time] + [Softmax()], + dims=[config.dim_hidden[-1]] + config.dim_hidden_time + [config.dim_output_time], + name='time_mlp') + + self.dest_classes = theano.shared(numpy.array(config.dest_tgtcls, dtype=theano.config.floatX), name='dest_classes') + self.time_classes = theano.shared(numpy.array(config.time_tgtcls, dtype=theano.config.floatX), name='time_classes') + + self.inputs.append('input_time') + self.children.extend([self.dest_mlp, self.time_mlp]) + + def _push_initialization_config(self): + super(Model, self)._push_initialization_config() + for mlp in [self.dest_mlp, self.time_mlp]: + mlp.weights_init = self.config.mlp_weights_init + mlp.biases_init = self.config.mlp_biases_init + + @application(outputs=['destination', 'duration']) + def predict(self, **kwargs): + hidden = super(Model, self).predict(**kwargs) + + dest_cls_probas = self.dest_mlp.apply(hidden) + dest_outputs = tensor.dot(dest_cls_probas, self.dest_classes) + + time_cls_probas = self.time_mlp.apply(hidden) + time_outputs = kwargs['input_time'] + tensor.dot(time_cls_probas, self.time_classes) + + self.add_auxiliary_variable(dest_cls_probas, name='destination classes ponderations') + self.add_auxiliary_variable(time_cls_probas, name='time classes ponderations') + + return (dest_outputs, time_outputs) + + @predict.property('inputs') + def predict_inputs(self): + return self.inputs + + @application(outputs=['cost']) + def cost(self, **kwargs): + (destination_hat, time_hat) = self.predict(**kwargs) + + destination = tensor.concatenate((kwargs['destination_latitude'][:, None], + kwargs['destination_longitude'][:, None]), axis=1) + time = kwargs['travel_time'] + + destination_cost = error.erdist(destination_hat, destination).mean() + time_cost = error.rmsle(time_hat.flatten(), time.flatten()) + + self.add_auxiliary_variable(destination_cost, [roles.COST], 'destination_cost') + self.add_auxiliary_variable(time_cost, [roles.COST], 'time_cost') + + return destination_cost + self.config.time_cost_factor * time_cost + + @cost.property('inputs') + def cost_inputs(self): + return self.inputs + ['destination_latitude', 'destination_longitude', 'travel_time'] diff --git a/model/joint_simple_mlp_tgtcls.py b/model/joint_simple_mlp_tgtcls.py deleted file mode 100644 index d6d4e49..0000000 --- a/model/joint_simple_mlp_tgtcls.py +++ /dev/null @@ -1,71 +0,0 @@ -import numpy -import theano -from theano import tensor -from blocks import roles -from blocks.bricks import application, MLP, Rectifier, Softmax - -import error -from model.mlp import FFMLP, Stream - - -class Model(FFMLP): - def __init__(self, config, **kwargs): - super(Model, self).__init__(config, **kwargs) - - self.dest_mlp = MLP(activations=[Rectifier() for _ in config.dim_hidden_dest] + [Softmax()], - dims=[config.dim_hidden[-1]] + config.dim_hidden_dest + [config.dim_output_dest], - name='dest_mlp') - self.time_mlp = MLP(activations=[Rectifier() for _ in config.dim_hidden_time] + [Softmax()], - dims=[config.dim_hidden[-1]] + config.dim_hidden_time + [config.dim_output_time], - name='time_mlp') - - self.dest_classes = theano.shared(numpy.array(config.dest_tgtcls, dtype=theano.config.floatX), name='dest_classes') - self.time_classes = theano.shared(numpy.array(config.time_tgtcls, dtype=theano.config.floatX), name='time_classes') - - self.inputs.append('input_time') - self.children.extend([self.dest_mlp, self.time_mlp]) - - def _push_initialization_config(self): - super(Model, self)._push_initialization_config() - for mlp in [self.dest_mlp, self.time_mlp]: - mlp.weights_init = self.config.mlp_weights_init - mlp.biases_init = self.config.mlp_biases_init - - @application(outputs=['destination', 'duration']) - def predict(self, **kwargs): - hidden = super(Model, self).predict(**kwargs) - - dest_cls_probas = self.dest_mlp.apply(hidden) - dest_outputs = tensor.dot(dest_cls_probas, self.dest_classes) - - time_cls_probas = self.time_mlp.apply(hidden) - time_outputs = kwargs['input_time'] + tensor.dot(time_cls_probas, self.time_classes) - - self.add_auxiliary_variable(dest_cls_probas, name='destination classes ponderations') - self.add_auxiliary_variable(time_cls_probas, name='time classes ponderations') - - return (dest_outputs, time_outputs) - - @predict.property('inputs') - def predict_inputs(self): - return self.inputs - - @application(outputs=['cost']) - def cost(self, **kwargs): - (destination_hat, time_hat) = self.predict(**kwargs) - - destination = tensor.concatenate((kwargs['destination_latitude'][:, None], - kwargs['destination_longitude'][:, None]), axis=1) - time = kwargs['travel_time'] - - destination_cost = error.erdist(destination_hat, destination).mean() - time_cost = error.rmsle(time_hat.flatten(), time.flatten()) - - self.add_auxiliary_variable(destination_cost, [roles.COST], 'destination_cost') - self.add_auxiliary_variable(time_cost, [roles.COST], 'time_cost') - - return destination_cost + self.config.time_cost_factor * time_cost - - @cost.property('inputs') - def cost_inputs(self): - return self.inputs + ['destination_latitude', 'destination_longitude', 'travel_time'] diff --git a/model/time_mlp.py b/model/time_mlp.py new file mode 100644 index 0000000..a52590d --- /dev/null +++ b/model/time_mlp.py @@ -0,0 +1,31 @@ +from blocks.bricks import application, Identity + +import error +from model.mlp import FFMLP, Stream + + +class Model(FFMLP): + def __init__(self, config, **kwargs): + super(Model, self).__init__(config, output_layer=Identity, **kwargs) + self.inputs.append('input_time') + + @application(outputs=['duration']) + def predict(self, **kwargs): + outputs = super(Model, self).predict(**kwargs).flatten() + if hasattr(self.config, 'exp_base'): + outputs = self.config.exp_base ** outputs + return kwargs['input_time'] + outputs + + @predict.property('inputs') + def predict_inputs(self): + return self.inputs + + @application(outputs=['cost']) + def cost(self, **kwargs): + y_hat = self.predict(**kwargs) + y = kwargs['travel_time'] + return error.rmsle(y_hat, y) + + @cost.property('inputs') + def cost_inputs(self): + return self.inputs + ['travel_time'] diff --git a/model/time_mlp_tgtcls.py b/model/time_mlp_tgtcls.py new file mode 100644 index 0000000..35c8d8a --- /dev/null +++ b/model/time_mlp_tgtcls.py @@ -0,0 +1,33 @@ +import numpy +import theano +from theano import tensor +from blocks.bricks import application, Softmax + +import error +from model.mlp import FFMLP, Stream + + +class Model(FFMLP): + def __init__(self, config, **kwargs): + super(Model, self, output_layer=Softmax).__init__(config, **kwargs) + self.classes = theano.shared(numpy.array(config.tgtcls, dtype=theano.config.floatX), name='classes') + self.inputs.append('input_time') + + @application(outputs=['duration']) + def predict(self, **kwargs): + cls_probas = super(Model, self).predict(**kwargs) + return kwargs['input_time'] + tensor.dot(cls_probas, self.classes) + + @predict.property('inputs') + def predict_inputs(self): + return self.inputs + + @application(outputs=['cost']) + def cost(self, **kwargs): + y_hat = self.predict(**kwargs) + y = kwargs['travel_time'] + return error.rmsle(y_hat.flatten(), y.flatten()) + + @cost.property('inputs') + def cost_inputs(self): + return self.inputs + ['travel_time'] diff --git a/model/time_simple_mlp.py b/model/time_simple_mlp.py deleted file mode 100644 index a52590d..0000000 --- a/model/time_simple_mlp.py +++ /dev/null @@ -1,31 +0,0 @@ -from blocks.bricks import application, Identity - -import error -from model.mlp import FFMLP, Stream - - -class Model(FFMLP): - def __init__(self, config, **kwargs): - super(Model, self).__init__(config, output_layer=Identity, **kwargs) - self.inputs.append('input_time') - - @application(outputs=['duration']) - def predict(self, **kwargs): - outputs = super(Model, self).predict(**kwargs).flatten() - if hasattr(self.config, 'exp_base'): - outputs = self.config.exp_base ** outputs - return kwargs['input_time'] + outputs - - @predict.property('inputs') - def predict_inputs(self): - return self.inputs - - @application(outputs=['cost']) - def cost(self, **kwargs): - y_hat = self.predict(**kwargs) - y = kwargs['travel_time'] - return error.rmsle(y_hat, y) - - @cost.property('inputs') - def cost_inputs(self): - return self.inputs + ['travel_time'] diff --git a/model/time_simple_mlp_tgtcls.py b/model/time_simple_mlp_tgtcls.py deleted file mode 100644 index 35c8d8a..0000000 --- a/model/time_simple_mlp_tgtcls.py +++ /dev/null @@ -1,33 +0,0 @@ -import numpy -import theano -from theano import tensor -from blocks.bricks import application, Softmax - -import error -from model.mlp import FFMLP, Stream - - -class Model(FFMLP): - def __init__(self, config, **kwargs): - super(Model, self, output_layer=Softmax).__init__(config, **kwargs) - self.classes = theano.shared(numpy.array(config.tgtcls, dtype=theano.config.floatX), name='classes') - self.inputs.append('input_time') - - @application(outputs=['duration']) - def predict(self, **kwargs): - cls_probas = super(Model, self).predict(**kwargs) - return kwargs['input_time'] + tensor.dot(cls_probas, self.classes) - - @predict.property('inputs') - def predict_inputs(self): - return self.inputs - - @application(outputs=['cost']) - def cost(self, **kwargs): - y_hat = self.predict(**kwargs) - y = kwargs['travel_time'] - return error.rmsle(y_hat.flatten(), y.flatten()) - - @cost.property('inputs') - def cost_inputs(self): - return self.inputs + ['travel_time'] -- cgit v1.2.3