aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlex Auvolat <alex.auvolat@ens.fr>2015-05-04 16:58:17 -0400
committerAlex Auvolat <alex.auvolat@ens.fr>2015-05-04 16:58:32 -0400
commitc912ef9424be973b11b4c7b7dbb2d32a8f3a9ab9 (patch)
treee60277aab59c93b4cc94026cd5c25d967a195251
parent80d3ea67a845484d119cb88f0a0412f981ab344c (diff)
downloadtaxi-c912ef9424be973b11b4c7b7dbb2d32a8f3a9ab9.tar.gz
taxi-c912ef9424be973b11b4c7b7dbb2d32a8f3a9ab9.zip
Restructure model & config
-rw-r--r--config/simple_mlp_0.py (renamed from config/model_0.py)2
-rw-r--r--model/__init__.py0
-rw-r--r--model/simple_mlp.py69
-rw-r--r--train.py (renamed from model.py)67
4 files changed, 76 insertions, 62 deletions
diff --git a/config/model_0.py b/config/simple_mlp_0.py
index c4985b2..61ddbfd 100644
--- a/config/model_0.py
+++ b/config/simple_mlp_0.py
@@ -1,3 +1,5 @@
+import model.simple_mlp as model
+
n_dow = 7 # number of division for dayofweek/dayofmonth/hourofday
n_dom = 31
n_hour = 24
diff --git a/model/__init__.py b/model/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/model/__init__.py
diff --git a/model/simple_mlp.py b/model/simple_mlp.py
new file mode 100644
index 0000000..896ccd3
--- /dev/null
+++ b/model/simple_mlp.py
@@ -0,0 +1,69 @@
+from blocks.bricks import MLP, Rectifier, Linear, Sigmoid, Identity
+from blocks.bricks.lookup import LookupTable
+
+from blocks.initialization import IsotropicGaussian, Constant
+
+from theano import tensor
+
+import data
+import hdist
+
+class Model(object):
+ def __init__(self, config):
+ # The input and the targets
+ x_firstk_latitude = (tensor.matrix('first_k_latitude') - data.porto_center[0]) / data.data_std[0]
+ x_firstk_longitude = (tensor.matrix('first_k_longitude') - data.porto_center[1]) / data.data_std[1]
+
+ x_lastk_latitude = (tensor.matrix('last_k_latitude') - data.porto_center[0]) / data.data_std[0]
+ x_lastk_longitude = (tensor.matrix('last_k_longitude') - data.porto_center[1]) / data.data_std[1]
+
+ x_client = tensor.lvector('origin_call')
+ x_stand = tensor.lvector('origin_stand')
+
+ y = tensor.concatenate((tensor.vector('destination_latitude')[:, None],
+ tensor.vector('destination_longitude')[:, None]), axis=1)
+
+ # Define the model
+ client_embed_table = LookupTable(length=data.n_train_clients+1, dim=config.dim_embed, name='client_lookup')
+ stand_embed_table = LookupTable(length=data.n_stands+1, dim=config.dim_embed, name='stand_lookup')
+ mlp = MLP(activations=[Rectifier() for _ in config.dim_hidden] + [Identity()],
+ dims=[config.dim_input] + config.dim_hidden + [config.dim_output])
+
+ # Create the Theano variables
+ client_embed = client_embed_table.apply(x_client)
+ stand_embed = stand_embed_table.apply(x_stand)
+ inputs = tensor.concatenate([x_firstk_latitude, x_firstk_longitude,
+ x_lastk_latitude, x_lastk_longitude,
+ client_embed, stand_embed],
+ axis=1)
+ # inputs = theano.printing.Print("inputs")(inputs)
+ outputs = mlp.apply(inputs)
+
+ # Normalize & Center
+ # outputs = theano.printing.Print("normal_outputs")(outputs)
+ outputs = data.data_std * outputs + data.porto_center
+
+ # outputs = theano.printing.Print("outputs")(outputs)
+ # y = theano.printing.Print("y")(y)
+
+ outputs.name = 'outputs'
+
+ # Calculate the cost
+ cost = hdist.erdist(outputs, y).mean()
+ cost.name = 'cost'
+ hcost = hdist.hdist(outputs, y).mean()
+ hcost.name = 'hcost'
+
+ # Initialization
+ client_embed_table.weights_init = IsotropicGaussian(0.001)
+ stand_embed_table.weights_init = IsotropicGaussian(0.001)
+ mlp.weights_init = IsotropicGaussian(0.01)
+ mlp.biases_init = Constant(0.001)
+
+ client_embed_table.initialize()
+ stand_embed_table.initialize()
+ mlp.initialize()
+
+ self.cost = cost
+ self.hcost = hcost
+ self.outputs = outputs
diff --git a/model.py b/train.py
index 744d877..1b39671 100644
--- a/model.py
+++ b/train.py
@@ -15,10 +15,6 @@ from theano.ifelse import ifelse
from blocks.filter import VariableFilter
-from blocks.bricks import MLP, Rectifier, Linear, Sigmoid, Identity
-from blocks.bricks.lookup import LookupTable
-
-from blocks.initialization import IsotropicGaussian, Constant
from blocks.model import Model
from fuel.datasets.hdf5 import H5PYDataset
@@ -88,64 +84,11 @@ def setup_test_stream():
def main():
- # The input and the targets
- x_firstk_latitude = (tensor.matrix('first_k_latitude') - data.porto_center[0]) / data.data_std[0]
- x_firstk_longitude = (tensor.matrix('first_k_longitude') - data.porto_center[1]) / data.data_std[1]
-
- x_lastk_latitude = (tensor.matrix('last_k_latitude') - data.porto_center[0]) / data.data_std[0]
- x_lastk_longitude = (tensor.matrix('last_k_longitude') - data.porto_center[1]) / data.data_std[1]
-
- x_client = tensor.lvector('origin_call')
- x_stand = tensor.lvector('origin_stand')
-
- y = tensor.concatenate((tensor.vector('destination_latitude')[:, None],
- tensor.vector('destination_longitude')[:, None]), axis=1)
-
- # x_firstk_latitude = theano.printing.Print("x_firstk_latitude")(x_firstk_latitude)
- # x_firstk_longitude = theano.printing.Print("x_firstk_longitude")(x_firstk_longitude)
- # x_lastk_latitude = theano.printing.Print("x_lastk_latitude")(x_lastk_latitude)
- # x_lastk_longitude = theano.printing.Print("x_lastk_longitude")(x_lastk_longitude)
-
- # Define the model
- client_embed_table = LookupTable(length=data.n_train_clients+1, dim=config.dim_embed, name='client_lookup')
- stand_embed_table = LookupTable(length=data.n_stands+1, dim=config.dim_embed, name='stand_lookup')
- mlp = MLP(activations=[Rectifier() for _ in config.dim_hidden] + [Identity()],
- dims=[config.dim_input] + config.dim_hidden + [config.dim_output])
-
- # Create the Theano variables
- client_embed = client_embed_table.apply(x_client)
- stand_embed = stand_embed_table.apply(x_stand)
- inputs = tensor.concatenate([x_firstk_latitude, x_firstk_longitude,
- x_lastk_latitude, x_lastk_longitude,
- client_embed, stand_embed],
- axis=1)
- # inputs = theano.printing.Print("inputs")(inputs)
- outputs = mlp.apply(inputs)
-
- # Normalize & Center
- # outputs = theano.printing.Print("normal_outputs")(outputs)
- outputs = data.data_std * outputs + data.porto_center
-
- # outputs = theano.printing.Print("outputs")(outputs)
- # y = theano.printing.Print("y")(y)
-
- outputs.name = 'outputs'
-
- # Calculate the cost
- cost = (outputs - y).norm(2, axis=1).mean()
- cost.name = 'cost'
- hcost = hdist.hdist(outputs, y).mean()
- hcost.name = 'hcost'
-
- # Initialization
- client_embed_table.weights_init = IsotropicGaussian(0.001)
- stand_embed_table.weights_init = IsotropicGaussian(0.001)
- mlp.weights_init = IsotropicGaussian(0.01)
- mlp.biases_init = Constant(0.001)
-
- client_embed_table.initialize()
- stand_embed_table.initialize()
- mlp.initialize()
+ model = config.model.Model(config)
+
+ cost = model.cost
+ hcost = model.hcost
+ outputs = model.outputs
train_stream = setup_train_stream()
valid_stream = setup_valid_stream()