-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
184 lines (140 loc) · 7.53 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
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
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.nn.parameter import Parameter
from torch.nn.modules.module import Module
import numpy as np
#from torch_geometric.nn import GCNConv, GATConv
class Encoder_omics(Module):
"""
Simple GCN layer, similar to https://arxiv.org/abs/1609.02907
"""
def __init__(self, dim_in_feat_omics1, dim_out_feat_omics1, dim_in_feat_omics2, dim_out_feat_omics2, dropout=0.0, act=F.relu):
super(Encoder_omics, self).__init__()
self.dim_in_feat_omics1 = dim_in_feat_omics1
self.dim_in_feat_omics2 = dim_in_feat_omics2
self.dim_out_feat_omics1 = dim_out_feat_omics1
self.dim_out_feat_omics2 = dim_out_feat_omics2
self.dropout = dropout
self.act = act
self.encoder_omics1 = Encoder(self.dim_in_feat_omics1, self.dim_out_feat_omics1)
self.decoder_omics1 = Decoder(self.dim_out_feat_omics1, self.dim_in_feat_omics1)
self.encoder_omics2 = Encoder(self.dim_in_feat_omics2, self.dim_out_feat_omics2)
self.decoder_omics2 = Decoder(self.dim_out_feat_omics2, self.dim_in_feat_omics2)
self.atten_omics1 = AttentionLayer(self.dim_out_feat_omics1, self.dim_out_feat_omics1)
self.atten_omics2 = AttentionLayer(self.dim_out_feat_omics2, self.dim_out_feat_omics2)
self.atten_cross = AttentionLayer(self.dim_out_feat_omics1, self.dim_out_feat_omics2)
self.discriminator = Discriminator(self.dim_out_feat_omics1)
#def forward(self, feat1, feat2, adj1_1, adj1_2, adj2_1, adj2_2):
def forward(self, features_omics1, features_omics2, adj_spatial_omics1, adj_feature_omics1, adj_spatial_omics2, adj_feature_omics2):
# graph1
emb_latent_spatial_omics1 = self.encoder_omics1(features_omics1, adj_spatial_omics1)
emb_latent_spatial_omics2 = self.encoder_omics2(features_omics2, adj_spatial_omics2)
# graph2
emb_latent_feature_omics1 = self.encoder_omics1(features_omics1, adj_feature_omics1)
emb_latent_feature_omics2 = self.encoder_omics2(features_omics2, adj_feature_omics2)
# within-modality attention aggregation
emb_latent_omics1, alpha_omics1 = self.atten_omics1(emb_latent_spatial_omics1, emb_latent_feature_omics1)
emb_latent_omics2, alpha_omics2 = self.atten_omics2(emb_latent_spatial_omics2, emb_latent_feature_omics2)
# between-modality attention aggregation
emb_latent_combined, alpha_omics_1_2 = self.atten_cross(emb_latent_omics1, emb_latent_omics2)
# reconstruct expression matrix using two modality-specific decoders, respectively
emb_recon_omics1 = self.decoder_omics1(emb_latent_combined, adj_spatial_omics1)
emb_recon_omics2 = self.decoder_omics2(emb_latent_combined, adj_spatial_omics2)
emb_latent_omics1_across_recon = self.encoder_omics2(self.decoder_omics2(emb_latent_omics1, adj_spatial_omics2), adj_spatial_omics2) # consistent encoding # dim=64
emb_latent_omics2_across_recon = self.encoder_omics1(self.decoder_omics1(emb_latent_omics2, adj_spatial_omics1), adj_spatial_omics1)
score_omics1 = self.discriminator(emb_latent_omics1)
score_omics2 = self.discriminator(emb_latent_omics2)
score_omics1=torch.squeeze(score_omics1, dim=1)
score_omics2=torch.squeeze(score_omics2, dim=1)
results = {'emb_latent_omics1':emb_latent_omics1,
'emb_latent_omics2':emb_latent_omics2,
'emb_latent_combined':emb_latent_combined,
'emb_recon_omics1':emb_recon_omics1,
'emb_recon_omics2':emb_recon_omics2,
'emb_latent_omics1_across_recon':emb_latent_omics1_across_recon,
'emb_latent_omics2_across_recon':emb_latent_omics2_across_recon,
'alpha':alpha_omics_1_2,
'score_omics1':score_omics1,
'score_omics2':score_omics2}
return results
class Encoder(Module):
"""
Simple GCN layer, similar to https://arxiv.org/abs/1609.02907
"""
def __init__(self, in_feat, out_feat, dropout=0.0, act=F.relu):
super(Encoder, self).__init__()
self.in_feat = in_feat
self.out_feat = out_feat
self.dropout = dropout
self.act = act
self.weight = Parameter(torch.FloatTensor(self.in_feat, self.out_feat))
self.reset_parameters()
def reset_parameters(self):
torch.nn.init.xavier_uniform_(self.weight)
def forward(self, feat, adj):
x = torch.mm(feat, self.weight)
x = torch.spmm(adj, x)
return x
class Decoder(Module):
"""
Simple GCN layer, similar to https://arxiv.org/abs/1609.02907
"""
def __init__(self, in_feat, out_feat, dropout=0.0, act=F.relu):
super(Decoder, self).__init__()
self.in_feat = in_feat
self.out_feat = out_feat
self.dropout = dropout
self.act = act
self.weight = Parameter(torch.FloatTensor(self.in_feat, self.out_feat))
self.reset_parameters()
def reset_parameters(self):
torch.nn.init.xavier_uniform_(self.weight)
def forward(self, feat, adj):
x = torch.mm(feat, self.weight)
x = torch.spmm(adj, x)
return x
class Discriminator(nn.Module):
"""Latent space discriminator"""
def __init__(self, dim_input, n_hidden=50, n_out=1):
super(Discriminator, self).__init__()
self.dim_input = dim_input
self.n_hidden = n_hidden
self.n_out = n_out
self.net = nn.Sequential(
nn.Linear(dim_input, n_hidden),
nn.LeakyReLU(inplace=True),
nn.Linear(n_hidden, 2*n_hidden),
nn.LeakyReLU(inplace=True),
#nn.Linear(n_hidden, n_hidden),
#nn.ReLU(inplace=True),
#nn.Linear(n_hidden, n_hidden),
#nn.ReLU(inplace=True),
#nn.Linear(n_hidden, n_hidden),
#nn.ReLU(inplace=True),
nn.Linear(2*n_hidden,n_out),
nn.Sigmoid(),
)
def forward(self, x):
return self.net(x)
class AttentionLayer(Module):
def __init__(self, in_feat, out_feat, dropout=0.0, act=F.relu):
super(AttentionLayer, self).__init__()
self.in_feat = in_feat
self.out_feat = out_feat
self.w_omega = Parameter(torch.FloatTensor(in_feat, out_feat))
self.u_omega = Parameter(torch.FloatTensor(out_feat, 1))
self.reset_parameters()
def reset_parameters(self):
torch.nn.init.xavier_uniform_(self.w_omega)
torch.nn.init.xavier_uniform_(self.u_omega)
def forward(self, emb1, emb2):
emb = []
emb.append(torch.unsqueeze(torch.squeeze(emb1), dim=1))
emb.append(torch.unsqueeze(torch.squeeze(emb2), dim=1))
self.emb = torch.cat(emb, dim=1)
self.v = F.tanh(torch.matmul(self.emb, self.w_omega))
self.vu= torch.matmul(self.v, self.u_omega)
self.alpha = F.softmax(torch.squeeze(self.vu) + 1e-6)
emb_combined = torch.matmul(torch.transpose(self.emb,1,2), torch.unsqueeze(self.alpha, -1))
return torch.squeeze(emb_combined), self.alpha