diff options
-rw-r--r-- | config/dest_simple_mlp_tgtcls_1_cswdtx_alexandre.py | 30 | ||||
-rw-r--r-- | model/dest_simple_mlp_tgtcls_alexandre.py | 75 | ||||
-rw-r--r-- | train.py | 5 |
3 files changed, 108 insertions, 2 deletions
diff --git a/config/dest_simple_mlp_tgtcls_1_cswdtx_alexandre.py b/config/dest_simple_mlp_tgtcls_1_cswdtx_alexandre.py new file mode 100644 index 0000000..91ad71c --- /dev/null +++ b/config/dest_simple_mlp_tgtcls_1_cswdtx_alexandre.py @@ -0,0 +1,30 @@ +import cPickle + +import data + +import model.dest_simple_mlp_tgtcls_alexandre as model + +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_embeddings = [ + ('origin_call', data.n_train_clients+1, 10), + ('origin_stand', data.n_stands+1, 10), + ('week_of_year', 52, 10), + ('day_of_week', 7, 10), + ('qhour_of_day', 24 * 4, 10), + ('day_type', 3, 10), + ('taxi_id', 448, 10), +] + +dim_input = n_begin_end_pts * 2 * 2 + sum(x for (_, _, x) in dim_embeddings) +dim_hidden = [500] +dim_output = tgtcls.shape[0] + +learning_rate = 0.01 +momentum = 0.9 +batch_size = 200 diff --git a/model/dest_simple_mlp_tgtcls_alexandre.py b/model/dest_simple_mlp_tgtcls_alexandre.py new file mode 100644 index 0000000..87e20a3 --- /dev/null +++ b/model/dest_simple_mlp_tgtcls_alexandre.py @@ -0,0 +1,75 @@ +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 error + +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] + + 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' + + # Calculate the cost + cost = error.erdist(outputs, y).mean() + cost.name = 'cost' + hcost = error.hdist(outputs, y).mean() + hcost.name = 'hcost' + + # Initialization + for tbl in embed_tables: + tbl.weights_init = IsotropicGaussian(0.01) + mlp.weights_init = IsotropicGaussian(0.1) + mlp.biases_init = Constant(0.01) + + for tbl in embed_tables: + tbl.initialize() + mlp.initialize() + + self.cost = cost + self.monitor = [cost, hcost] + self.outputs = outputs + self.pred_vars = ['destination_latitude', 'destination_longitude'] + @@ -27,7 +27,7 @@ from blocks.graph import ComputationGraph from blocks.main_loop import MainLoop from blocks.extensions import Printing, FinishAfter from blocks.extensions.saveload import Dump, LoadFromDump, Checkpoint -from blocks.extensions.monitoring import DataStreamMonitoring +from blocks.extensions.monitoring import DataStreamMonitoring, TrainingDataMonitoring import data import transformers @@ -107,7 +107,8 @@ def main(): step_rule=Momentum(learning_rate=config.learning_rate, momentum=config.momentum), params=params) - extensions=[DataStreamMonitoring(model.monitor, valid_stream, + extensions=[TrainingDataMonitoring(model.monitor, prefix='train', every_n_batches=1000), + DataStreamMonitoring(model.monitor, valid_stream, prefix='valid', every_n_batches=1000), Printing(every_n_batches=1000), |