-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.py
205 lines (173 loc) · 7.32 KB
/
main.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable
import torchvision
import torchvision.transforms as transforms
import torch.utils.data as Data
import torch.nn.utils.rnn as rnn_utils
import time
import pickle
from termcolor import colored
def genData(file,max_len):
aa_dict={'A':1,'R':2,'N':3,'D':4,'C':5,'Q':6,'E':7,'G':8,'H':9,'I':10,
'L':11,'K':12,'M':13,'F':14,'P':15,'O':16,'S':17,'U':18,'T':19,
'W':20,'Y':21,'V':22,'X':23}
with open(file, 'r') as inf:
lines = inf.read().splitlines()
long_pep_counter=0
pep_codes=[]
labels=[]
for pep in lines:
pep,label=pep.split(",")
labels.append(int(label))
if not len(pep) > max_len:
current_pep=[]
for aa in pep:
current_pep.append(aa_dict[aa])
pep_codes.append(torch.tensor(current_pep))
else:
long_pep_counter += 1
print("length > 81:",long_pep_counter)
data = rnn_utils.pad_sequence(pep_codes,batch_first=True)
return data,torch.tensor(labels)
data,label=genData("./dataset/Homo_sapiens.csv",81)
print(data.shape,label.shape)
train_data,train_label=data[:70000],label[:70000]
test_data,test_label=data[70000:],label[70000:]
train_dataset = Data.TensorDataset(train_data, train_label)
test_dataset = Data.TensorDataset(test_data, test_label)
batch_size=256
train_iter = torch.utils.data.DataLoader(train_dataset, batch_size=batch_size, shuffle=True)
test_iter = torch.utils.data.DataLoader(test_dataset, batch_size=batch_size, shuffle=False)
class newModel(nn.Module):
def __init__(self, vocab_size=24):
super().__init__()
self.hidden_dim = 25
self.batch_size = 256
self.emb_dim = 512
self.embedding = nn.Embedding(vocab_size, self.emb_dim, padding_idx=0)
self.encoder_layer = nn.TransformerEncoderLayer(d_model=512, nhead=8)
self.transformer_encoder = nn.TransformerEncoder(self.encoder_layer, num_layers=1)
self.gru = nn.GRU(self.emb_dim, self.hidden_dim, num_layers=2,
bidirectional=True, dropout=0.2)
self.block1=nn.Sequential(nn.Linear(4050,1024),
nn.BatchNorm1d(1024),
nn.LeakyReLU(),
nn.Linear(1024,256),
)
self.block2=nn.Sequential(
nn.BatchNorm1d(256),
nn.LeakyReLU(),
nn.Linear(256,128),
nn.BatchNorm1d(128),
nn.LeakyReLU(),
nn.Linear(128,64),
nn.BatchNorm1d(64),
nn.LeakyReLU(),
nn.Linear(64,2)
)
def forward(self, x):
x=self.embedding(x)
output=self.transformer_encoder(x).permute(1, 0, 2)
output,hn=self.gru(output)
output=output.permute(1,0,2)
hn=hn.permute(1,0,2)
output=output.reshape(output.shape[0],-1)
hn=hn.reshape(output.shape[0],-1)
output=torch.cat([output,hn],1)
# print(output.shape,hn.shape)
return self.block1(output)
def trainModel(self, x):
with torch.no_grad():
output=self.forward(x)
return self.block2(output)
class ContrastiveLoss(torch.nn.Module):
def __init__(self, margin=2.0):
super(ContrastiveLoss, self).__init__()
self.margin = margin
def forward(self, output1, output2, label):
# euclidean_distance: [128]
euclidean_distance = F.pairwise_distance(output1, output2)
loss_contrastive = torch.mean((1-label) * torch.pow(euclidean_distance, 2) + # calmp夹断用法
(label) * torch.pow(torch.clamp(self.margin - euclidean_distance, min=0.0), 2))
return loss_contrastive
def collate(batch):
seq1_ls=[]
seq2_ls=[]
label1_ls=[]
label2_ls=[]
label_ls=[]
batch_size=len(batch)
for i in range(int(batch_size/2)):
seq1,label1=batch[i][0],batch[i][1]
seq2,label2=batch[i+int(batch_size/2)][0],batch[i+int(batch_size/2)][1]
label1_ls.append(label1.unsqueeze(0))
label2_ls.append(label2.unsqueeze(0))
label=(label1^label2)
seq1_ls.append(seq1.unsqueeze(0))
seq2_ls.append(seq2.unsqueeze(0))
label_ls.append(label.unsqueeze(0))
seq1=torch.cat(seq1_ls).to(device)
seq2=torch.cat(seq2_ls).to(device)
label=torch.cat(label_ls).to(device)
label1=torch.cat(label1_ls).to(device)
label2=torch.cat(label2_ls).to(device)
return seq1,seq2,label,label1,label2
train_iter_cont = torch.utils.data.DataLoader(train_dataset, batch_size=batch_size,
shuffle=True,collate_fn=collate)
device = torch.device("cuda",1)
def evaluate_accuracy(data_iter, net):
acc_sum, n = 0.0, 0
for x, y in data_iter:
x,y=x.to(device),y.to(device)
outputs=net.trainModel(x)
acc_sum += (outputs.argmax(dim=1) == y).float().sum().item()
n += y.shape[0]
return acc_sum / n
def to_log(log):
with open("./modelLog.log","a+") as f:
f.write(log+'\n')
for num_model in range(10):
net=newModel().to(device)
lr = 0.0001
optimizer = torch.optim.Adam(net.parameters(), lr=lr,weight_decay=5e-4)
criterion = ContrastiveLoss()
criterion_model = nn.CrossEntropyLoss(reduction='sum')
best_acc=0
EPOCH=250
for epoch in range(EPOCH):
loss_ls=[]
loss1_ls=[]
loss2_3_ls=[]
t0=time.time()
net.train()
for seq1,seq2,label,label1,label2 in train_iter_cont:
output1=net(seq1)
output2=net(seq2)
output3=net.trainModel(seq1)
output4=net.trainModel(seq2)
loss1=criterion(output1, output2, label)
loss2=criterion_model(output3,label1)
loss3=criterion_model(output4,label2)
loss=loss1+loss2+loss3
# print(loss)
optimizer.zero_grad()
loss.backward()
optimizer.step()
loss_ls.append(loss.item())
loss1_ls.append(loss1.item())
loss2_3_ls.append((loss2+loss3).item())
net.eval()
with torch.no_grad():
train_acc=evaluate_accuracy(train_iter,net)
test_acc=evaluate_accuracy(test_iter,net)
results=f"epoch: {epoch+1}, loss: {np.mean(loss_ls):.5f}, loss1: {np.mean(loss1_ls):.5f}, loss2_3: {np.mean(loss2_3_ls):.5f}\n"
results+=f'\ttrain_acc: {train_acc:.4f}, test_acc: {colored(test_acc,"red")}, time: {time.time()-t0:.2f}'
print(results)
to_log(results)
if test_acc>best_acc:
best_acc=test_acc
torch.save({"best_acc":best_acc,"model":net.state_dict()},f'./Model/{num_model}.pl')
print(f"best_acc: {best_acc}")