forked from PKU-DAIR/GAMLP
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlayer.py
135 lines (124 loc) · 4.63 KB
/
layer.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
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
104
105
106
107
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
import math
import os
import random
import time
import dgl
import dgl.function as fn
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.nn import Parameter
# adapted from https://github.com/chennnM/GBP
class Dense(nn.Module):
def __init__(self, in_features, out_features, bias='bn'):
super(Dense, self).__init__()
self.in_features = in_features
self.out_features = out_features
self.weight = nn.Parameter(torch.FloatTensor(in_features, out_features))
if bias == 'bn':
self.bias = nn.BatchNorm1d(out_features)
else:
self.bias = lambda x: x
self.reset_parameters()
def reset_parameters(self):
stdv = 1. / math.sqrt(self.weight.size(1))
self.weight.data.uniform_(-stdv, stdv)
def forward(self, input):
output = torch.mm(input, self.weight)
output = self.bias(output)
if self.in_features == self.out_features:
output = output + input
return output
# MLP apply initial residual
class GraphConvolution(nn.Module):
def __init__(self, in_features, out_features,alpha,bns=False):
super(GraphConvolution, self).__init__()
self.in_features = in_features
self.out_features = out_features
self.weight = Parameter(torch.FloatTensor(self.in_features,self.out_features))
self.alpha=alpha
self.reset_parameters()
self.bns=bns
self.bias = nn.BatchNorm1d(out_features)
def reset_parameters(self):
stdv = 1. / math.sqrt(self.out_features)
self.weight.data.uniform_(-stdv, stdv)
def forward(self, input ,h0):
support = (1-self.alpha)*input+self.alpha*h0
output = torch.mm(support, self.weight)
#if self.bns:
output=self.bias(output)
if self.in_features==self.out_features:
output = output+input
return output
# adapted from dgl sign
class FeedForwardNet(nn.Module):
def __init__(self, in_feats, hidden, out_feats, n_layers, dropout,bns=True):
super(FeedForwardNet, self).__init__()
self.layers = nn.ModuleList()
self.bns = nn.ModuleList()
self.n_layers = n_layers
if n_layers == 1:
self.layers.append(nn.Linear(in_feats, out_feats))
else:
self.layers.append(nn.Linear(in_feats, hidden))
self.bns.append(nn.BatchNorm1d(hidden))
for i in range(n_layers - 2):
self.layers.append(nn.Linear(hidden, hidden))
self.bns.append(nn.BatchNorm1d(hidden))
self.layers.append(nn.Linear(hidden, out_feats))
if self.n_layers > 1:
self.prelu = nn.PReLU()
self.dropout = nn.Dropout(dropout)
self.norm=bns
self.reset_parameters()
def reset_parameters(self):
gain = nn.init.calculate_gain("relu")
for layer in self.layers:
nn.init.xavier_uniform_(layer.weight, gain=gain)
nn.init.zeros_(layer.bias)
def forward(self, x):
for layer_id, layer in enumerate(self.layers):
x = layer(x)
if layer_id < self.n_layers -1:
if self.norm:
x = self.dropout(self.prelu(self.bns[layer_id](x)))
else:
x = self.dropout(self.prelu(x))
return x
class FeedForwardNetII(nn.Module):
def __init__(self, in_feats, hidden, out_feats, n_layers, dropout,alpha,bns=False):
super(FeedForwardNetII, self).__init__()
self.layers = nn.ModuleList()
self.n_layers = n_layers
self.in_feats=in_feats
self.hidden=hidden
self.out_feats=out_feats
if n_layers == 1:
self.layers.append(Dense(in_feats, out_feats))
else:
self.layers.append(Dense(in_feats, hidden))
for i in range(n_layers - 2):
self.layers.append(GraphConvolution(hidden, hidden,alpha,bns))
self.layers.append(Dense(hidden, out_feats))
self.prelu = nn.PReLU()
self.dropout = nn.Dropout(dropout)
self.reset_parameters()
def reset_parameters(self):
for layer in self.layers:
layer.reset_parameters()
def forward(self, x):
x=self.layers[0](x)
h0=x
for layer_id, layer in enumerate(self.layers):
if layer_id==0:
continue
elif layer_id== self.n_layers - 1:
x = self.dropout(self.prelu(x))
x = layer(x)
else:
x = self.dropout(self.prelu(x))
x = layer(x,h0)
#x = self.dropout(self.prelu(x))
return x