gnn.py 2.58 KB
Newer Older
1
2
import copy
import itertools
GaiYu0's avatar
GaiYu0 committed
3
4
import dgl
import dgl.function as fn
5
6
7
import networkx as nx
import torch as th
import torch.nn as nn
GaiYu0's avatar
GaiYu0 committed
8
import torch.nn.functional as F
HQ's avatar
HQ committed
9
import numpy as np
10

GaiYu0's avatar
GaiYu0 committed
11
class GNNModule(nn.Module):
12
13
    def __init__(self, in_feats, out_feats, radius):
        super().__init__()
GaiYu0's avatar
GaiYu0 committed
14
        self.out_feats = out_feats
15
16
        self.radius = radius

17
18
        new_linear = lambda: nn.Linear(in_feats, out_feats)
        new_linear_list = lambda: nn.ModuleList([new_linear() for i in range(radius)])
19

GaiYu0's avatar
GaiYu0 committed
20
21
        self.theta_x, self.theta_deg, self.theta_y = \
            new_linear(), new_linear(), new_linear()
22
        self.theta_list = new_linear_list()
23

GaiYu0's avatar
GaiYu0 committed
24
25
        self.gamma_y, self.gamma_deg, self.gamma_x = \
            new_linear(), new_linear(), new_linear()
26
        self.gamma_list = new_linear_list()
27

GaiYu0's avatar
GaiYu0 committed
28
29
        self.bn_x = nn.BatchNorm1d(out_feats)
        self.bn_y = nn.BatchNorm1d(out_feats)
30

GaiYu0's avatar
GaiYu0 committed
31
32
    def aggregate(self, g, z):
        z_list = []
33
        g.ndata['z'] = z
34
        g.update_all(fn.copy_src(src='z', out='m'), fn.sum(msg='m', out='z'))
35
        z_list.append(g.ndata['z'])
GaiYu0's avatar
GaiYu0 committed
36
37
        for i in range(self.radius - 1):
            for j in range(2 ** i):
38
                g.update_all(fn.copy_src(src='z', out='m'), fn.sum(msg='m', out='z'))
39
            z_list.append(g.ndata['z'])
GaiYu0's avatar
GaiYu0 committed
40
        return z_list
41

42
43
    def forward(self, g, lg, x, y, deg_g, deg_lg, pm_pd):
        pmpd_x = F.embedding(pm_pd, x)
44

45
        sum_x = sum(theta(z) for theta, z in zip(self.theta_list, self.aggregate(g, x)))
GaiYu0's avatar
GaiYu0 committed
46

47
        g.edata['y'] = y
48
        g.update_all(fn.copy_edge(edge='y', out='m'), fn.sum('m', 'pmpd_y'))
49
        pmpd_y = g.ndata.pop('pmpd_y')
GaiYu0's avatar
GaiYu0 committed
50

51
52
53
54
        x = self.theta_x(x) + self.theta_deg(deg_g * x) + sum_x + self.theta_y(pmpd_y)
        n = self.out_feats // 2
        x = th.cat([x[:, :n], F.relu(x[:, n:])], 1)
        x = self.bn_x(x)
55

56
        sum_y = sum(gamma(z) for gamma, z in zip(self.gamma_list, self.aggregate(lg, y)))
57

58
59
60
        y = self.gamma_y(y) + self.gamma_deg(deg_lg * y) + sum_y + self.gamma_x(pmpd_x)
        y = th.cat([y[:, :n], F.relu(y[:, n:])], 1)
        y = self.bn_y(y)
61

62
        return x, y
63
64

class GNN(nn.Module):
GaiYu0's avatar
GaiYu0 committed
65
    def __init__(self, feats, radius, n_classes):
GaiYu0's avatar
GaiYu0 committed
66
67
68
69
70
        super(GNN, self).__init__()
        self.linear = nn.Linear(feats[-1], n_classes)
        self.module_list = nn.ModuleList([GNNModule(m, n, radius)
                                          for m, n in zip(feats[:-1], feats[1:])])

71
72
    def forward(self, g, lg, deg_g, deg_lg, pm_pd):
        x, y = deg_g, deg_lg
GaiYu0's avatar
GaiYu0 committed
73
        for module in self.module_list:
74
            x, y = module(g, lg, x, y, deg_g, deg_lg, pm_pd)
GaiYu0's avatar
GaiYu0 committed
75
        return self.linear(x)