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
| class FeedForward(nn.Module): def __init__(self, d_model, d_ff=2048, dropout = 0.1): super().__init__() self.linear_1 = nn.Linear(d_model, d_ff) self.dropout = nn.Dropout(dropout) self.linear_2 = nn.Linear(d_ff, d_model) def forward(self, x): x = self.dropout(F.relu(self.linear_1(x))) x = self.linear_2(x) return x class EncoderLayer(nn.Module): def __init__(self, d_model, heads, dropout=0.1): super().__init__() self.norm_1 = nn.LayerNorm(d_model) self.norm_2 = nn.LayerNorm(d_model) self.attn = nn.MultiheadAttention(d_model, heads, dropout=dropout) self.ff = FeedForward(d_model, dropout=dropout) self.dropout_1 = nn.Dropout(dropout) self.dropout_2 = nn.Dropout(dropout) def forward(self, x, mask): x2, att_weight= self.attn(x,x,x,mask) x = x + self.dropout_1(x2) x = self.norm_1(x) x2 = self.ff(x) x = x+self.dropout_2(x2) x = self.norm_2(x) return x,att_weight
class PositionalEncoder(nn.Module): def __init__(self, d_model, max_seq_len = 200, dropout = 0.1): super().__init__() self.d_model = d_model self.dropout = nn.Dropout(dropout) pe = torch.zeros(max_seq_len, d_model) for pos in range(max_seq_len): for i in range(0, d_model, 2): pe[pos, i] = math.sin(pos / (10000 ** ((2 * i)/d_model))) pe[pos, i + 1] = math.cos(pos / (10000 ** ((2 * (i + 1))/d_model))) pe = pe.unsqueeze(0) self.register_buffer('pe', pe) def forward(self, x): x = x * math.sqrt(self.d_model) seq_len = x.size(1) pe = Variable(self.pe[:,:seq_len], requires_grad=False) if x.is_cuda: pe.cuda() x = x + pe return self.dropout(x) class K_mer_aggregate(nn.Module): def __init__(self,kmers,in_dim,out_dim,dropout=0.1): ''' x: (batch_size, sequence_length, features) return: (batch_size, sequence_length, features) ''' super(K_mer_aggregate, self).__init__() self.dropout=nn.Dropout(dropout) self.convs=[] for i in kmers: print(i) self.convs.append(nn.Conv1d(in_dim,out_dim,i,padding=0)) self.convs=nn.ModuleList(self.convs) self.activation=nn.ReLU(inplace=True) self.norm=nn.LayerNorm(out_dim)
def forward(self,x): x = x.permute(0,2,1) outputs=[] for conv in self.convs: outputs.append(conv(x)) outputs=torch.cat(outputs,dim=2) outputs=self.norm(outputs.permute(0,2,1)) return outputs class LinearDecoder(nn.Module): def __init__(self,d_model,n_class,dropout): super(LinearDecoder, self).__init__() self.global_avg_pool = nn.AdaptiveAvgPool1d(1) self.d_ff = n_class * 8 self.linear_1 = nn.Linear(d_model, self.d_ff) self.relu = nn.ReLU(inplace=True) self.dropout = nn.Dropout(dropout) self.classifier = nn.Linear(self.d_ff, n_class) def forward(self,x): x = x.permute(0,2,1) x = self.global_avg_pool(x).squeeze(2) x = self.dropout(self.relu(self.linear_1(x))) x = self.classifier(x) return x def get_clones(module, n_layers): return nn.ModuleList([copy.deepcopy(module) for i in range(n_layers)])
class Encoder(nn.Module): def __init__(self, vocab_size, d_model, n_layers, heads, n_class, kmers, dropout): super().__init__() self.n_layers = n_layers self.embed = nn.Embedding(vocab_size, d_model) self.pe = PositionalEncoder(d_model, dropout=dropout) self.kmer_aggregation = K_mer_aggregate(kmers,d_model,d_model) self.layers = get_clones(EncoderLayer(d_model, heads, dropout), n_layers) self.norm = nn.LayerNorm(d_model) self.decoder = LinearDecoder(d_model,n_class,dropout)
def forward(self, src, mask=None): x = self.embed(src) x = self.pe(x)
x = self.kmer_aggregation(x)
x = x.permute(1,0,2) attention_weights = [] for i in range(self.N): x, attention_weights_layer = self.layers[i](x, mask) attention_weights.append(attention_weights_layer) attention_weights=torch.stack(attention_weights).permute(1,0,2,3)
x = self.norm(x).permute(1,0,2) x = self.decoder(x)
return x,attention_weights
|