-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathresppi_reg.py
96 lines (79 loc) · 3.48 KB
/
resppi_reg.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
#!/usr/bin/env
# coding:utf-8
"""
Created on 2021/1/26 下午3:29
base Info
"""
__author__ = 'xx'
__version__ = '1.0'
import torch.nn.functional as F
import torch.nn as nn
import torch
class BasicBlock2D(nn.Module):
def __init__(self, in_channel, out_channel, kernel_size=3, padding=1, res_connect=True):
super(BasicBlock2D, self).__init__()
self.res_connect = res_connect
self.residual_function = nn.Sequential(
nn.Conv2d(in_channel, out_channel//2, kernel_size=kernel_size, padding=padding, bias=False),
nn.BatchNorm2d(out_channel//2),
nn.ReLU(inplace=True),
nn.Conv2d(out_channel//2, out_channel//2, kernel_size=kernel_size, padding=padding, bias=False),
nn.BatchNorm2d(out_channel//2),
nn.ReLU(inplace=True),
nn.Conv2d(out_channel//2, out_channel, kernel_size=kernel_size, padding=padding, bias=False),
nn.BatchNorm2d(out_channel),
)
if self.res_connect is True:
self.shortcut = nn.Sequential(
nn.Conv2d(in_channel, out_channel, kernel_size=1, padding=0, bias=False),
nn.BatchNorm2d(out_channel),
)
def forward(self, x):
residual = self.residual_function(x)
if self.res_connect:
residual += self.shortcut(x)
residual = F.relu(residual)
return residual
class ResPPIReg(nn.Module):
def __init__(self,
amino_ft_dim,
max_antibody_len,
max_virus_len,
h_dim=512,
dropout=0.3,
):
super(ResPPIReg, self).__init__()
self.h_dim = h_dim
self.amino_ft_dim = amino_ft_dim
self.max_antibody_len = max_antibody_len
self.max_virus_len = max_virus_len
self.dropout = dropout
self.mid_channels = 16
self.out_linear1 = nn.Linear(self.mid_channels * self.amino_ft_dim * 2, self.h_dim)
self.out_linear2 = nn.Linear(self.h_dim, 1)
self.res_net = nn.Sequential(
BasicBlock2D(in_channel=1, out_channel=self.mid_channels, res_connect=True),
BasicBlock2D(in_channel=self.mid_channels, out_channel=self.mid_channels, res_connect=False),
BasicBlock2D(in_channel=self.mid_channels, out_channel=self.mid_channels, res_connect=True),
# BasicBlock2D(in_channel=64, out_channel=64, res_connect=False),
# BasicBlock2D(in_channel=64, out_channel=64, res_connect=True),
)
self.activation = nn.ELU()
def forward(self, batch_antibody_onehot_ft, batch_virus_onehot_ft):
'''
:param batch_antibody_ft: tensor batch, antibody_dim
:param batch_virus_ft: tensor batch, virus_dim
:return:
'''
batch_size = batch_antibody_onehot_ft.size()[0]
batch_virus_onehot_ft = batch_virus_onehot_ft.unsqueeze(1)
batch_antibody_onehot_ft = batch_antibody_onehot_ft.unsqueeze(1)
virus_ft = self.res_net(batch_virus_onehot_ft)
antibody_ft = self.res_net(batch_antibody_onehot_ft)
virus_ft = F.max_pool2d(virus_ft, kernel_size=[self.max_virus_len, 1]).view(batch_size, -1)
antibody_ft = F.max_pool2d(antibody_ft, kernel_size=[self.max_antibody_len, 1]).view(batch_size, -1)
pair_ft = torch.cat([virus_ft, antibody_ft], dim=-1)
pair_ft = self.out_linear1(pair_ft)
pair_ft = self.activation(pair_ft)
pred = self.out_linear2(pair_ft)
return pred