taxi

Winning entry to the Kaggle taxi competition
git clone https://esimon.eu/repos/taxi.git
Log | Files | Refs | README

joint_mlp_tgtcls.py (3041B)


      1 import numpy
      2 import theano
      3 from theano import tensor
      4 from blocks import roles
      5 from blocks.bricks import application, MLP, Rectifier, Softmax
      6 
      7 import error
      8 from model.mlp import FFMLP, Stream
      9 
     10 
     11 class Model(FFMLP):
     12     def __init__(self, config, **kwargs):
     13         super(Model, self).__init__(config, **kwargs)
     14         
     15         self.dest_mlp = MLP(activations=[Rectifier() for _ in config.dim_hidden_dest] + [Softmax()],
     16                        dims=[config.dim_hidden[-1]] + config.dim_hidden_dest + [config.dim_output_dest],
     17                        name='dest_mlp')
     18         self.time_mlp = MLP(activations=[Rectifier() for _ in config.dim_hidden_time] + [Softmax()],
     19                        dims=[config.dim_hidden[-1]] + config.dim_hidden_time + [config.dim_output_time],
     20                        name='time_mlp')
     21 
     22         self.dest_classes = theano.shared(numpy.array(config.dest_tgtcls, dtype=theano.config.floatX), name='dest_classes')
     23         self.time_classes = theano.shared(numpy.array(config.time_tgtcls, dtype=theano.config.floatX), name='time_classes')
     24 
     25         self.inputs.append('input_time')
     26         self.children.extend([self.dest_mlp, self.time_mlp])
     27 
     28     def _push_initialization_config(self):
     29         super(Model, self)._push_initialization_config()
     30         for mlp in [self.dest_mlp, self.time_mlp]:
     31             mlp.weights_init = self.config.mlp_weights_init
     32             mlp.biases_init = self.config.mlp_biases_init
     33 
     34     @application(outputs=['destination', 'duration'])
     35     def predict(self, **kwargs):
     36         hidden = super(Model, self).predict(**kwargs)
     37 
     38         dest_cls_probas = self.dest_mlp.apply(hidden)
     39         dest_outputs = tensor.dot(dest_cls_probas, self.dest_classes)
     40 
     41         time_cls_probas = self.time_mlp.apply(hidden)
     42         time_outputs = kwargs['input_time'] + tensor.dot(time_cls_probas, self.time_classes)
     43 
     44         self.add_auxiliary_variable(dest_cls_probas, name='destination classes ponderations')
     45         self.add_auxiliary_variable(time_cls_probas, name='time classes ponderations')
     46 
     47         return (dest_outputs, time_outputs)
     48 
     49     @predict.property('inputs')
     50     def predict_inputs(self):
     51         return self.inputs
     52 
     53     @application(outputs=['cost'])
     54     def cost(self, **kwargs):
     55         (destination_hat, time_hat) = self.predict(**kwargs)
     56 
     57         destination = tensor.concatenate((kwargs['destination_latitude'][:, None],
     58                                           kwargs['destination_longitude'][:, None]), axis=1)
     59         time = kwargs['travel_time']
     60 
     61         destination_cost = error.erdist(destination_hat, destination).mean()
     62         time_cost = error.rmsle(time_hat.flatten(), time.flatten())
     63 
     64         self.add_auxiliary_variable(destination_cost, [roles.COST], 'destination_cost')
     65         self.add_auxiliary_variable(time_cost, [roles.COST], 'time_cost')
     66 
     67         return destination_cost + self.config.time_cost_factor * time_cost
     68 
     69     @cost.property('inputs')
     70     def cost_inputs(self):
     71         return self.inputs + ['destination_latitude', 'destination_longitude', 'travel_time']