taxi

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

dest_mlp_tgtcls.py (1118B)


      1 import numpy
      2 import theano
      3 from theano import tensor
      4 from blocks.bricks import application, Softmax
      5 
      6 import error
      7 from model.mlp import FFMLP, Stream
      8 
      9 
     10 class Model(FFMLP):
     11     def __init__(self, config, **kwargs):
     12         super(Model, self).__init__(config, output_layer=Softmax, **kwargs)
     13         self.classes = theano.shared(numpy.array(config.tgtcls, dtype=theano.config.floatX), name='classes')
     14 
     15     @application(outputs=['destination'])
     16     def predict(self, **kwargs):
     17         cls_probas = super(Model, self).predict(**kwargs)
     18         return tensor.dot(cls_probas, self.classes)
     19 
     20     @predict.property('inputs')
     21     def predict_inputs(self):
     22         return self.inputs
     23 
     24     @application(outputs=['cost'])
     25     def cost(self, **kwargs):
     26         y_hat = self.predict(**kwargs)
     27         y = tensor.concatenate((kwargs['destination_latitude'][:, None],
     28                                 kwargs['destination_longitude'][:, None]), axis=1)
     29 
     30         return error.erdist(y_hat, y).mean()
     31 
     32     @cost.property('inputs')
     33     def cost_inputs(self):
     34         return self.inputs + ['destination_latitude', 'destination_longitude']