forked from yongqyu/MolGAN-pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
layers.py
57 lines (43 loc) · 2.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
import math
import torch
import torch.nn as nn
from torch.nn.parameter import Parameter
from torch.nn.modules.module import Module
class GraphConvolution(Module):
def __init__(self, in_features, out_feature_list, b_dim, dropout):
super(GraphConvolution, self).__init__()
self.in_features = in_features
self.out_feature_list = out_feature_list
self.linear1 = nn.Linear(in_features, out_feature_list[0])
self.linear2 = nn.Linear(out_feature_list[0], out_feature_list[1])
self.dropout = nn.Dropout(dropout)
def forward(self, input, adj, activation=None):
# input : 16x9x9
# adj : 16x4x9x9
hidden = torch.stack([self.linear1(input) for _ in range(adj.size(1))], 1)
hidden = torch.einsum('bijk,bikl->bijl', (adj, hidden))
hidden = torch.sum(hidden, 1) + self.linear1(input)
hidden = activation(hidden) if activation is not None else hidden
hidden = self.dropout(hidden)
output = torch.stack([self.linear2(hidden) for _ in range(adj.size(1))], 1)
output = torch.einsum('bijk,bikl->bijl', (adj, output))
output = torch.sum(output, 1) + self.linear2(hidden)
output = activation(output) if activation is not None else output
output = self.dropout(output)
return output
class GraphAggregation(Module):
def __init__(self, in_features, out_features, b_dim, dropout):
super(GraphAggregation, self).__init__()
self.sigmoid_linear = nn.Sequential(nn.Linear(in_features+b_dim, out_features),
nn.Sigmoid())
self.tanh_linear = nn.Sequential(nn.Linear(in_features+b_dim, out_features),
nn.Tanh())
self.dropout = nn.Dropout(dropout)
def forward(self, input, activation):
i = self.sigmoid_linear(input)
j = self.tanh_linear(input)
output = torch.sum(torch.mul(i,j), 1)
output = activation(output) if activation is not None\
else output
output = self.dropout(output)
return output