forked from Tobi2K/Graph-MLP-Sampling
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
75 lines (60 loc) · 1.99 KB
/
models.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
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.nn import Dropout, Linear, LayerNorm
class Mlp(nn.Module):
def __init__(self, input_dim, hid_dim, dropout):
super(Mlp, self).__init__()
self.fc1 = Linear(input_dim, hid_dim)
self.fc2 = Linear(hid_dim, hid_dim)
self.act_fn = torch.nn.functional.gelu
self._init_weights()
self.dropout = Dropout(dropout)
self.layernorm = LayerNorm(hid_dim, eps=1e-6)
def _init_weights(self):
nn.init.xavier_uniform_(self.fc1.weight)
nn.init.xavier_uniform_(self.fc2.weight)
nn.init.normal_(self.fc1.bias, std=1e-6)
nn.init.normal_(self.fc2.bias, std=1e-6)
def forward(self, x):
x = self.fc1(x)
x = self.act_fn(x)
x = self.layernorm(x)
x = self.dropout(x)
x = self.fc2(x)
return x
def get_feature_dis(x, cuda=True):
"""
x : batch_size x nhid
x_dis(i,j): item means the similarity between x(i) and x(j).
"""
x_dis = [email protected]
mask = torch.eye(x_dis.shape[0])
if cuda:
mask = mask.cuda()
x_sum = torch.sum(x**2, 1).reshape(-1, 1)
x_sum = torch.sqrt(x_sum).reshape(-1, 1)
x_sum = x_sum @ x_sum.T
x_dis = x_dis*(x_sum**(-1))
x_dis = (1-mask) * x_dis
return x_dis
class GMLP(nn.Module):
def __init__(self, nfeat, nhid, nclass, dropout, cuda):
super(GMLP, self).__init__()
self.nhid = nhid
self.mlp = Mlp(nfeat, self.nhid, dropout)
self.classifier = Linear(self.nhid, nclass)
self.use_cuda = cuda
def forward(self, x):
x = self.mlp(x)
feature_cls = x
Z = x
if self.training:
x_dis = get_feature_dis(Z, self.use_cuda)
class_feature = self.classifier(feature_cls)
class_logits = F.log_softmax(class_feature, dim=1)
if self.training:
return class_logits, x_dis
else:
return class_logits