/
gat_layers.py
98 lines (82 loc) · 3.63 KB
/
gat_layers.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
#!/usr/bin/env python
# encoding: utf-8
# File Name: gat_layers.py
# Author: Jiezhong Qiu
# Create Time: 2017/12/18 15:11
# TODO:
from __future__ import absolute_import
from __future__ import unicode_literals
from __future__ import division
from __future__ import print_function
import torch
import torch.nn as nn
import torch.nn.init as init
import torch.nn.functional as F
from torch.nn.parameter import Parameter
class MultiHeadGraphAttention(nn.Module):
def __init__(self, n_head, f_in, f_out, attn_dropout, bias=True):
super(MultiHeadGraphAttention, self).__init__()
self.n_head = n_head
self.w = Parameter(torch.Tensor(n_head, f_in, f_out))
self.a_src = Parameter(torch.Tensor(n_head, f_out, 1))
self.a_dst = Parameter(torch.Tensor(n_head, f_out, 1))
self.leaky_relu = nn.LeakyReLU(negative_slope=0.2)
self.softmax = nn.Softmax(dim=-1)
self.dropout = nn.Dropout(attn_dropout)
if bias:
self.bias = Parameter(torch.Tensor(f_out))
init.constant_(self.bias, 0)
else:
self.register_parameter('bias', None)
init.xavier_uniform_(self.w)
init.xavier_uniform_(self.a_src)
init.xavier_uniform_(self.a_dst)
def forward(self, h, adj):
n = h.size(0) # h is of size n x f_in
h_prime = torch.matmul(h.unsqueeze(0), self.w) # n_head x n x f_out
attn_src = torch.bmm(h_prime, self.a_src) # n_head x n x 1
attn_dst = torch.bmm(h_prime, self.a_dst) # n_head x n x 1
attn = attn_src.expand(-1, -1, n) + attn_dst.expand(-1, -1, n).permute(0, 2, 1) # n_head x n x n
attn = self.leaky_relu(attn)
attn.data.masked_fill_(1 - adj, float("-inf"))
attn = self.softmax(attn) # n_head x n x n
attn = self.dropout(attn)
output = torch.bmm(attn, h_prime) # n_head x n x f_out
if self.bias is not None:
return output + self.bias
else:
return output
class BatchMultiHeadGraphAttention(nn.Module):
def __init__(self, n_head, f_in, f_out, attn_dropout, bias=True):
super(BatchMultiHeadGraphAttention, self).__init__()
self.n_head = n_head
self.w = Parameter(torch.Tensor(n_head, f_in, f_out))
self.a_src = Parameter(torch.Tensor(n_head, f_out, 1))
self.a_dst = Parameter(torch.Tensor(n_head, f_out, 1))
self.leaky_relu = nn.LeakyReLU(negative_slope=0.2)
self.softmax = nn.Softmax(dim=-1)
self.dropout = nn.Dropout(attn_dropout)
if bias:
self.bias = Parameter(torch.Tensor(f_out))
init.constant_(self.bias, 0)
else:
self.register_parameter('bias', None)
init.xavier_uniform_(self.w)
init.xavier_uniform_(self.a_src)
init.xavier_uniform_(self.a_dst)
def forward(self, h, adj):
bs, n = h.size()[:2] # h is of size bs x n x f_in
h_prime = torch.matmul(h.unsqueeze(1), self.w) # bs x n_head x n x f_out
attn_src = torch.matmul(F.tanh(h_prime), self.a_src) # bs x n_head x n x 1
attn_dst = torch.matmul(F.tanh(h_prime), self.a_dst) # bs x n_head x n x 1
attn = attn_src.expand(-1, -1, -1, n) + attn_dst.expand(-1, -1, -1, n).permute(0, 1, 3, 2) # bs x n_head x n x n
attn = self.leaky_relu(attn)
mask = 1 - adj.unsqueeze(1) # bs x 1 x n x n
attn.data.masked_fill_(mask, float("-inf"))
attn = self.softmax(attn) # bs x n_head x n x n
attn = self.dropout(attn)
output = torch.matmul(attn, h_prime) # bs x n_head x n x f_out
if self.bias is not None:
return output + self.bias
else:
return output