forked from IdoSpringer/ERGO
-
Notifications
You must be signed in to change notification settings - Fork 2
/
ERGO_models.py
179 lines (164 loc) · 7.96 KB
/
ERGO_models.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
import torch
import torch.autograd as autograd
import torch.nn as nn
import torch.nn.functional as F
from torch.nn.utils.rnn import pack_padded_sequence, pad_packed_sequence
class DoubleLSTMClassifier(nn.Module):
def __init__(self, embedding_dim, lstm_dim, dropout, device):
super(DoubleLSTMClassifier, self).__init__()
# GPU
self.device = device
# Dimensions
self.embedding_dim = embedding_dim
self.lstm_dim = lstm_dim
self.dropout = dropout
# Embedding matrices - 20 amino acids + padding
self.tcr_embedding = nn.Embedding(20 + 1, embedding_dim, padding_idx=0)
self.pep_embedding = nn.Embedding(20 + 1, embedding_dim, padding_idx=0)
# RNN - LSTM
self.tcr_lstm = nn.LSTM(embedding_dim, lstm_dim, num_layers=2, batch_first=True, dropout=dropout)
self.pep_lstm = nn.LSTM(embedding_dim, lstm_dim, num_layers=2, batch_first=True, dropout=dropout)
# MLP
self.hidden_layer = nn.Linear(lstm_dim * 2, lstm_dim)
self.relu = torch.nn.LeakyReLU()
self.output_layer = nn.Linear(lstm_dim, 1)
self.dropout = nn.Dropout(p=dropout)
def init_hidden(self, batch_size):
return (autograd.Variable(torch.zeros(2, batch_size, self.lstm_dim)).to(self.device),
autograd.Variable(torch.zeros(2, batch_size, self.lstm_dim)).to(self.device))
def lstm_pass(self, lstm, padded_embeds, lengths):
# Before using PyTorch pack_padded_sequence we need to order the sequences batch by descending sequence length
lengths, perm_idx = lengths.sort(0, descending=True)
padded_embeds = padded_embeds[perm_idx]
# Pack the batch and ignore the padding
padded_embeds = torch.nn.utils.rnn.pack_padded_sequence(padded_embeds, lengths, batch_first=True)
# Initialize the hidden state
batch_size = len(lengths)
hidden = self.init_hidden(batch_size)
# Feed into the RNN
lstm_out, hidden = lstm(padded_embeds, hidden)
# Unpack the batch after the RNN
lstm_out, lengths = torch.nn.utils.rnn.pad_packed_sequence(lstm_out, batch_first=True)
# Remember that our outputs are sorted. We want the original ordering
_, unperm_idx = perm_idx.sort(0)
lstm_out = lstm_out[unperm_idx]
lengths = lengths[unperm_idx]
return lstm_out
def forward(self, tcrs, tcr_lens, peps, pep_lens):
# TCR Encoder:
# Embedding
tcr_embeds = self.tcr_embedding(tcrs)
# LSTM Acceptor
tcr_lstm_out = self.lstm_pass(self.tcr_lstm, tcr_embeds, tcr_lens)
tcr_last_cell = torch.cat([tcr_lstm_out[i, j.data - 1] for i, j in enumerate(tcr_lens)]).view(len(tcr_lens), self.lstm_dim)
# PEPTIDE Encoder:
# Embedding
pep_embeds = self.pep_embedding(peps)
# LSTM Acceptor
pep_lstm_out = self.lstm_pass(self.pep_lstm, pep_embeds, pep_lens)
pep_last_cell = torch.cat([pep_lstm_out[i, j.data - 1] for i, j in enumerate(pep_lens)]).view(len(pep_lens), self.lstm_dim)
# MLP Classifier
tcr_pep_concat = torch.cat([tcr_last_cell, pep_last_cell], 1)
hidden_output = self.dropout(self.relu(self.hidden_layer(tcr_pep_concat)))
mlp_output = self.output_layer(hidden_output)
output = F.sigmoid(mlp_output)
return output
class PaddingAutoencoder(nn.Module):
def __init__(self, input_len, input_dim, encoding_dim):
super(PaddingAutoencoder, self).__init__()
self.input_dim = input_dim
self.input_len = input_len
self.encoding_dim = encoding_dim
# Encoder
self.encoder = nn.Sequential(
nn.Linear(self.input_len * self.input_dim, 300),
nn.ELU(),
nn.Dropout(0.1),
nn.Linear(300, 100),
nn.ELU(),
nn.Dropout(0.1),
nn.Linear(100, self.encoding_dim))
# Decoder
self.decoder = nn.Sequential(
nn.Linear(self.encoding_dim, 100),
nn.ELU(),
nn.Dropout(0.1),
nn.Linear(100, 300),
nn.ELU(),
nn.Dropout(0.1),
nn.Linear(300, self.input_len * self.input_dim))
def forward(self, batch_size, padded_input):
concat = padded_input.view(batch_size, self.input_len * self.input_dim)
encoded = self.encoder(concat)
decoded = self.decoder(encoded)
decoding = decoded.view(batch_size, self.input_len, self.input_dim)
decoding = F.softmax(decoding, dim=2)
return decoding
pass
class AutoencoderLSTMClassifier(nn.Module):
def __init__(self, embedding_dim, device, max_len, input_dim, encoding_dim, batch_size, ae_file, train_ae):
super(AutoencoderLSTMClassifier, self).__init__()
# GPU
self.device = device
# Dimensions
self.embedding_dim = embedding_dim
self.lstm_dim = encoding_dim
self.max_len = max_len
self.input_dim = input_dim
self.batch_size = batch_size
# TCR Autoencoder
self.autoencoder = PaddingAutoencoder(max_len, input_dim, encoding_dim)
checkpoint = torch.load(ae_file, map_location=device)
self.autoencoder.load_state_dict(checkpoint['model_state_dict'])
if train_ae is False:
for param in self.autoencoder.parameters():
param.requires_grad = False
self.autoencoder.eval()
# Embedding matrices - 20 amino acids + padding
self.pep_embedding = nn.Embedding(20 + 1, embedding_dim, padding_idx=0)
# RNN - LSTM
self.pep_lstm = nn.LSTM(embedding_dim, self.lstm_dim, num_layers=2, batch_first=True, dropout=0.1)
# MLP
self.mlp_dim = self.lstm_dim + encoding_dim
self.hidden_layer = nn.Linear(self.mlp_dim, self.mlp_dim // 2)
self.relu = torch.nn.LeakyReLU()
self.output_layer = nn.Linear(self.mlp_dim // 2, 1)
self.dropout = nn.Dropout(p=0.1)
def init_hidden(self, batch_size):
return (autograd.Variable(torch.zeros(2, batch_size, self.lstm_dim)).to(self.device),
autograd.Variable(torch.zeros(2, batch_size, self.lstm_dim)).to(self.device))
def lstm_pass(self, lstm, padded_embeds, lengths):
# Before using PyTorch pack_padded_sequence we need to order the sequences batch by descending sequence length
lengths, perm_idx = lengths.sort(0, descending=True)
padded_embeds = padded_embeds[perm_idx]
# Pack the batch and ignore the padding
padded_embeds = torch.nn.utils.rnn.pack_padded_sequence(padded_embeds, lengths, batch_first=True)
# Initialize the hidden state
batch_size = len(lengths)
hidden = self.init_hidden(batch_size)
# Feed into the RNN
lstm_out, hidden = lstm(padded_embeds, hidden)
# Unpack the batch after the RNN
lstm_out, lengths = torch.nn.utils.rnn.pad_packed_sequence(lstm_out, batch_first=True)
# Remember that our outputs are sorted. We want the original ordering
_, unperm_idx = perm_idx.sort(0)
lstm_out = lstm_out[unperm_idx]
lengths = lengths[unperm_idx]
return lstm_out
def forward(self, padded_tcrs, peps, pep_lens):
# TCR Encoder:
# Embedding
concat = padded_tcrs.view(self.batch_size, self.max_len * self.input_dim)
encoded_tcrs = self.autoencoder.encoder(concat)
# PEPTIDE Encoder:
# Embedding
pep_embeds = self.pep_embedding(peps)
# LSTM Acceptor
pep_lstm_out = self.lstm_pass(self.pep_lstm, pep_embeds, pep_lens)
pep_last_cell = torch.cat([pep_lstm_out[i, j.data - 1] for i, j in enumerate(pep_lens)]).view(len(pep_lens), self.lstm_dim)
# MLP Classifier
tcr_pep_concat = torch.cat([encoded_tcrs, pep_last_cell], 1)
hidden_output = self.dropout(self.relu(self.hidden_layer(tcr_pep_concat)))
mlp_output = self.output_layer(hidden_output)
output = F.sigmoid(mlp_output)
return output