-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmodules.py
144 lines (101 loc) · 4.66 KB
/
modules.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
import torch
import torch.nn as nn
import torch.nn.functional as F
import math
from torch.autograd import Variable
from utils import my_softmax, get_offdiag_indices, gumbel_softmax, preprocess_adj, preprocess_adj_new, preprocess_adj_new1, gauss_sample_z, my_normalize
_EPS = 1e-10
class MLPEncoder(nn.Module):
"""MLP encoder module."""
def __init__(self, n_in, n_xdims, n_hid, n_out, adj_A, batch_size, do_prob=0., factor=True, tol = 0.1):
super(MLPEncoder, self).__init__()
self.adj_A = nn.Parameter(Variable(torch.from_numpy(adj_A).double(), requires_grad=True))
self.factor = factor
self.Wa = nn.Parameter(torch.zeros(n_out), requires_grad=True)
self.fc1 = nn.Linear(n_xdims, n_hid, bias = True)
self.fc2 = nn.Linear(n_hid, n_out, bias = True)
self.dropout_prob = do_prob
self.batch_size = batch_size
self.z = nn.Parameter(torch.tensor(tol))
self.z_positive = nn.Parameter(torch.ones_like(torch.from_numpy(adj_A)).double())
self.init_weights()
def init_weights(self):
for m in self.modules():
if isinstance(m, nn.Linear):
nn.init.xavier_normal_(m.weight.data)
elif isinstance(m, nn.BatchNorm1d):
m.weight.data.fill_(1)
m.bias.data.zero_()
def forward(self, inputs):
if torch.sum(self.adj_A != self.adj_A):
print('nan error \n')
# to amplify the value of A and accelerate convergence.
adj_A1 = torch.sinh(3.*self.adj_A)
# adj_Aforz = I-A^T
adj_Aforz = preprocess_adj_new(adj_A1)
adj_A = torch.eye(adj_A1.size()[0]).double()
H1 = F.relu((self.fc1(inputs)))
x = (self.fc2(H1))
logits = torch.matmul(adj_Aforz, x+self.Wa) -self.Wa
return x, logits, adj_A1, adj_A, self.z, self.z_positive, self.adj_A, self.Wa
class SEMEncoder(nn.Module):
"""SEM encoder module."""
def __init__(self, n_in, n_hid, n_out, adj_A, batch_size, do_prob=0., factor=True, tol = 0.1):
super(SEMEncoder, self).__init__()
self.factor = factor
self.adj_A = nn.Parameter(Variable(torch.from_numpy(adj_A).double(), requires_grad = True))
self.dropout_prob = do_prob
self.batch_size = batch_size
def init_weights(self):
nn.init.xavier_normal(self.adj_A.data)
def forward(self, inputs):
if torch.sum(self.adj_A != self.adj_A):
print('nan error \n')
adj_A1 = torch.sinh(3.*self.adj_A)
# adj_A = I-A^T, adj_A_inv = (I-A^T)^(-1)
adj_A = preprocess_adj_new((adj_A1))
adj_A_inv = preprocess_adj_new1((adj_A1))
meanF = torch.matmul(adj_A_inv, torch.mean(torch.matmul(adj_A, inputs), 0))
logits = torch.matmul(adj_A, inputs-meanF)
return inputs-meanF, logits, adj_A1, adj_A, self.z, self.z_positive, self.adj_A
class MLPDecoder(nn.Module):
"""MLP decoder module."""
def __init__(self, n_in_node, n_in_z, n_out, encoder, data_variable_size, batch_size, n_hid,
do_prob=0.):
super(MLPDecoder, self).__init__()
self.out_fc1 = nn.Linear(n_in_z, n_hid, bias = True)
self.out_fc2 = nn.Linear(n_hid, n_out, bias = True)
self.batch_size = batch_size
self.data_variable_size = data_variable_size
self.dropout_prob = do_prob
self.init_weights()
def init_weights(self):
for m in self.modules():
if isinstance(m, nn.Linear):
nn.init.xavier_normal_(m.weight.data)
m.bias.data.fill_(0.0)
elif isinstance(m, nn.BatchNorm1d):
m.weight.data.fill_(1)
m.bias.data.zero_()
def forward(self, inputs, input_z, n_in_node, origin_A, adj_A_tilt, Wa):
#adj_A_new1 = (I-A^T)^(-1)
adj_A_new1 = preprocess_adj_new1(origin_A)
mat_z = torch.matmul(adj_A_new1, input_z+Wa)-Wa
H3 = F.relu(self.out_fc1((mat_z)))
out = self.out_fc2(H3)
return mat_z, out, adj_A_tilt
class SEMDecoder(nn.Module):
"""SEM decoder module."""
def __init__(self, n_in_node, n_in_z, n_out, encoder, data_variable_size, batch_size, n_hid,
do_prob=0.):
super(SEMDecoder, self).__init__()
self.batch_size = batch_size
self.data_variable_size = data_variable_size
print('Using learned interaction net decoder.')
self.dropout_prob = do_prob
def forward(self, inputs, input_z, n_in_node, origin_A, adj_A_tilt, Wa):
# adj_A_new1 = (I-A^T)^(-1)
adj_A_new1 = preprocess_adj_new1(origin_A)
mat_z = torch.matmul(adj_A_new1, input_z + Wa)
out = mat_z
return mat_z, out-Wa, adj_A_tilt