-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlayers.py
82 lines (69 loc) · 3.07 KB
/
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
import torch
from torch import nn
from torch.nn import Parameter
import torch.nn.functional as F
import numpy as np
class GraphConvolution(nn.Module):
"""Basic graph convolution layer for undirected graph without edge labels."""
def __init__(self, input_dim, output_dim, dropout, device):
super(GraphConvolution, self).__init__()
self.input_dim = input_dim
self.output_dim = output_dim
self.weights = Parameter(torch.Tensor(input_dim, output_dim))
self.dropout = dropout
self.device = device
self.reset_parameters()
def reset_parameters(self):
init_range = np.sqrt(6.0 / (self.input_dim + self.output_dim))
nn.init.uniform_(self.weights, -init_range, init_range)
def forward(self, inputs, adj, act=nn.ReLU()):
x = inputs
x = F.dropout(x, self.dropout, training=self.training)
x = torch.matmul(x, self.weights)
x = x.to(self.device)
adj = torch.sparse_coo_tensor(indices=torch.from_numpy(adj[0].transpose()), values=torch.from_numpy(adj[1]),
size=adj[2]).to(torch.float32).to_dense()
adj = adj.to(self.device)
x = torch.matmul(adj, x)
outputs = act(x)
return outputs
class GraphConvolutionSparse(nn.Module):
"""Graph convolution layer for sparse inputs."""
def __init__(self, input_dim, output_dim, dropout, device):
super(GraphConvolutionSparse, self).__init__()
self.input_dim = input_dim
self.output_dim = output_dim
self.weights = Parameter(torch.Tensor(input_dim, output_dim))
self.dropout = dropout
# self.issparse = True
self.device = device
self.reset_parameters()
def reset_parameters(self):
init_range = np.sqrt(6.0 / (self.input_dim + self.output_dim))
nn.init.uniform_(self.weights, -init_range, init_range)
def forward(self, inputs, adj, act=nn.ReLU()):
x = inputs
x = torch.sparse_coo_tensor(indices=torch.from_numpy(x[0].transpose()), values=torch.from_numpy(x[1]), size=x[2]).to(torch.float32)
x = x.to(self.device)
x = torch.matmul(x.to_dense(), self.weights)
x = F.dropout(x, self.dropout, training=self.training)
adj = torch.sparse_coo_tensor(indices=torch.from_numpy(adj[0].transpose()), values=torch.from_numpy(adj[1]),
size=adj[2]).to(torch.float32).to_dense()
adj = adj.to(self.device)
x = torch.matmul(adj, x)
outputs = act(x)
return outputs
class InnerProductDecoder(nn.Module):
"""Decoder model layer for link prediction."""
def __init__(self, dropout, device):
super(InnerProductDecoder, self).__init__()
self.dropout = dropout
self.device = device
def forward(self, inputs, act=nn.Sigmoid()):
inputs = F.dropout(inputs, self.dropout, training=self.training)
x = inputs.transpose(1,0)
x = torch.matmul(inputs, x)
x = torch.reshape(x, [-1])
outputs = act(x)
outputs = outputs.to(self.device)
return outputs