-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsigmalayer.py
211 lines (167 loc) · 6.24 KB
/
sigmalayer.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import torch
import torch.nn as nn
import numpy as np
import torch.nn.functional as F
from torch.autograd import Variable
# import dgl
from torch_geometric.nn import TAGConv
sp=nn.Softplus()
MeanAct = lambda x : torch.clamp(torch.exp(x), 1e-5, 1e6)
DispAct = lambda x : torch.clamp(sp(x), 1e-4, 1e4)
class layer_zinb(nn.Module):
def __init__(self, in_feature, out_feature) :
super(layer_zinb, self).__init__()
self.in_feature = in_feature
self.out_feature = out_feature
self.w=nn.Parameter(torch.zeros(size=(in_feature, out_feature)))
nn.init.xavier_uniform_(self.w.data, gain=1)
def forward(self, input, type):
h=torch.mm(input, self.w)
# activation
if type=='sigmoid':
layer=nn.Sigmoid()
h=layer(h)
#h=nn.Sigmoid(h)
elif type=='MeanAct':
#h=torch.exp(h)
h=MeanAct(h)
elif type=='DispAct':
#layer=nn.Softplus()
h=DispAct(h)
return h
class Encoder(nn.Module):
def __init__(self, n1, n2, n3, n4, dropout, activation=nn.ReLU(),g=None, n_clusters=None):
super(Encoder, self).__init__()
self.g=g
self.dropout = nn.Dropout(p=dropout)
if self.g!=None:
self.enc1 = TAGConv(in_channels=n4, out_channels=n3)
self.enc2 = TAGConv(in_channels=n3, out_channels=n2)
self.enc3 = TAGConv(in_channels=n2, out_channels=n1)
#self.enc4 = TAGConv(in_channels=n1, out_channels=n_clusters)
else:
self.enc1 = nn.Linear(n4, n3)
self.BN1 = nn.BatchNorm1d(n3, momentum=0.01, eps=0.001)
self.enc2 = nn.Linear(n3, n2)
self.BN2 = nn.BatchNorm1d(n2, momentum=0.01, eps=0.001)
self.enc3 = nn.Linear(n2, n1)
self.BN3 = nn.BatchNorm1d(n1, momentum=0.01, eps=0.001)
#self.enc4 = nn.Linear(n1, n_clusters)
def forward_g1(self, x):
if self.g!=None:
enc_h1 = self.dropout(self.enc1(x, self.g))
return enc_h1
def forward_g2(self, x):
if self.g!=None:
enc_h2 = self.dropout(self.enc2(x, self.g))
return enc_h2
def forward_g3(self, x):
if self.g!=None:
enc_h3 = self.dropout(self.enc3(x, self.g))
return enc_h3
def forward(self, x): # ?
if self.g!=None:
enc_h1 = self.dropout(self.enc1(x, self.g))
enc_h2 = self.dropout(self.enc2(enc_h1, self.g))
enc_h3 = self.dropout(self.enc3(enc_h2, self.g))
h=F.normalize(enc_h3, p=2, dim=1)
else:
enc_h1 = self.dropout(nn.ReLU(self.BN1(self.enc1(x))))
enc_h2 = self.dropout(nn.ReLU(self.BN2(self.enc2(enc_h1))))
enc_h3 = self.dropout(nn.ReLU(self.BN3(self.enc3(enc_h2))))
h = enc_h3
return h, enc_h1, enc_h2, enc_h3
def dot_product_decoder(self, Z):
A_pred=torch.sigmoid(torch.matmul(Z, Z.t()))
return A_pred
class decoder_ZINB(nn.Module):
def __init__(self, n_layers, hidden, input_size, dropout) :
super(decoder_ZINB, self).__init__()
self.decoder=nn.ModuleList()
#dec_dim?
for i , (n_in, n_out)in enumerate(zip(n_layers[:-1], n_layers[1:])):
self.decoder.append(nn.Linear(n_in, n_out))
self.decoder.append(nn.BatchNorm1d(n_out, momentum=0.01, eps=0.001))
self.decoder.append(nn.ReLU())
self.decoder.append(nn.Dropout(p=dropout))
self.layer_zinb=layer_zinb(n_layers[-1], input_size)# ?
def forward(self, z):
latent=z
for i, layer in enumerate(self.decoder):
latent=layer(latent.float())
#latent=self.decoder(z)
# pi
pi=self.layer_zinb(latent, type='sigmoid')
disp=self.layer_zinb(latent, type='DispAct')
mean=self.layer_zinb(latent, type='MeanAct')
return pi, disp, mean
def _nelem(x):
isnan=~torch.isnan(x)
isnan=isnan.type(torch.FloatTensor)
nelem=torch.sum(isnan)
def _nan2zero(x):
return torch.where(torch.isnan(x), torch.zeros_like(x), x)
def _nan2inf(x):
return torch.where(torch.isnan(x), torch.zeros_like(x)+np.inf, x)
def _reduce_mean(x):
nelem=_nelem(x)
x=_nan2zero(x)
return torch.divide(torch.reduce_sum(x),nelem)
class NB(object):
def __init__(self, theta=None, masking=False, scale_factor=1.0):
self.eps=1e-10
self.scale_factor=scale_factor
self.masking=masking
self.theta=theta
def loss(self, y_true, y_pred, mean=True):
scale_factor=self.scale_factor
eps=self.eps
if self.masking:
nelem=_nelem(y_true)
y_true=_nan2zero(y_true)
a=torch.full(self.theta.size(), 1e6)
a=a.to("cuda:0")
theta=torch.minimum(self.theta, a)
t1=torch.lgamma(theta+eps)+torch.lgamma(y_true+1.0)-torch.lgamma(y_true+theta+eps)
t2=(theta+y_true)*torch.log(1.0+(y_pred/(theta+eps)))+(y_true*(torch.log(theta+eps)-torch.log(y_pred+eps)))
final=t1+t2
final=_nan2inf(final)
if mean:
if self.masking:
final=torch.divide(torch.reduce_sum(final), nelem)
else:
final=torch.mean(final)
return final
class ZINB(NB):
def __init__(self, pi, ridge_lambda=0.0, **kwargs):
super().__init__(**kwargs)
self.pi=pi
self.ridge_lambda=ridge_lambda
def loss(self, y_true, y_pred, mean=True):
scale_factor=self.scale_factor
eps=self.eps
nb_case=super().loss(y_true, y_pred, mean=False)-torch.log(1.0-self.pi)
y_true=y_true.type(torch.FloatTensor)
y_pred=y_pred.type(torch.FloatTensor)
y_pred=y_pred*scale_factor
a=torch.full(self.theta.size(), 1e6)
a=a.to("cuda:0")
theta=torch.minimum(self.theta, a)
theta=theta.to("cuda:0")
#eps=eps.to("cuda:0")
eps=torch.full(y_pred.size(), self.eps)
eps=eps.to("cuda:0")
y_pred=y_pred.to("cuda:0")
y_true=y_true.to("cuda:0")
zero_nb=torch.pow(theta/(theta+y_pred+eps), theta)
zero_case=-torch.log(self.pi+((1.0-self.pi)*zero_nb)+eps)
result=torch.where(torch.lt(y_true, 1e-8), zero_case, nb_case)
ridge=self.ridge_lambda*torch.square(self.pi)
result+=ridge
if mean:
if self.masking:
result=_reduce_mean(result)
else:
result=torch.mean(result)
result = _nan2inf(result)
return result