aboutsummaryrefslogtreecommitdiff
path: root/model/dest_simple_mlp.py
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/dest_simple_mlp.py
parent1e6d08b0c9ac5983691b182631c71e9d46ee71cc (diff)
downloadtaxi-6d946f29f7548c75e97f30c4356dbac200ee6cce.tar.gz
taxi-6d946f29f7548c75e97f30c4356dbac200ee6cce.zip
Refactor models, clean the code and separate training from testing.
Diffstat (limited to 'model/dest_simple_mlp.py')
-rw-r--r--model/dest_simple_mlp.py81
1 files changed, 21 insertions, 60 deletions
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']