memory_network_mlp.py (2920B)
1 2 from theano import tensor 3 4 from fuel.transformers import Batch, MultiProcessing, Merge 5 from fuel.streams import DataStream 6 from fuel.schemes import ConstantScheme, ShuffledExampleScheme, SequentialExampleScheme 7 from blocks.bricks import application, MLP, Rectifier, Initializable, Softmax 8 9 import data 10 from data import transformers 11 from data.cut import TaxiTimeCutScheme 12 from data.hdf5 import TaxiDataset, TaxiStream 13 import error 14 from model import ContextEmbedder 15 16 from memory_network import StreamSimple as Stream 17 from memory_network import MemoryNetworkBase 18 19 class MLPEncoder(Initializable): 20 def __init__(self, config, output_dim, activation, **kwargs): 21 super(MLPEncoder, self).__init__(**kwargs) 22 23 self.config = config 24 self.context_embedder = ContextEmbedder(self.config) 25 26 self.encoder_mlp = MLP(activations=[Rectifier() for _ in config.dim_hidden] 27 + [activation()], 28 dims=[config.dim_input] 29 + config.dim_hidden 30 + [output_dim], 31 name='encoder') 32 33 self.extremities = {'%s_k_%s' % (side, ['latitude', 'longitude'][axis]): axis 34 for side in ['first', 'last'] for axis in [0, 1]} 35 36 self.children = [ self.context_embedder, 37 self.encoder_mlp ] 38 39 def _push_initialization_config(self): 40 for brick in [self.context_embedder, self.encoder_mlp]: 41 brick.weights_init = self.config.weights_init 42 brick.biases_init = self.config.biases_init 43 44 @application 45 def apply(self, **kwargs): 46 embeddings = tuple(self.context_embedder.apply( 47 **{k: kwargs[k] for k in self.context_embedder.inputs })) 48 extremities = tuple((kwargs[k] - data.train_gps_mean[v]) / data.train_gps_std[v] 49 for k, v in self.extremities.items()) 50 inputs = tensor.concatenate(extremities + embeddings, axis=1) 51 52 return self.encoder_mlp.apply(inputs) 53 54 @apply.property('inputs') 55 def apply_inputs(self): 56 return self.context_embedder.inputs + self.extremities.keys() 57 58 59 class Model(MemoryNetworkBase): 60 def __init__(self, config, **kwargs): 61 prefix_encoder = MLPEncoder(config.prefix_encoder, 62 config.representation_size, 63 config.representation_activation, 64 name='prefix_encoder') 65 66 candidate_encoder = MLPEncoder(config.candidate_encoder, 67 config.representation_size, 68 config.representation_activation, 69 name='candidate_encoder') 70 71 super(Model, self).__init__(config, prefix_encoder, candidate_encoder, **kwargs)