-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.py
More file actions
56 lines (48 loc) · 1.73 KB
/
Copy pathmodels.py
File metadata and controls
56 lines (48 loc) · 1.73 KB
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
import torch
import torch.nn.functional as F
from fusion import AdaptiveFusion
class GHCN(torch.nn.Module):
def __init__(self, n_feature, n_hidden, n_class, k, dropout, fusion):
"""
Args:
n_feature (int): the dimension of feature
n_hidden (int): the dimension of hidden layer
n_class (int): the number of classification label
k (int): k-hop aggregation
dropout (float): dropout rate
fusion (str) type of fusion
"""
super(GHCN, self).__init__()
self.k = k
self.dropout = dropout
self.fusion = fusion
self.Ws = torch.nn.ModuleList()
for _ in range(self.k):
self.Ws.append(torch.nn.Linear(n_feature, n_hidden))
if self.fusion == 'adaptive':
self.attention = AdaptiveFusion(n_hidden, dropout)
if self.fusion == 'concat':
self.fc = torch.nn.Linear(k * n_hidden, n_class)
else:
self.fc = torch.nn.Linear(n_hidden, n_class)
def forward(self, feature):
"""
Args:
feature (torch Tensor): feature input
Returns:
(torch Tensor): log probability for each class in label
"""
xs = []
for i in range(self.k):
x = self.Ws[i](feature[i])
x = F.relu(x)
x = F.dropout(x, self.dropout, training=self.training)
xs.append(x)
if self.fusion == 'noderank':
out = torch.sum(torch.stack(xs), dim=0)
elif self.fusion == 'adaptive':
out = self.attention(xs)
elif self.fusion == 'concat':
out = torch.cat(xs, dim=1)
out = self.fc(out)
return F.log_softmax(out, dim=1)