-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmodel.py
192 lines (151 loc) · 8.02 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
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import torch
import torch.nn.functional as F
from torch.nn import Linear, BatchNorm1d, LayerNorm, Dropout, Softmax
'''
Slightly modified multihead attention for Gamora
'''
class MultiheadAttentionMix(torch.nn.Module):
def __init__(self, input_dim, num_heads, dropout=0.0):
super(MultiheadAttentionMix, self).__init__()
self.input_dim = input_dim
self.num_heads = num_heads
self.head_dim = input_dim // num_heads
# Linear projections for queries, keys, and values
self.query_projection = Linear(input_dim, input_dim)
self.key_projection = Linear(input_dim, input_dim)
self.value_projection = Linear(input_dim, input_dim)
# Linear projection for the output of the attention heads
self.output_projection = Linear(input_dim, input_dim)
self.dropout = Dropout(dropout)
self.softmax = Softmax(dim=-1)
def forward(self, query, key, value, mask=None):
batch_size = query.size(0)
# Linear projections for queries, keys, and values
query = self.query_projection(query)
key = self.key_projection(key)
value = self.value_projection(value)
# Reshape the projected queries, keys, and values
query = query.view(batch_size * self.num_heads, -1, self.head_dim)
key = key.view(batch_size * self.num_heads, -1, self.head_dim)
value = value.view(batch_size * self.num_heads, -1, self.head_dim)
# Compute the scaled dot-product attention
attention_scores = torch.bmm(query, key.transpose(1, 2))
attention_scores = attention_scores / torch.sqrt(torch.tensor(self.head_dim, dtype=torch.float32))
# Apply the mask (if provided)
if mask is not None:
mask = mask.unsqueeze(1) # Add head dimension
attention_scores = attention_scores.masked_fill(mask == 0, float("-inf"))
attention_probs = self.softmax(attention_scores)
attention_probs = self.dropout(attention_probs)
# Compute the output of the attention heads
attention_output = torch.bmm(attention_probs, value)
# Reshape and project the output of the attention heads
attention_output = attention_output.view(batch_size, -1, self.input_dim)
attention_output = self.output_projection(attention_output)
return attention_output, attention_probs
'''
Vanilla multihead attention (recommended for general use cases)
'''
class MultiheadAttention(torch.nn.Module):
def __init__(self, input_dim, num_heads, dropout=0.0):
super(MultiheadAttention, self).__init__()
self.input_dim = input_dim
self.num_heads = num_heads
self.head_dim = input_dim // num_heads
# Linear projections for queries, keys, and values
self.query_projection = Linear(input_dim, input_dim)
self.key_projection = Linear(input_dim, input_dim)
self.value_projection = Linear(input_dim, input_dim)
# Linear projection for the output of the attention heads
self.output_projection = Linear(input_dim, input_dim)
self.dropout = Dropout(dropout)
self.softmax = Softmax(dim=-1)
def forward(self, query, key, value, mask=None):
batch_size, seq_len, _ = query.size()
# Linear projections for queries, keys, and values
query = self.query_projection(query)
key = self.key_projection(key)
value = self.value_projection(value)
# Reshape the projected queries, keys, and values
query = query.view(batch_size, seq_len, self.head_dim, -1)
key = key.view(batch_size, seq_len, self.head_dim, -1)
value = value.view(batch_size, seq_len, self.head_dim, -1)
# Compute the scaled dot-product attention
attention_scores = torch.einsum('bldh, bndh -> blnh', query, key)
attention_scores = attention_scores / torch.sqrt(torch.tensor(self.head_dim, dtype=torch.float32))
attention_probs = self.softmax(attention_scores)
attention_probs = self.dropout(attention_probs)
# Compute the output of the attention heads
attention_output = torch.einsum('blnh, bndh -> bldh', attention_probs, value)
# Reshape and project the output of the attention heads
attention_output = attention_output.reshape(batch_size, seq_len, self.input_dim)
attention_output = self.output_projection(attention_output)
return attention_output, attention_probs
class HOGA(torch.nn.Module):
def __init__(self, in_channels, hidden_channels, out_channels, num_layers,
dropout, num_hops, heads, attn_dropout=0.0, attn_type="vanilla", use_bias=False):
super(HOGA, self).__init__()
self.num_layers = num_layers
self.num_hops = num_hops
self.lins = torch.nn.ModuleList()
self.gates = torch.nn.ModuleList()
self.trans = torch.nn.ModuleList()
self.lns = torch.nn.ModuleList()
self.lins.append(Linear(in_channels, hidden_channels, bias=use_bias))
self.lins.append(Linear(hidden_channels, hidden_channels, bias=use_bias))
self.lins.append(Linear(hidden_channels, hidden_channels, bias=use_bias))
self.gates.append(Linear(hidden_channels, hidden_channels, bias=use_bias))
if attn_type == "vanilla":
self.trans.append(MultiheadAttention(hidden_channels, heads, dropout=attn_dropout))
else:
self.trans.append(MultiheadAttentionMix(hidden_channels, heads, dropout=attn_dropout))
self.lns.append(LayerNorm(hidden_channels))
for _ in range(num_layers - 1):
self.lins.append(Linear(hidden_channels, hidden_channels, bias=use_bias))
self.gates.append(Linear(hidden_channels, hidden_channels, bias=use_bias))
if attn_type == "vanilla":
self.trans.append(MultiheadAttention(hidden_channels, heads, dropout=attn_dropout))
else:
self.trans.append(MultiheadAttentionMix(hidden_channels, heads, dropout=attn_dropout))
self.lns.append(LayerNorm(hidden_channels))
# Linear layers for predictions
self.linear = torch.nn.ModuleList()
self.linear.append(Linear(hidden_channels, hidden_channels, bias=use_bias))
self.linear.append(Linear(hidden_channels, out_channels, bias=use_bias))
self.linear.append(Linear(hidden_channels, out_channels, bias=use_bias))
self.linear.append(Linear(hidden_channels, out_channels, bias=use_bias))
self.bn = BatchNorm1d(hidden_channels)
self.attn_layer = Linear(2 * hidden_channels, 1)
self.dropout = dropout
def reset_parameters(self):
for lin in self.lins:
lin.reset_parameters()
for gate in self.gates:
gate.reset_parameters()
for li in self.linear:
li.reset_parameters()
self.bn.reset_parameters()
def forward(self, x):
# Current implementation: use a shared linear layer for all hop-wise features
# Note: apply separate layers for different hop-wise features may further improve accuracy
x = self.lins[0](x)
for i, tran in enumerate(self.trans):
x = self.lns[i](self.gates[i](x)*(tran(x, x, x)[0]))
x = F.relu(x)
x = F.dropout(x, p=self.dropout, training=self.training)
target = x[:,0,:].unsqueeze(1).repeat(1,self.num_hops-1,1)
split_tensor = torch.split(x, [1, self.num_hops-1], dim=1)
node_tensor = split_tensor[0]
neighbor_tensor = split_tensor[1]
layer_atten = self.attn_layer(torch.cat((target, neighbor_tensor), dim=2))
layer_atten = F.softmax(layer_atten, dim=1)
neighbor_tensor = neighbor_tensor * layer_atten
neighbor_tensor = torch.sum(neighbor_tensor, dim=1, keepdim=True)
x = (node_tensor + neighbor_tensor).squeeze()
x = self.linear[0](x)
x = self.bn(F.relu(x))
x = F.dropout(x, p=self.dropout, training=self.training)
x1 = self.linear[1](x) # for xor
x2 = self.linear[2](x) # for maj
x3 = self.linear[3](x) # for roots
return x1, x2, x3, layer_atten