"vscode:/vscode.git/clone" did not exist on "13b8c5f3d50d1ea53f4baff11328f2717c0d67dd"
geniepath.py 8.64 KB
Newer Older
Ivan Brugere's avatar
Ivan Brugere committed
1
2
3
4
5
6
7
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Jul  9 13:34:38 2018

@author: ivabruge

Ivan Brugere's avatar
Ivan Brugere committed
8
9
10
11
12
13
14
GeniePath: Graph Neural Networks with Adaptive Receptive Paths
Paper: https://arxiv.org/abs/1802.00910

this model uses an LSTM on the node reductions of the message-passing step 

we store the network states at the graph node, since the LSTM variables are not transmitted

Ivan Brugere's avatar
Ivan Brugere committed
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
"""

from dgl.graph import DGLGraph
import torch
import torch.nn as nn
import torch.nn.functional as F
import argparse
from dataset import load_data, preprocess_features

class NodeReduceModule(nn.Module):
    def __init__(self, input_dim, num_hidden, num_heads=3, input_dropout=None,
            attention_dropout=None, act=lambda x: F.softmax(F.leaky_relu(x), dim=0)):
        super(NodeReduceModule, self).__init__()
        self.num_heads = num_heads
        self.input_dropout = input_dropout
        self.attention_dropout = attention_dropout
        self.act = act
        self.fc = nn.ModuleList(
                [nn.Linear(input_dim, num_hidden, bias=False)
                    for _ in range(num_heads)])
        self.attention = nn.ModuleList(
                [nn.Linear(num_hidden * 2, 1, bias=False) for _ in range(num_heads)])

    def forward(self, msgs):
        src, dst = zip(*msgs)
        hu = torch.cat(src, dim=0) # neighbor repr
        hv = torch.cat(dst, dim=0)

        msgs_repr = []

        # iterate for each head
        for i in range(self.num_heads):
            # calc W*hself and W*hneigh
            hvv = self.fc[i](hv)
            huu = self.fc[i](hu)
            # calculate W*hself||W*hneigh
            h = torch.cat((hvv, huu), dim=1)
            a = self.act(self.attention[i](h))
            if self.attention_dropout is not None:
                a = F.dropout(a, self.attention_dropout)
            if self.input_dropout is not None:
                hvv = F.dropout(hvv, self.input_dropout)
            h = torch.sum(a * hvv, 0, keepdim=True)
            msgs_repr.append(h)

        return msgs_repr


class NodeUpdateModule(nn.Module):
    def __init__(self, residual, fc, act, aggregator):
        super(NodeUpdateModule, self).__init__()
        
        self.residual = residual
        self.fc = fc
        self.act = act
        self.aggregator = aggregator

    def forward(self, node, msgs_repr):
        # apply residual connection and activation for each head
        for i in range(len(msgs_repr)):
            if self.residual:
                h = self.fc[i](node['h'])
                msgs_repr[i] = msgs_repr[i] + h
            if self.act is not None:
                msgs_repr[i] = self.act(msgs_repr[i])

        # aggregate multi-head results
        h = self.aggregator(msgs_repr)
        c0 = torch.zeros(h.shape)
        if node['c'] is None:
            c0 = torch.zeros(h.shape)
        else:
            c0 = node['c']
        if node['h_i'] is None:
            h0 = torch.zeros(h.shape)
        else:
            h0 = node['h_i']
        lstm = nn.LSTM(input_size=h.shape[1], hidden_size=h.shape[1], num_layers=1)
        
        #add dimension to handle sequential (create sequence of length 1)
        h, (h_i, c) = lstm(h.unsqueeze(0), (h0.unsqueeze(0), c0.unsqueeze(0)))
        
        #remove sequential dim
        h = torch.squeeze(h, 0)
        h_i = torch.squeeze(h, 0)
        c = torch.squeeze(c, 0)
        
        return {'h': h, 'c':c, 'h_i':h_i}

Ivan Brugere's avatar
Ivan Brugere committed
104
class GeniePath(nn.Module):
Ivan Brugere's avatar
Ivan Brugere committed
105
106
    def __init__(self, num_layers, in_dim, num_hidden, num_classes, num_heads,
            activation, input_dropout, attention_dropout, use_residual=False ):
Ivan Brugere's avatar
Ivan Brugere committed
107
        super(GeniePath, self).__init__()
Ivan Brugere's avatar
Ivan Brugere committed
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149

        self.input_dropout = input_dropout
        self.reduce_layers = nn.ModuleList()
        self.update_layers = nn.ModuleList()
        # hidden layers
        for i in range(num_layers):
            if i == 0:
                last_dim = in_dim
                residual = False
            else:
                last_dim = num_hidden * num_heads # because of concat heads
                residual = use_residual
            self.reduce_layers.append(
                    NodeReduceModule(last_dim, num_hidden, num_heads, input_dropout,
                        attention_dropout))
            self.update_layers.append(
                    NodeUpdateModule(residual, self.reduce_layers[-1].fc, activation,
                        lambda x: torch.cat(x, 1)))
        # projection
        self.reduce_layers.append(
            NodeReduceModule(num_hidden * num_heads, num_classes, 1, input_dropout,
                attention_dropout))
        self.update_layers.append(
            NodeUpdateModule(False, self.reduce_layers[-1].fc, None, sum))

    def forward(self, g):
        g.register_message_func(lambda src, dst, edge: (src['h'], dst['h']))
        for reduce_func, update_func in zip(self.reduce_layers, self.update_layers):
            # apply dropout
            if self.input_dropout is not None:
                # TODO (lingfan): use batched dropout once we have better api
                #                 for global manipulation
                for n in g.nodes():
                    g.node[n]['h'] = F.dropout(g.node[n]['h'], p=self.input_dropout)
                    g.node[n]['c'] = None
                    g.node[n]['h_i'] = None
            g.register_reduce_func(reduce_func)
            g.register_update_func(update_func)
            g.update_all()
        logits = [g.node[n]['h'] for n in g.nodes()]
        logits = torch.cat(logits, dim=0)
        return logits
Ivan Brugere's avatar
Ivan Brugere committed
150
151
    
    #train on graph g with features, and target labels. Accepts a loss function and an optimizer function which implements optimizer.step()
Ivan Brugere's avatar
Ivan Brugere committed
152
153
154
155
156
157
158
159
    def train(self, g, features, labels, epochs, loss_f=torch.nn.NLLLoss, loss_params={}, optimizer=torch.optim.Adam, optimizer_parameters=None, lr=0.001, ignore=[0], quiet=False):
        
        labels = torch.LongTensor(labels)
        _, labels = torch.max(labels, dim=1)
        # convert labels and masks to tensor
        
        if optimizer_parameters is None:
            optimizer_parameters = self.parameters()
Ivan Brugere's avatar
Ivan Brugere committed
160
161
            
        #instantiate optimizer on given params
Ivan Brugere's avatar
Ivan Brugere committed
162
163
164
165
166
167
168
169
170
171
172
173
        optimizer_f = optimizer(optimizer_parameters, lr)        
        
        for epoch in range(args.epochs):
            # reset grad
            optimizer_f.zero_grad()
    
            # reset graph states
            for n in g.nodes():
                g.node[n]['h'] = torch.FloatTensor(features[n].toarray())
    
            # forward
            logits = self.forward(g)
Ivan Brugere's avatar
Ivan Brugere committed
174
175
   
            #intantiate loss on passed parameters (e.g. class weight params)         
Ivan Brugere's avatar
Ivan Brugere committed
176
            loss = loss_f(**loss_params)
Ivan Brugere's avatar
Ivan Brugere committed
177
178
            
            #trim null labels
Ivan Brugere's avatar
Ivan Brugere committed
179
180
181
182
183
184
185
186
187
188
189
190
191
            idx = [i for i, a in enumerate(labels) if a not in ignore]
            logits = logits[idx, :]
            labels = labels[idx]
            out = loss(logits, labels)
            
            if not quiet:
                print("epoch {} loss: {}".format(epoch, out))
                
            out.backward()
            optimizer_f.step()

def main(args):
    # dropout parameters
Ivan Brugere's avatar
Ivan Brugere committed
192
193
    input_dropout = args.idrop
    attention_dropout = args.adrop
Ivan Brugere's avatar
Ivan Brugere committed
194
195
196
197
198
199
200
201
202

    # load and preprocess dataset
    adj, features, y_train, y_val, y_test, train_mask, val_mask, test_mask = load_data(args.dataset)
    features = preprocess_features(features)

    # initialize graph
    g = DGLGraph(adj)

    # create model
Ivan Brugere's avatar
Ivan Brugere committed
203
    model = GeniePath(args.num_layers,
Ivan Brugere's avatar
Ivan Brugere committed
204
205
206
207
208
209
210
211
                features.shape[1],
                args.num_hidden,
                y_train.shape[1],
                args.num_heads,
                F.elu,
                input_dropout,
                attention_dropout,
                args.residual)
Ivan Brugere's avatar
Ivan Brugere committed
212
    model.train(g, features, y_train, epochs=args.epochs)
Ivan Brugere's avatar
Ivan Brugere committed
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='GAT')
    parser.add_argument("--dataset", type=str, required=True,
            help="dataset name")
    parser.add_argument("--epochs", type=int, default=10,
            help="training epoch")
    parser.add_argument("--num-heads", type=int, default=3,
            help="number of attentional heads to use")
    parser.add_argument("--num-layers", type=int, default=1,
            help="number of hidden layers")
    parser.add_argument("--num-hidden", type=int, default=8,
            help="size of hidden units")
    parser.add_argument("--residual", action="store_true",
            help="use residual connection")
    parser.add_argument("--lr", type=float, default=0.001,
            help="learning rate")
Ivan Brugere's avatar
Ivan Brugere committed
230
231
232
233
234
    parser.add_argument("--idrop", type=float, default=0.2,
            help="Input dropout")
    parser.add_argument("--adrop", type=float, default=0.2,
            help="attention dropout")
    
Ivan Brugere's avatar
Ivan Brugere committed
235
236
237
238
    args = parser.parse_args()
    print(args)

    main(args)