-
Notifications
You must be signed in to change notification settings - Fork 5
/
local_module.py
180 lines (136 loc) · 5.68 KB
/
local_module.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
import torch
import math
import torch.nn as nn
import numpy as np
import torch.nn.functional as F
def init_params(module, n_layers):
if isinstance(module, nn.Linear):
module.weight.data.normal_(mean=0.0, std=0.02 / math.sqrt(n_layers))
if module.bias is not None:
module.bias.data.zero_()
if isinstance(module, nn.Embedding):
module.weight.data.normal_(mean=0.0, std=0.02)
class LocalModule(nn.Module):
def __init__(
self,
seq_len,
input_dim,
node_only_readout=False,
n_layers=1,
num_heads=8,
hidden_dim=64,
dropout_rate=0.3,
attention_dropout_rate=0,
):
super().__init__()
self.seq_len = seq_len
self.node_only_readout = node_only_readout
self.input_dim = input_dim
self.hidden_dim = hidden_dim
self.ffn_dim = 2 * hidden_dim
self.num_heads = num_heads
self.n_layers = n_layers
self.dropout_rate = dropout_rate
self.attention_dropout_rate = attention_dropout_rate
self.att_embeddings_nope = nn.Linear(self.input_dim, self.hidden_dim)
encoders = [
EncoderLayer(
self.hidden_dim,
self.ffn_dim,
self.dropout_rate,
self.attention_dropout_rate,
self.num_heads,
)
for _ in range(self.n_layers)
]
self.layers = nn.ModuleList(encoders)
self.final_ln = nn.LayerNorm(hidden_dim * num_heads)
self.out_proj = nn.Linear(self.ffn_dim, int(self.ffn_dim / 2))
self.attn_layer = nn.Linear(2 * self.hidden_dim * num_heads, 1)
self.apply(lambda module: init_params(module, n_layers=n_layers))
def forward(self, batched_data):
tensor = self.att_embeddings_nope(batched_data)
# transformer encoder
for enc_layer in self.layers:
tensor = enc_layer(tensor)
output = self.final_ln(tensor)
_target = output[:, 0, :].unsqueeze(1).repeat(1, self.seq_len - 1, 1)
split_tensor = torch.split(output, [1, self.seq_len - 1], dim=1)
node_tensor = split_tensor[0]
_neighbor_tensor = split_tensor[1]
if self.node_only_readout:
# only slicing the indices that belong to nodes and not the 1-hop and 2-hop feats
indices = torch.arange(3, self.seq_len, 3)
neighbor_tensor = _neighbor_tensor[:, indices]
target = _target[:, indices]
else:
target = _target
neighbor_tensor = _neighbor_tensor
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)
output = (node_tensor + neighbor_tensor).squeeze()
return output
class FeedForwardNetwork(nn.Module):
def __init__(self, hidden_size, ffn_size, dropout_rate):
super(FeedForwardNetwork, self).__init__()
self.layer1 = nn.Linear(hidden_size, ffn_size)
self.gelu = nn.GELU()
self.layer2 = nn.Linear(ffn_size, hidden_size)
def forward(self, x):
x = self.layer1(x)
x = self.gelu(x)
x = self.layer2(x)
return x
class MultiHeadAttention(nn.Module):
def __init__(self, hidden_size, attention_dropout_rate, num_heads):
super(MultiHeadAttention, self).__init__()
self.num_heads = num_heads
self.att_size = att_size = hidden_size # // num_heads
self.scale = att_size**-0.5
self.linear_q = nn.Linear(hidden_size, num_heads * att_size)
self.linear_k = nn.Linear(hidden_size, num_heads * att_size)
self.linear_v = nn.Linear(hidden_size, num_heads * att_size)
self.att_dropout = nn.Dropout(attention_dropout_rate)
self.output_layer = nn.Linear(num_heads * att_size, num_heads * att_size)
def forward(self, q, k, v, attn_bias=None):
d_k = self.att_size
d_v = self.att_size
batch_size = q.size(0)
q = self.linear_q(q).view(batch_size, -1, self.num_heads, d_k)
k = self.linear_k(k).view(batch_size, -1, self.num_heads, d_k)
v = self.linear_v(v).view(batch_size, -1, self.num_heads, d_v)
q = q.transpose(1, 2) # [b, h, q_len, d_k]
v = v.transpose(1, 2) # [b, h, v_len, d_v]
k = k.transpose(1, 2).transpose(2, 3) # [b, h, d_k, k_len]
q = q * self.scale
x = torch.matmul(q, k) # [b, h, q_len, k_len]
if attn_bias is not None:
x = x + attn_bias
x = torch.softmax(x, dim=3)
x = self.att_dropout(x)
x = x.matmul(v) # [b, h, q_len, attn]
x = x.transpose(1, 2).contiguous() # [b, q_len, h, attn]
x = x.view(batch_size, -1, self.num_heads * d_v)
x = self.output_layer(x)
return x
class EncoderLayer(nn.Module):
def __init__(
self, hidden_size, ffn_size, dropout_rate, attention_dropout_rate, num_heads
):
super(EncoderLayer, self).__init__()
self.self_attention_norm = nn.LayerNorm(hidden_size)
self.self_attention = MultiHeadAttention(
hidden_size, attention_dropout_rate, num_heads
)
self.self_attention_dropout = nn.Dropout(dropout_rate)
self.res_proj = nn.Linear(hidden_size, hidden_size * num_heads)
self.ffn_dropout = nn.Dropout(dropout_rate)
def forward(self, x, attn_bias=None):
y = self.self_attention_norm(x)
y = self.self_attention(y, y, y, attn_bias)
y = self.self_attention_dropout(y)
x = self.res_proj(x) + y
x = self.ffn_dropout(x)
return x