-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel.py
More file actions
172 lines (134 loc) · 5.17 KB
/
Copy pathmodel.py
File metadata and controls
172 lines (134 loc) · 5.17 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
import logging
import torch
import torch.nn as nn
import torch.nn.functional as F
# Based on our current dataset, we have 40 clades.
# Check the log printed in the preprocess stage to see how many clades our dataset has.
CLADE_NUMBERS: int = 40
class CNNDecoder(nn.Module):
conv: nn.Conv2d
fc_conv: nn.Linear
def __init__(
self,
kernel_size: tuple[int, int],
original_target_length: int,
conv_target_length: int,
in_channels: int = 1,
out_channels: int = 1,
stride: tuple[int, int] = (1, 1),
padding: tuple[int, int] = (0, 0),
):
super(CNNDecoder, self).__init__()
self.conv = nn.Conv2d(
in_channels=in_channels,
out_channels=out_channels,
kernel_size=kernel_size,
stride=stride,
padding=padding,
)
# Use a fully connected layer to reduce the length of the sequence
self.fc_conv = nn.Linear(original_target_length, conv_target_length)
def forward(self, x: torch.Tensor) -> torch.Tensor:
x = self.conv(x)
x = F.relu(x)
x = self.fc_conv(x)
return x
class Transformer(nn.Module):
encoder_layer: nn.TransformerEncoderLayer
transformer_encoder: nn.TransformerEncoder
def __init__(
self,
input_dim: int,
dim_feedforward: int = 256, # Number of neurons in the feedforward network model
num_heads: int = 1,
dropout: float = 0.1,
activation: str = "gelu",
num_layers: int = 3, # Number of sub-layers in the encoder
):
super(Transformer, self).__init__()
self.encoder_layer = nn.TransformerEncoderLayer(
d_model=input_dim,
nhead=num_heads,
dim_feedforward=dim_feedforward,
dropout=dropout,
activation=activation,
batch_first=True
)
self.transformer_encoder = nn.TransformerEncoder(self.encoder_layer, num_layers=num_layers)
def forward(self, x: torch.Tensor) -> torch.Tensor:
x = self.transformer_encoder(x)
return x
class MultilayerPerceptron(nn.Module):
fc1: nn.Linear
fc2: nn.Linear
fc3: nn.Linear
def __init__(self, input_dim: int, output_dim: int):
super(MultilayerPerceptron, self).__init__()
self.fc1 = nn.Linear(input_dim, 128)
self.fc2 = nn.Linear(128, 32)
self.fc3 = nn.Linear(32, output_dim)
def forward(self, x: torch.Tensor) -> torch.Tensor:
x = self.fc1(x)
x = F.elu(x)
x = nn.Dropout(p=0.5)(x)
x = self.fc2(x)
x = F.elu(x)
x = nn.Dropout(p=0.3)(x)
x = self.fc3(x)
return x
class DNASequenceClassifier(nn.Module):
cnn1: CNNDecoder
cnn2: CNNDecoder
cnn3: CNNDecoder
batch_norm_1d: nn.BatchNorm1d
transformer: Transformer
mlp: MultilayerPerceptron
def __init__(self, original_target_length: int, conv_target_length: int):
super(DNASequenceClassifier, self).__init__()
self.cnn1 = CNNDecoder(
kernel_size=(3, 4),
padding=(1, 0),
original_target_length=original_target_length,
conv_target_length=conv_target_length
)
self.cnn2 = CNNDecoder(
kernel_size=(2, 1),
original_target_length=original_target_length - 1,
conv_target_length=conv_target_length
)
self.cnn3 = CNNDecoder(
kernel_size=(2, 1),
original_target_length=original_target_length - 2,
conv_target_length=conv_target_length
)
self.batch_norm_1d = nn.BatchNorm1d(conv_target_length * 3)
self.transformer = Transformer(input_dim=conv_target_length * 3)
self.mlp = MultilayerPerceptron(input_dim=conv_target_length * 3, output_dim=CLADE_NUMBERS)
def forward(self, x: torch.Tensor, logger: logging.Logger) -> torch.Tensor:
"""
The forward function of the model.
The input tensor should be in the shape of [batch_size, sequence_length, 4].
:param src:
:param logger:
:return torch.Tensor:
"""
# Reshape the input tensor to [batch_size, 1, sequence_length, channels] to fit the input requirement of conv2d
x = x.unsqueeze(1)
logger.info(f'After unsqueeze: {x.shape}')
cnn1_out: torch.Tensor = self.cnn1(x)
cnn2_out: torch.Tensor = self.cnn2(x)
cnn3_out: torch.Tensor = self.cnn3(x)
logger.info(f'cnn1_out: {cnn1_out.shape}, cnn2_out: {cnn2_out.shape}, cnn3_out: {cnn3_out.shape}')
x = torch.cat((cnn1_out, cnn2_out, cnn3_out), dim=1)
logger.info(f'combined cnn output: {x.shape}')
x = self.batch_norm_1d(x)
logger.info(f'After batch norm: {x.shape}')
# Reshape the tensor to [batch_size, combined_channels, sequence_length] to
# fit the input requirement of Transformer
x = x.squeeze()
logger.info(f'After squeeze: {x.shape}')
x = self.transformer(x)
logger.info(f'After transformer: {x.shape}')
x = self.mlp(x)
logger.info(f'After mlp: {x.shape}')
return x