aboutsummaryrefslogtreecommitdiff
path: root/model/simple_mlp_tgtcls.py
blob: 99a459a18b1e76ba29d57cf96cf0083f80818305 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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 hdist

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]

        x_client = tensor.lvector('origin_call')
        x_stand = tensor.lvector('origin_stand')

        y = tensor.concatenate((tensor.vector('destination_latitude')[:, None],
                                tensor.vector('destination_longitude')[:, None]), axis=1)

        # Define the model
        client_embed_table = LookupTable(length=data.n_train_clients+1, dim=config.dim_embed, name='client_lookup')
        stand_embed_table = LookupTable(length=data.n_stands+1, dim=config.dim_embed, name='stand_lookup')
        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
        client_embed = client_embed_table.apply(x_client)
        stand_embed = stand_embed_table.apply(x_stand)
        inputs = tensor.concatenate([x_firstk_latitude, x_firstk_longitude,
                                     x_lastk_latitude, x_lastk_longitude,
                                     client_embed, stand_embed],
                                    axis=1)
        # inputs = theano.printing.Print("inputs")(inputs)
        cls_probas = mlp.apply(inputs)
        outputs = tensor.dot(cls_probas, classes)

        # Normalize & Center
        # outputs = theano.printing.Print("normal_outputs")(outputs)
        outputs = data.data_std * outputs + data.porto_center

        # outputs = theano.printing.Print("outputs")(outputs)
        # y = theano.printing.Print("y")(y)

        outputs.name = 'outputs'

        # Calculate the cost
        cost = hdist.erdist(outputs, y).mean()
        cost.name = 'cost'
        hcost = hdist.hdist(outputs, y).mean()
        hcost.name = 'hcost'

        # Initialization
        client_embed_table.weights_init = IsotropicGaussian(0.001)
        stand_embed_table.weights_init = IsotropicGaussian(0.001)
        mlp.weights_init = IsotropicGaussian(0.01)
        mlp.biases_init = Constant(0.001)

        client_embed_table.initialize()
        stand_embed_table.initialize()
        mlp.initialize()

        self.cost = cost
        self.hcost = hcost
        self.outputs = outputs