time_mlp_tgtcls.py (1034B)
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, output_layer=Softmax).__init__(config, **kwargs) 13 self.classes = theano.shared(numpy.array(config.tgtcls, dtype=theano.config.floatX), name='classes') 14 self.inputs.append('input_time') 15 16 @application(outputs=['duration']) 17 def predict(self, **kwargs): 18 cls_probas = super(Model, self).predict(**kwargs) 19 return kwargs['input_time'] + tensor.dot(cls_probas, self.classes) 20 21 @predict.property('inputs') 22 def predict_inputs(self): 23 return self.inputs 24 25 @application(outputs=['cost']) 26 def cost(self, **kwargs): 27 y_hat = self.predict(**kwargs) 28 y = kwargs['travel_time'] 29 return error.rmsle(y_hat.flatten(), y.flatten()) 30 31 @cost.property('inputs') 32 def cost_inputs(self): 33 return self.inputs + ['travel_time']