commit 5f42c01231ccec377196472b6f4682b6afeb878d
parent c912ef9424be973b11b4c7b7dbb2d32a8f3a9ab9
Author: Alex Auvolat <alex.auvolat@ens.fr>
Date: Mon, 4 May 2015 17:13:08 -0400
Add model with predefined target classes
Diffstat:
4 files changed, 104 insertions(+), 3 deletions(-)
diff --git a/.gitignore b/.gitignore
@@ -66,3 +66,4 @@ target/
# saved params
taxi_model/*
+model_data/*
diff --git a/config/simple_mlp_tgtcls_0.py b/config/simple_mlp_tgtcls_0.py
@@ -0,0 +1,25 @@
+import cPickle
+
+import data
+
+import model.simple_mlp_tgtcls as model
+
+n_dow = 7 # number of division for dayofweek/dayofmonth/hourofday
+n_dom = 31
+n_hour = 24
+
+n_begin_end_pts = 5 # how many points we consider at the beginning and end of the known trajectory
+n_end_pts = 5
+
+n_valid = 1000
+
+with open(data.DATA_PATH + "/arrival-clusters.pkl") as f: tgtcls = cPickle.load(f)
+
+dim_embed = 10
+dim_input = n_begin_end_pts * 2 * 2 + dim_embed + dim_embed
+dim_hidden = [200]
+dim_output = tgtcls.shape[0]
+
+learning_rate = 0.0001
+momentum = 0.99
+batch_size = 32
diff --git a/model/simple_mlp_tgtcls.py b/model/simple_mlp_tgtcls.py
@@ -0,0 +1,74 @@
+import numpy
+
+import theano
+from theano import tensor
+
+from blocks.bricks import MLP, Rectifier, Linear, Sigmoid, Identity, Softmax
+from blocks.bricks.lookup import LookupTable
+
+from blocks.initialization import IsotropicGaussian, Constant
+
+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] + [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
+ 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)
+ cls_probas = mlp.apply(inputs)
+ outputs = tensor.dot(cls_probas, classes)
+
+ # 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/train.py b/train.py
@@ -38,7 +38,8 @@ if __name__ == "__main__":
if len(sys.argv) != 2:
print >> sys.stderr, 'Usage: %s config' % sys.argv[0]
sys.exit(1)
- config = importlib.import_module(sys.argv[1])
+ model_name = sys.argv[1]
+ config = importlib.import_module(model_name)
def setup_train_stream():
@@ -107,8 +108,8 @@ def main():
every_n_batches=1000),
Printing(every_n_batches=1000),
# Checkpoint('model.pkl', every_n_batches=100),
- Dump('taxi_model', every_n_batches=1000),
- LoadFromDump('taxi_model'),
+ Dump('model_data/' + model_name, every_n_batches=1000),
+ LoadFromDump('model_data/' + model_name),
FinishAfter(after_epoch=5)
]