-
Notifications
You must be signed in to change notification settings - Fork 16
/
model.py
66 lines (51 loc) · 2.07 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
import torch.nn as nn
import torch.nn.functional as F
import torch
from layers import GraphConvolution
class Encoder(nn.Module):
def __init__(self, nfeat, nhid, dropout):
super(Encoder, self).__init__()
self.gc1 = GraphConvolution(nfeat, nhid)
self.gc2 = GraphConvolution(nhid, nhid)
self.dropout = dropout
def forward(self, x, adj):
x = F.relu(self.gc1(x, adj))
x = F.dropout(x, self.dropout, training=self.training)
x = F.relu(self.gc2(x, adj))
return x
class Attribute_Decoder(nn.Module):
def __init__(self, nfeat, nhid, dropout):
super(Attribute_Decoder, self).__init__()
self.gc1 = GraphConvolution(nhid, nhid)
self.gc2 = GraphConvolution(nhid, nfeat)
self.dropout = dropout
def forward(self, x, adj):
x = F.relu(self.gc1(x, adj))
x = F.dropout(x, self.dropout, training=self.training)
x = F.relu(self.gc2(x, adj))
return x
class Structure_Decoder(nn.Module):
def __init__(self, nhid, dropout):
super(Structure_Decoder, self).__init__()
self.gc1 = GraphConvolution(nhid, nhid)
self.dropout = dropout
def forward(self, x, adj):
x = F.relu(self.gc1(x, adj))
x = F.dropout(x, self.dropout, training=self.training)
x = x @ x.T
return x
class Dominant(nn.Module):
def __init__(self, feat_size, hidden_size, dropout):
super(Dominant, self).__init__()
self.shared_encoder = Encoder(feat_size, hidden_size, dropout)
self.attr_decoder = Attribute_Decoder(feat_size, hidden_size, dropout)
self.struct_decoder = Structure_Decoder(hidden_size, dropout)
def forward(self, x, adj):
# encode
x = self.shared_encoder(x, adj)
# decode feature matrix
x_hat = self.attr_decoder(x, adj)
# decode adjacency matrix
struct_reconstructed = self.struct_decoder(x, adj)
# return reconstructed matrices
return struct_reconstructed, x_hat