-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmodel.py
100 lines (81 loc) · 2.89 KB
/
model.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
import torch.nn as nn
import torch.nn.functional as F
from utils import *
from torch import Tensor
from torch.nn.modules.module import Module
from torch_geometric.nn.inits import glorot
class GCN(nn.Module):
def __init__(self, in_ft, out_ft, act, bias=True):
super(GCN, self).__init__()
self.fc = nn.Linear(in_ft, out_ft, bias=False)
self.bn = nn.BatchNorm1d(out_ft)
self.act = nn.PReLU() if act == 'prelu' else act
if bias:
self.bias = nn.Parameter(torch.FloatTensor(out_ft))
self.bias.data.fill_(0.0)
else:
self.register_parameter('bias', None)
for m in self.modules():
self.weights_init(m)
def weights_init(self, m):
if isinstance(m, nn.Linear):
torch.nn.init.xavier_uniform_(m.weight.data)
if m.bias is not None:
m.bias.data.fill_(0.0)
def forward(self, seq, adj=None, sparse=False):
out = self.fc(seq)
if adj is not None:
if sparse:
out = torch.spmm(adj, out)
else:
out = torch.mm(adj, out)
if self.bias is not None:
out += self.bias
out = self.bn(out)
out = self.act(out)
return out
class Model(nn.Module):
def __init__(self, n_in, n_h, activation):
super(Model, self).__init__()
self.gcn1 = GCN(n_in, n_h, activation)
def forward(self, seq, adj):
feat = self.gcn1(seq, adj)
return feat
def reset_parameters(self,):
for m in self.modules():
self.weights_init(m)
def weights_init(self, m):
if isinstance(m, nn.Linear):
torch.nn.init.xavier_uniform_(m.weight.data)
if m.bias is not None:
m.bias.data.fill_(0.0)
class SimplePrompt(nn.Module):
def __init__(self, in_channels: int):
super(SimplePrompt, self).__init__()
self.global_emb = nn.Parameter(torch.Tensor(1, in_channels))
self.reset_parameters()
def reset_parameters(self):
glorot(self.global_emb)
def add(self, x: Tensor):
return x + self.global_emb
class GPFplusAtt(nn.Module):
def __init__(self, in_channels: int, p_num: int):
super(GPFplusAtt, self).__init__()
self.p_list = nn.Parameter(torch.Tensor(p_num, in_channels))
self.a = nn.Linear(in_channels, p_num)
self.reset_parameters()
def reset_parameters(self):
glorot(self.p_list)
self.a.reset_parameters()
def add(self, x: Tensor):
score = self.a(x)
weight = F.softmax(score, dim=1)
p = torch.mm(weight, self.p_list)
return x + p
class Projection(nn.Module):
def __init__(self, hidden_dim):
super(Projection, self).__init__()
self.fc1 = torch.nn.Linear(hidden_dim, hidden_dim)
def forward(self, feat):
feat = self.fc1(feat)
return feat