aboutsummaryrefslogtreecommitdiff
path: root/model
diff options
context:
space:
mode:
authorÉtienne Simon <esimon@esimon.eu>2015-05-18 16:22:00 -0400
committerÉtienne Simon <esimon@esimon.eu>2015-05-18 16:22:00 -0400
commit6d946f29f7548c75e97f30c4356dbac200ee6cce (patch)
tree387e586c7ad0c1a0167d21451c9a8c877cf3ef0e /model
parent1e6d08b0c9ac5983691b182631c71e9d46ee71cc (diff)
downloadtaxi-6d946f29f7548c75e97f30c4356dbac200ee6cce.tar.gz
taxi-6d946f29f7548c75e97f30c4356dbac200ee6cce.zip
Refactor models, clean the code and separate training from testing.
Diffstat (limited to 'model')
-rw-r--r--model/__init__.py36
-rw-r--r--model/dest_simple_mlp.py81
-rw-r--r--model/dest_simple_mlp_tgtcls.py85
-rw-r--r--model/joint_simple_mlp_tgtcls.py148
-rw-r--r--model/mlp.py103
-rw-r--r--model/time_simple_mlp.py76
-rw-r--r--model/time_simple_mlp_tgtcls.py78
7 files changed, 281 insertions, 326 deletions
diff --git a/model/__init__.py b/model/__init__.py
index e69de29..5c051f4 100644
--- a/model/__init__.py
+++ b/model/__init__.py
@@ -0,0 +1,36 @@
+from blocks.bricks import application, Initializable
+from blocks.bricks.lookup import LookupTable
+
+
+class ContextEmbedder(Initializable):
+ def __init__(self, config, **kwargs):
+ super(ContextEmbedder, self).__init__(**kwargs)
+ self.dim_embeddings = config.dim_embeddings
+ self.embed_weights_init = config.embed_weights_init
+
+ self.inputs = [ name for (name, _, _) in self.dim_embeddings ]
+ self.outputs = [ '%s_embedded' % name for name in self.inputs ]
+
+ self.lookups = { name: LookupTable(name='%s_lookup' % name) for name in self.inputs }
+ self.children = self.lookups.values()
+
+ def _push_allocation_config(self):
+ for (name, num, dim) in self.dim_embeddings:
+ self.lookups[name].length = num
+ self.lookups[name].dim = dim
+
+ def _push_initialization_config(self):
+ for name in self.inputs:
+ self.lookups[name].weights_init = self.embed_weights_init
+
+ @application
+ def apply(self, **kwargs):
+ return tuple(self.lookups[name].apply(kwargs[name]) for name in self.inputs)
+
+ @apply.property('inputs')
+ def apply_inputs(self):
+ return self.inputs
+
+ @apply.property('outputs')
+ def apply_outputs(self):
+ return self.outputs
diff --git a/model/dest_simple_mlp.py b/model/dest_simple_mlp.py
index a9e97cb..78d7131 100644
--- a/model/dest_simple_mlp.py
+++ b/model/dest_simple_mlp.py
@@ -1,71 +1,32 @@
-from blocks.bricks import MLP, Rectifier, Linear, Sigmoid, Identity
-from blocks.bricks.lookup import LookupTable
-
from theano import tensor
+from blocks.bricks import application, Identity
import data
import error
+from model.mlp import FFMLP, Stream
-class Model(object):
- def __init__(self, config):
- # The input and the targets
- x_firstk_latitude = (tensor.matrix('first_k_latitude') - data.train_gps_mean[0]) / data.train_gps_std[0]
- x_firstk_longitude = (tensor.matrix('first_k_longitude') - data.train_gps_mean[1]) / data.train_gps_std[1]
-
- x_lastk_latitude = (tensor.matrix('last_k_latitude') - data.train_gps_mean[0]) / data.train_gps_std[0]
- x_lastk_longitude = (tensor.matrix('last_k_longitude') - data.train_gps_mean[1]) / data.train_gps_std[1]
-
- input_list = [x_firstk_latitude, x_firstk_longitude, x_lastk_latitude, x_lastk_longitude]
- embed_tables = []
-
- self.require_inputs = ['first_k_latitude', 'first_k_longitude', 'last_k_latitude', 'last_k_longitude']
-
- for (varname, num, dim) in config.dim_embeddings:
- self.require_inputs.append(varname)
- vardata = tensor.lvector(varname)
- tbl = LookupTable(length=num, dim=dim, name='%s_lookup'%varname)
- embed_tables.append(tbl)
- input_list.append(tbl.apply(vardata))
-
- y = tensor.concatenate((tensor.vector('destination_latitude')[:, None],
- tensor.vector('destination_longitude')[:, None]), axis=1)
-
- # Define the model
- mlp = MLP(activations=[Rectifier() for _ in config.dim_hidden] + [Identity()],
- dims=[config.dim_input] + config.dim_hidden + [config.dim_output])
-
- # Create the Theano variables
- inputs = tensor.concatenate(input_list, axis=1)
- # inputs = theano.printing.Print("inputs")(inputs)
- outputs = mlp.apply(inputs)
-
- # Normalize & Center
- # outputs = theano.printing.Print("normal_outputs")(outputs)
- outputs = data.train_gps_std * outputs + data.train_gps_mean
-
- # outputs = theano.printing.Print("outputs")(outputs)
- # y = theano.printing.Print("y")(y)
- outputs.name = 'outputs'
+class Model(FFMLP):
+ def __init__(self, config, **kwargs):
+ super(Model, self).__init__(config, output_layer=Identity, **kwargs)
- # Calculate the cost
- cost = error.erdist(outputs, y).mean()
- cost.name = 'cost'
- hcost = error.hdist(outputs, y).mean()
- hcost.name = 'hcost'
+ @application(outputs=['destination'])
+ def predict(self, **kwargs):
+ outputs = super(Model, self).predict(**kwargs)
+ return data.train_gps_std * outputs + data.train_gps_mean
- # Initialization
- for tbl in embed_tables:
- tbl.weights_init = config.embed_weights_init
- mlp.weights_init = config.mlp_weights_init
- mlp.biases_init = config.mlp_biases_init
+ @predict.property('inputs')
+ def predict_inputs(self):
+ return self.inputs
- for tbl in embed_tables:
- tbl.initialize()
- mlp.initialize()
+ @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)
- self.cost = cost
- self.monitor = [cost, hcost]
- self.outputs = outputs
- self.pred_vars = ['destination_latitude', 'destination_longitude']
+ 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
index 1381d4c..2d65097 100644
--- a/model/dest_simple_mlp_tgtcls.py
+++ b/model/dest_simple_mlp_tgtcls.py
@@ -1,73 +1,34 @@
-import numpy
-
+import numpy
import theano
from theano import tensor
+from blocks.bricks import application, Softmax
-from blocks.bricks import MLP, Rectifier, Linear, Sigmoid, Identity, Softmax
-from blocks.bricks.lookup import LookupTable
-
-import data
import error
+from model.mlp import FFMLP, Stream
-class Model(object):
- def __init__(self, config):
- # The input and the targets
- x_firstk_latitude = (tensor.matrix('first_k_latitude') - data.train_gps_mean[0]) / data.train_gps_std[0]
- x_firstk_longitude = (tensor.matrix('first_k_longitude') - data.train_gps_mean[1]) / data.train_gps_std[1]
-
- x_lastk_latitude = (tensor.matrix('last_k_latitude') - data.train_gps_mean[0]) / data.train_gps_std[0]
- x_lastk_longitude = (tensor.matrix('last_k_longitude') - data.train_gps_mean[1]) / data.train_gps_std[1]
-
- input_list = [x_firstk_latitude, x_firstk_longitude, x_lastk_latitude, x_lastk_longitude]
- embed_tables = []
-
- self.require_inputs = ['first_k_latitude', 'first_k_longitude', 'last_k_latitude', 'last_k_longitude']
-
- for (varname, num, dim) in config.dim_embeddings:
- self.require_inputs.append(varname)
- vardata = tensor.lvector(varname)
- tbl = LookupTable(length=num, dim=dim, name='%s_lookup'%varname)
- embed_tables.append(tbl)
- input_list.append(tbl.apply(vardata))
-
- y = tensor.concatenate((tensor.vector('destination_latitude')[:, None],
- tensor.vector('destination_longitude')[:, None]), axis=1)
-
- # Define the model
- mlp = MLP(activations=[Rectifier() for _ in config.dim_hidden] + [Softmax()],
- dims=[config.dim_input] + config.dim_hidden + [config.dim_output])
- classes = theano.shared(numpy.array(config.tgtcls, dtype=theano.config.floatX), name='classes')
-
- # Create the Theano variables
- inputs = tensor.concatenate(input_list, axis=1)
-
- # inputs = theano.printing.Print("inputs")(inputs)
- cls_probas = mlp.apply(inputs)
- outputs = tensor.dot(cls_probas, classes)
-
- # outputs = theano.printing.Print("outputs")(outputs)
- # y = theano.printing.Print("y")(y)
- outputs.name = 'outputs'
+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')
- # Calculate the cost
- cost = error.erdist(outputs, y).mean()
- cost.name = 'cost'
- hcost = error.hdist(outputs, y).mean()
- hcost.name = 'hcost'
+ @application(outputs=['destination'])
+ def predict(self, **kwargs):
+ cls_probas = super(Model, self).predict(**kwargs)
+ return tensor.dot(cls_probas, self.classes)
- # Initialization
- for tbl in embed_tables:
- tbl.weights_init = config.embed_weights_init
- mlp.weights_init = config.mlp_weights_init
- mlp.biases_init = config.mlp_biases_init
+ @predict.property('inputs')
+ def predict_inputs(self):
+ return self.inputs
- for tbl in embed_tables:
- tbl.initialize()
- mlp.initialize()
+ @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)
- self.cost = cost
- self.monitor = [cost, hcost]
- self.outputs = outputs
- self.pred_vars = ['destination_latitude', 'destination_longitude']
+ 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_simple_mlp_tgtcls.py b/model/joint_simple_mlp_tgtcls.py
index 834afbf..d6d4e49 100644
--- a/model/joint_simple_mlp_tgtcls.py
+++ b/model/joint_simple_mlp_tgtcls.py
@@ -1,109 +1,71 @@
-from blocks.bricks import MLP, Rectifier, Linear, Sigmoid, Identity, Softmax
-from blocks.bricks.lookup import LookupTable
-
-from blocks.filter import VariableFilter
-from blocks.graph import ComputationGraph, apply_dropout
-
import numpy
import theano
from theano import tensor
+from blocks import roles
+from blocks.bricks import application, MLP, Rectifier, Softmax
-import data
import error
+from model.mlp import FFMLP, Stream
-class Model(object):
- def __init__(self, config):
- # The input and the targets
- x_firstk_latitude = (tensor.matrix('first_k_latitude') - data.train_gps_mean[0]) / data.train_gps_std[0]
- x_firstk_longitude = (tensor.matrix('first_k_longitude') - data.train_gps_mean[1]) / data.train_gps_std[1]
- x_lastk_latitude = (tensor.matrix('last_k_latitude') - data.train_gps_mean[0]) / data.train_gps_std[0]
- x_lastk_longitude = (tensor.matrix('last_k_longitude') - data.train_gps_mean[1]) / data.train_gps_std[1]
+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')
- x_input_time = tensor.lvector('input_time')
+ 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')
- input_list = [x_firstk_latitude, x_firstk_longitude, x_lastk_latitude, x_lastk_longitude]
- embed_tables = []
+ self.inputs.append('input_time')
+ self.children.extend([self.dest_mlp, self.time_mlp])
- self.require_inputs = ['first_k_latitude', 'first_k_longitude', 'last_k_latitude', 'last_k_longitude', 'input_time']
+ 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
- for (varname, num, dim) in config.dim_embeddings:
- self.require_inputs.append(varname)
- vardata = tensor.lvector(varname)
- tbl = LookupTable(length=num, dim=dim, name='%s_lookup'%varname)
- embed_tables.append(tbl)
- input_list.append(tbl.apply(vardata))
+ @application(outputs=['destination', 'duration'])
+ def predict(self, **kwargs):
+ hidden = super(Model, self).predict(**kwargs)
- y_dest = tensor.concatenate((tensor.vector('destination_latitude')[:, None],
- tensor.vector('destination_longitude')[:, None]), axis=1)
- y_time = tensor.lvector('travel_time')
+ dest_cls_probas = self.dest_mlp.apply(hidden)
+ dest_outputs = tensor.dot(dest_cls_probas, self.dest_classes)
- # Define the model
- common_mlp = MLP(activations=[Rectifier() for _ in config.dim_hidden],
- dims=[config.dim_input] + config.dim_hidden)
+ time_cls_probas = self.time_mlp.apply(hidden)
+ time_outputs = kwargs['input_time'] + tensor.dot(time_cls_probas, self.time_classes)
- 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')
- dest_classes = theano.shared(numpy.array(config.dest_tgtcls, dtype=theano.config.floatX), name='dest_classes')
+ self.add_auxiliary_variable(dest_cls_probas, name='destination classes ponderations')
+ self.add_auxiliary_variable(time_cls_probas, name='time classes ponderations')
- 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')
- time_classes = theano.shared(numpy.array(config.time_tgtcls, dtype=theano.config.floatX), name='time_classes')
-
- # Create the Theano variables
- inputs = tensor.concatenate(input_list, axis=1)
- # inputs = theano.printing.Print("inputs")(inputs)
- hidden = common_mlp.apply(inputs)
-
- dest_cls_probas = dest_mlp.apply(hidden)
- dest_outputs = tensor.dot(dest_cls_probas, dest_classes)
- dest_outputs.name = 'dest_outputs'
-
- time_cls_probas = time_mlp.apply(hidden)
- time_outputs = tensor.dot(time_cls_probas, time_classes) + x_input_time
- time_outputs.name = 'time_outputs'
-
- # Calculate the cost
- dest_cost = error.erdist(dest_outputs, y_dest).mean()
- dest_cost.name = 'dest_cost'
- dest_hcost = error.hdist(dest_outputs, y_dest).mean()
- dest_hcost.name = 'dest_hcost'
-
- time_cost = error.rmsle(time_outputs.flatten(), y_time.flatten())
- time_cost.name = 'time_cost'
- time_scost = config.time_cost_factor * time_cost
- time_scost.name = 'time_scost'
-
- cost = dest_cost + time_scost
-
- if hasattr(config, 'dropout_p'):
- cg = ComputationGraph(cost)
- dropout_inputs = VariableFilter(
- bricks=[b for b in list(common_mlp.children) +
- list(dest_mlp.children) +
- list(time_mlp.children)
- if isinstance(b, Rectifier)],
- name='output')(cg)
- cg = apply_dropout(cg, dropout_inputs, config.dropout_p)
- cost = cg.outputs[0]
-
- cost.name = 'cost'
-
- # Initialization
- for tbl in embed_tables:
- tbl.weights_init = config.embed_weights_init
- tbl.initialize()
-
- for mlp in [common_mlp, dest_mlp, time_mlp]:
- mlp.weights_init = config.mlp_weights_init
- mlp.biases_init = config.mlp_biases_init
- mlp.initialize()
-
- self.cost = cost
- self.monitor = [cost, dest_cost, dest_hcost, time_cost, time_scost]
- self.outputs = tensor.concatenate([dest_outputs, time_outputs[:, None]], axis=1)
- self.outputs.name = 'outputs'
- self.pred_vars = ['destination_longitude', 'destination_latitude', 'travel_time']
+ 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/mlp.py b/model/mlp.py
new file mode 100644
index 0000000..9c84ef9
--- /dev/null
+++ b/model/mlp.py
@@ -0,0 +1,103 @@
+from theano import tensor
+
+from fuel.transformers import Batch
+from fuel.streams import DataStream
+from fuel.schemes import ConstantScheme, ShuffledExampleScheme
+from blocks.bricks import application, MLP, Rectifier, Initializable
+
+import data
+from data import transformers
+from data.hdf5 import TaxiDataset, TaxiStream
+from model import ContextEmbedder
+
+
+class FFMLP(Initializable):
+ def __init__(self, config, output_layer=None, **kwargs):
+ super(FFMLP, self).__init__(**kwargs)
+ self.config = config
+
+ self.context_embedder = ContextEmbedder(config)
+
+ output_activation = [] if output_layer is None else [output_layer()]
+ output_dim = [] if output_layer is None else [config.dim_output]
+ self.mlp = MLP(activations=[Rectifier() for _ in config.dim_hidden] + output_activation,
+ dims=[config.dim_input] + config.dim_hidden + output_dim)
+
+ self.extremities = {'%s_k_%s' % (side, ['latitude', 'longitude'][axis]): axis for side in ['first', 'last'] for axis in [0, 1]}
+ self.inputs = self.context_embedder.inputs + self.extremities.keys()
+ self.children = [ self.context_embedder, self.mlp ]
+
+ def _push_initialization_config(self):
+ self.mlp.weights_init = self.config.mlp_weights_init
+ self.mlp.biases_init = self.config.mlp_biases_init
+
+ @application(outputs=['prediction'])
+ def predict(self, **kwargs):
+ embeddings = tuple(self.context_embedder.apply(**{k: kwargs[k] for k in self.context_embedder.inputs }))
+ extremities = tuple((kwargs[k] - data.train_gps_mean[v]) / data.train_gps_std[v] for k, v in self.extremities.items())
+
+ inputs = tensor.concatenate(extremities + embeddings, axis=1)
+ outputs = self.mlp.apply(inputs)
+
+ return outputs
+
+ @predict.property('inputs')
+ def predict_inputs(self):
+ return self.inputs
+
+class Stream(object):
+ def __init__(self, config):
+ self.config = config
+
+ def train(self, req_vars):
+ stream = TaxiDataset('train')
+ stream = DataStream(stream, iteration_scheme=ShuffledExampleScheme(stream.num_examples))
+
+ valid = TaxiDataset(self.config.valid_set, 'valid.hdf5', sources=('trip_id',))
+ valid_trips_ids = valid.get_data(None, slice(0, valid.num_examples))[0]
+
+ stream = transformers.TaxiExcludeTrips(valid_trips_ids, stream)
+ stream = transformers.TaxiGenerateSplits(stream, max_splits=100)
+
+ stream = transformers.TaxiAddDateTime(stream)
+ stream = transformers.TaxiAddFirstLastLen(self.config.n_begin_end_pts, stream)
+ stream = transformers.Select(stream, tuple(req_vars))
+ return Batch(stream, iteration_scheme=ConstantScheme(self.config.batch_size))
+
+ def valid(self, req_vars):
+ stream = TaxiStream(self.config.valid_set, 'valid.hdf5')
+
+ stream = transformers.TaxiAddDateTime(stream)
+ stream = transformers.TaxiAddFirstLastLen(self.config.n_begin_end_pts, stream)
+ stream = transformers.Select(stream, tuple(req_vars))
+ return Batch(stream, iteration_scheme=ConstantScheme(1000))
+
+ def test(self, req_vars):
+ stream = TaxiStream('test')
+
+ stream = transformers.TaxiAddDateTime(stream)
+ stream = transformers.TaxiAddFirstLastLen(self.config.n_begin_end_pts, stream)
+
+ return Batch(stream, iteration_scheme=ConstantScheme(1))
+
+ def inputs(self):
+ return {'call_type': tensor.bvector('call_type'),
+ 'origin_call': tensor.ivector('origin_call'),
+ 'origin_stand': tensor.bvector('origin_stand'),
+ 'taxi_id': tensor.wvector('taxi_id'),
+ 'timestamp': tensor.ivector('timestamp'),
+ 'day_type': tensor.bvector('day_type'),
+ 'missing_data': tensor.bvector('missing_data'),
+ 'latitude': tensor.matrix('latitude'),
+ 'longitude': tensor.matrix('longitude'),
+ 'destination_latitude': tensor.vector('destination_latitude'),
+ 'destination_longitude': tensor.vector('destination_longitude'),
+ 'travel_time': tensor.ivector('travel_time'),
+ 'first_k_latitude': tensor.matrix('first_k_latitude'),
+ 'first_k_longitude': tensor.matrix('first_k_longitude'),
+ 'last_k_latitude': tensor.matrix('last_k_latitude'),
+ 'last_k_longitude': tensor.matrix('last_k_longitude'),
+ 'input_time': tensor.ivector('input_time'),
+ 'week_of_year': tensor.bvector('week_of_year'),
+ 'day_of_week': tensor.bvector('day_of_week'),
+ 'qhour_of_day': tensor.bvector('qhour_of_day')}
diff --git a/model/time_simple_mlp.py b/model/time_simple_mlp.py
index 9e1b10a..0ce6b29 100644
--- a/model/time_simple_mlp.py
+++ b/model/time_simple_mlp.py
@@ -1,63 +1,29 @@
-from blocks.bricks import MLP, Rectifier, Linear, Sigmoid, Identity
-from blocks.bricks.lookup import LookupTable
+from blocks.bricks import application, Identity
-from theano import tensor
-
-import data
import error
+from model.mlp import FFMLP, Stream
-class Model(object):
- def __init__(self, config):
- # The input and the targets
- x_firstk_latitude = (tensor.matrix('first_k_latitude') - data.train_gps_mean[0]) / data.train_gps_std[0]
- x_firstk_longitude = (tensor.matrix('first_k_longitude') - data.train_gps_mean[1]) / data.train_gps_std[1]
-
- x_lastk_latitude = (tensor.matrix('last_k_latitude') - data.train_gps_mean[0]) / data.train_gps_std[0]
- x_lastk_longitude = (tensor.matrix('last_k_longitude') - data.train_gps_mean[1]) / data.train_gps_std[1]
-
- input_list = [x_firstk_latitude, x_firstk_longitude, x_lastk_latitude, x_lastk_longitude]
- embed_tables = []
-
- self.require_inputs = ['first_k_latitude', 'first_k_longitude', 'last_k_latitude', 'last_k_longitude']
-
- for (varname, num, dim) in config.dim_embeddings:
- self.require_inputs.append(varname)
- vardata = tensor.lvector(varname)
- tbl = LookupTable(length=num, dim=dim, name='%s_lookup'%varname)
- embed_tables.append(tbl)
- input_list.append(tbl.apply(vardata))
-
- y = tensor.lvector('travel_time')
-
- # Define the model
- mlp = MLP(activations=[Rectifier() for _ in config.dim_hidden] + [Identity()],
- dims=[config.dim_input] + config.dim_hidden + [config.dim_output])
-
- # Create the Theano variables
- inputs = tensor.concatenate(input_list, axis=1)
- # inputs = theano.printing.Print("inputs")(inputs)
- outputs = config.exp_base ** mlp.apply(inputs)
-
- # outputs = theano.printing.Print("outputs")(outputs)
- # y = theano.printing.Print("y")(y)
- outputs.name = 'outputs'
+class Model(FFMLP):
+ def __init__(self, config, **kwargs):
+ super(Model, self).__init__(config, output_layer=Identity, **kwargs)
+ self.inputs.append('input_time')
- # Calculate the cost
- cost = error.rmsle(outputs.flatten(), y.flatten())
- cost.name = 'cost'
+ @application(outputs=['duration'])
+ def predict(self, **kwargs):
+ outputs = super(Model, self).predict(**kwargs)
+ return kwargs['input_time'] + self.config.exp_base ** outputs
- # Initialization
- for tbl in embed_tables:
- tbl.weights_init = config.embed_weights_init
- mlp.weights_init = config.mlp_weights_init
- mlp.biases_init = config.mlp_biases_init
+ @predict.property('inputs')
+ def predict_inputs(self):
+ return self.inputs
- for tbl in embed_tables:
- tbl.initialize()
- mlp.initialize()
+ @application(outputs=['cost'])
+ def cost(self, **kwargs):
+ y_hat = self.predict(**kwargs)
+ y = kwargs['travel_time']
+ return error.rmsle(y_hat.flatten(), y.flatten())
- self.cost = cost
- self.monitor = [cost]
- self.outputs = outputs
- self.pred_vars = ['travel_time']
+ @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
index 1f1eab7..35c8d8a 100644
--- a/model/time_simple_mlp_tgtcls.py
+++ b/model/time_simple_mlp_tgtcls.py
@@ -1,67 +1,33 @@
-from blocks.bricks import MLP, Rectifier, Linear, Sigmoid, Identity, Softmax
-from blocks.bricks.lookup import LookupTable
-
import numpy
import theano
from theano import tensor
+from blocks.bricks import application, Softmax
-import data
import error
+from model.mlp import FFMLP, Stream
-class Model(object):
- def __init__(self, config):
- # The input and the targets
- x_firstk_latitude = (tensor.matrix('first_k_latitude') - data.train_gps_mean[0]) / data.train_gps_std[0]
- x_firstk_longitude = (tensor.matrix('first_k_longitude') - data.train_gps_mean[1]) / data.train_gps_std[1]
-
- x_lastk_latitude = (tensor.matrix('last_k_latitude') - data.train_gps_mean[0]) / data.train_gps_std[0]
- x_lastk_longitude = (tensor.matrix('last_k_longitude') - data.train_gps_mean[1]) / data.train_gps_std[1]
-
- input_list = [x_firstk_latitude, x_firstk_longitude, x_lastk_latitude, x_lastk_longitude]
- embed_tables = []
-
- self.require_inputs = ['first_k_latitude', 'first_k_longitude', 'last_k_latitude', 'last_k_longitude']
-
- for (varname, num, dim) in config.dim_embeddings:
- self.require_inputs.append(varname)
- vardata = tensor.lvector(varname)
- tbl = LookupTable(length=num, dim=dim, name='%s_lookup'%varname)
- embed_tables.append(tbl)
- input_list.append(tbl.apply(vardata))
-
- y = tensor.lvector('travel_time')
-
- # Define the model
- mlp = MLP(activations=[Rectifier() for _ in config.dim_hidden] + [Softmax()],
- dims=[config.dim_input] + config.dim_hidden + [config.dim_output])
- classes = theano.shared(numpy.array(config.tgtcls, dtype=theano.config.floatX), name='classes')
-
- # Create the Theano variables
- inputs = tensor.concatenate(input_list, axis=1)
- # inputs = theano.printing.Print("inputs")(inputs)
- cls_probas = mlp.apply(inputs)
- outputs = tensor.dot(cls_probas, classes)
-
- # outputs = theano.printing.Print("outputs")(outputs)
- # y = theano.printing.Print("y")(y)
- outputs.name = 'outputs'
+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')
- # Calculate the cost
- cost = error.rmsle(outputs.flatten(), y.flatten())
- cost.name = 'cost'
+ @application(outputs=['duration'])
+ def predict(self, **kwargs):
+ cls_probas = super(Model, self).predict(**kwargs)
+ return kwargs['input_time'] + tensor.dot(cls_probas, self.classes)
- # Initialization
- for tbl in embed_tables:
- tbl.weights_init = config.embed_weights_init
- mlp.weights_init = config.mlp_weights_init
- mlp.biases_init = config.mlp_biases_init
+ @predict.property('inputs')
+ def predict_inputs(self):
+ return self.inputs
- for tbl in embed_tables:
- tbl.initialize()
- mlp.initialize()
+ @application(outputs=['cost'])
+ def cost(self, **kwargs):
+ y_hat = self.predict(**kwargs)
+ y = kwargs['travel_time']
+ return error.rmsle(y_hat.flatten(), y.flatten())
- self.cost = cost
- self.monitor = [cost]
- self.outputs = outputs
- self.pred_vars = ['travel_time']
+ @cost.property('inputs')
+ def cost_inputs(self):
+ return self.inputs + ['travel_time']