forked from snap-stanford/GraphGym
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.py
31 lines (23 loc) · 961 Bytes
/
example.py
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
import torch.nn as nn
import torch.nn.functional as F
from graphgym.config import cfg
from graphgym.register import register_stage
from graphgym.models.layer import GeneralLayer
def GNNLayer(dim_in, dim_out, has_act=True):
return GeneralLayer(cfg.gnn.layer_type, dim_in, dim_out, has_act)
class GNNStackStage(nn.Module):
'''Simple Stage that stack GNN layers'''
def __init__(self, dim_in, dim_out, num_layers):
super(GNNStackStage, self).__init__()
for i in range(num_layers):
d_in = dim_in if i == 0 else dim_out
layer = GNNLayer(d_in, dim_out)
self.add_module('layer{}'.format(i), layer)
self.dim_out = dim_out
def forward(self, batch):
for layer in self.children():
batch = layer(batch)
if cfg.gnn.l2norm:
batch.node_feature = F.normalize(batch.node_feature, p=2, dim=-1)
return batch
register_stage('example', GNNStackStage)