-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRBPNet.py
More file actions
205 lines (160 loc) · 5.8 KB
/
Copy pathRBPNet.py
File metadata and controls
205 lines (160 loc) · 5.8 KB
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 torch.nn.functional as F
class FirstLayerConv(nn.Module):
def __init__(
self,
filters=128,
kernel_size=12,
use_bias=False,
activation='relu'
):
super(FirstLayerConv, self).__init__()
# arguments
self.filters = filters
self.kernel_size = kernel_size
self.use_bias = use_bias
self.layer = nn.Conv1d(
in_channels=4,
out_channels=filters,
kernel_size=kernel_size,
padding='same',
bias=use_bias
)
self.activation = nn.ReLU() if activation == 'relu' else None
def forward(self, x):
x = self.layer(x)
if self.activation:
x = self.activation(x)
return x
class BodyConv(nn.Module):
def __init__(
self,
filters=128,
kernel_size=6,
dilation_rate=1,
dropout_rate=0.25,
activation='relu',
batch_norm=True,
residual=True,
use_bias=True
):
super(BodyConv, self).__init__()
self.filters = filters
self.kernel_size = kernel_size
self.dilation_rate = dilation_rate
self.dropout_rate = dropout_rate
self.residual = residual
# Layers
self.conv1d_layer = nn.Conv1d(
in_channels=filters,
out_channels=filters,
kernel_size=kernel_size,
dilation=dilation_rate,
padding='same',
bias=use_bias
)
self.batch_norm = nn.BatchNorm1d(filters) if batch_norm else None
self.activation = nn.ReLU() if activation == 'relu' else None # Add other activations if necessary
self.dropout_layer = nn.Dropout(dropout_rate) if dropout_rate > 0.0 else None
def forward(self, inputs):
# conv
x = self.conv1d_layer(inputs)
# batch_norm
if self.batch_norm:
x = self.batch_norm(x)
# activation
if self.activation:
x = self.activation(x)
# dropout
if self.dropout_layer:
x = self.dropout_layer(x)
# residual
if self.residual:
x = x + inputs
return x
class SequenceAdditiveMixingCoefficient(nn.Module):
def __init__(self, in_features):
super(SequenceAdditiveMixingCoefficient, self).__init__()
self.global_average_pooling = nn.AdaptiveAvgPool1d(1)
self.dense = nn.Linear(in_features=in_features, out_features=1) # Update in_features accordingly
def forward(self, inputs):
x = self.global_average_pooling(inputs)
x = x.view(x.size(0), -1) # Flatten
x = self.dense(x)
return x
class ProfileHead(nn.Module):
def __init__(self, in_channels, kernel_size=21, use_bias=True):
super(ProfileHead, self).__init__()
# arguments
self.in_channels = in_channels
self.kernel_size = kernel_size
self.use_bias = use_bias
self.layer = nn.ConvTranspose1d(
in_channels=in_channels,
out_channels=1,
kernel_size=kernel_size,
padding=kernel_size // 2,
bias=use_bias
)
def forward(self, x):
x = self.layer(x)
x = torch.squeeze(x, 1)
return x
class AdditiveTargetBias(nn.Module):
def __init__(self, penalty=None, stop_bias_gradient=False):
super(AdditiveTargetBias, self).__init__()
self.stop_bias_gradient = stop_bias_gradient
# Implementing the numerically stable log-sum-exp operation
def stable_logsumexp(self, x, y):
max_val = torch.max(x, y)
return max_val + torch.log1p(torch.exp(-torch.abs(x - y)))
def forward(self, inputs, training=False):
logits_t, logits_b, a = inputs
if self.stop_bias_gradient and training:
logits_b = logits_b.detach()
# Compute log probabilities
log_p = logits_t - torch.logsumexp(logits_t, dim=1, keepdim=True)
log_q = logits_b - torch.logsumexp(logits_b, dim=1, keepdim=True)
# Calculate the mixed log probabilities
s = self.stable_logsumexp(a + log_p, log_q)
return s
class RBPNet(nn.Module):
def __init__(
self,
filters=128,
residual_blocks=9,
dilation=True,
use_bias=True,
mask = 100
):
super(RBPNet, self).__init__()
# First conv
self.first_layer_conv = FirstLayerConv(filters=filters)
# Residual blocks
self.body_convs = nn.ModuleList(
[BodyConv(filters=filters, dilation_rate=(2**i if dilation else 1)) for i in range(1, residual_blocks + 1)]
)
# dlog odds
self.dlogodds_bn = torch.nn.BatchNorm1d(1)
# Output heads
self.mixing_coefficient = SequenceAdditiveMixingCoefficient(in_features=filters)
self.signal_head = ProfileHead(in_channels=filters)
self.ctl_head = ProfileHead(in_channels=filters)
self.target_bias_layer = AdditiveTargetBias()
self.mask = mask
def forward(self, x_in):
x = self.first_layer_conv(x_in)
for conv in self.body_convs:
x = conv(x)
# x is[batch_size, channel(filters), L]
# Mixing coefficient
x_mix = self.mixing_coefficient(x)
# Each head
x_signal = self.signal_head(x)
x_ctl = self.ctl_head(x)
# Additive
x_total = self.target_bias_layer((x_signal, x_ctl, x_mix))
# d_log_odds is the sum of signal track
d_log_odds = self.dlogodds_bn(torch.sum(x_signal[:, self.mask:-self.mask-1], dim = -1).unsqueeze(dim = -1)).squeeze()
return x_total, x_signal, x_ctl, x_mix, d_log_odds