-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnetwork.py
More file actions
144 lines (132 loc) · 5.92 KB
/
network.py
File metadata and controls
144 lines (132 loc) · 5.92 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
#!/dors/meilerlab/data/belle6/miniforge3/envs/glypred/bin/python
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch_geometric.nn as tgnn
from torch.distributions import Normal, Independent
from torch_geometric.utils import scatter, softmax
from math import sqrt, log
class AttnNN(nn.Module):
def __init__(self):
super().__init__()
self.embedDim = 128
self.esmEmbed = nn.Linear(2560, self.embedDim)
self.nodeEmbed = nn.Linear(self.embedDim+1, self.embedDim)
self.seqEmbed = nn.Linear(28, self.embedDim)
self.ss = SeqStructBlock(self.embedDim)
self.norm = nn.BatchNorm1d(self.embedDim)
self.nnout = nn.Linear(self.embedDim, 29)
self.inside = 4
self.outside = 8
def forward(self, x, edge_index, edge_attr, batch, hotslice):
batches, counts = torch.unique_consecutive(batch, return_counts=True)
middleSelect = torch.zeros(x.size(0), device=x.device)
middleSelect[0] = 1.
middleSelect[torch.cumsum(counts, dim=0)[:-1]] = 1.
seq = self.seqEmbed(hotslice)
#seq = self.middle(seq.view(-1,(21*self.embedDim))
struct = self.esmEmbed(x[:,1:])
struct = self.nodeEmbed(torch.hstack((struct,x[:,0].unsqueeze(1))))
seqy = torch.randn_like(seq)
seqz = torch.randn_like(seq)
structy = torch.randn_like(struct)
structz = torch.randn_like(struct)
with torch.no_grad():
for n in range(self.outside-1):
for j in range(self.inside):
seqh = seq + seqy + seqz
structh = struct + structy + structz
seqz, structz = self.ss(seqh, structh, edge_index, edge_attr, middleSelect)
seqh = seqy + seqz
structh = structy + structz
seqy, structy = self.ss(seqh, structh, edge_index, edge_attr, middleSelect)
for j in range(self.inside):
seqh = seq + seqy + seqz
structh = struct + structy + structz
seqz, structz = self.ss(seqh, structh, edge_index, edge_attr, middleSelect)
seqh = seqy + seqz
structh = structy + structz
seqy, structy = self.ss(seqh, structh, edge_index, edge_attr, middleSelect)
seq = seqy[:,10,:]
struct = structy[middleSelect.to(torch.bool)]
#print(torch.norm(struct,p=2,dim=1)/torch.norm(seq,p=2,dim=1))
x = self.norm(seq + struct)
if not self.training:
return x
else:
pred = self.nnout(x)
return pred
class SeqStructBlock(nn.Module):
def __init__(self, embedDim, doStruct=True):
super().__init__()
self.seqNorm = nn.LayerNorm(embedDim)
self.structNorm = nn.LayerNorm(embedDim)
self.dropout = nn.Dropout(p=0.2)
self.inModel = nn.Sequential(
nn.Linear(2*embedDim+3,embedDim),
nn.Sigmoid()
)
self.outModel = nn.Sequential(
nn.Linear(2*embedDim+3,embedDim),
nn.Sigmoid()
)
self.inRand = torch.randn(embedDim, device=torch.device("cuda"))
self.outRand = torch.randn(embedDim, device=torch.device("cuda"))
self.seqAttn = nn.LSTM(embedDim, int(embedDim/2), batch_first=True, bidirectional=True)
def forward(self, seq, struct, edge_index, edge_attr, middleSelect):
seqm = int(seq.size(1)/2)
seq = self.seqNorm(seq + self.dropout(self.seqAttn(seq)[0]))
ms = middleSelect.to(torch.bool)
#struct[ms] = struct[ms] + seq[:,seqm,:]
sn = struct
inGate = self.inModel(torch.hstack((sn[edge_index[0,0::2]],sn[edge_index[1,0::2]],edge_attr[1::2])))
outGate = self.outModel(torch.hstack((sn[edge_index[0,0::2]],sn[edge_index[1,0::2]],edge_attr[0::2])))
inMessage = scatter(inGate * sn[edge_index[1,0::2]], edge_index[0,0::2], dim_size=struct.size(0))
struct = struct + self.dropout(inMessage)
outMessage = scatter(outGate * sn[edge_index[0,0::2]], edge_index[1,0::2], dim_size=struct.size(0))
struct = struct + self.dropout(outMessage)
struct = self.structNorm(struct)
#seq[:,seqm,:] = seq[:,seqm,:] + struct[ms]
return seq, struct
class MuyGPLayer(nn.Module):
def __init__(self):
super().__init__()
self.trainX = None
self.trainy = None
self.ymean = nn.Linear(128,29)
#self.ymean = nn.Parameter(torch.zeros((1,29)))
#self.ymean = None
#self.l = nn.Parameter(torch.ones((1,64)))
self.l = nn.Parameter(torch.tensor(1.))
self.a = nn.Parameter(torch.tensor(3.))
self.nn = 128
#This kernel is the RBF kernel (easier to implement than Matern)
def kernel(self, A, B):
A = A
B = B
d = torch.cdist(A, B)
#val = self.a * torch.exp(-torch.pow(d, 2.) / (2. * self.l ** 2))
#val = self.a * (1 + np.sqrt(3) * d / self.l) * torch.exp(-np.sqrt(3) * d / self.l)
val = self.a * torch.exp(-d / self.l)
return val
def forward(self, x):
ymean = self.ymean(x).unsqueeze(1)
dists = torch.cdist(x, self.trainX)
if self.training:
_, neighbors = torch.topk(dists, self.nn+1, largest=False, dim=1)
nX = self.trainX[neighbors[:,1:]]
nY = self.trainy[neighbors[:,1:]]
else:
_, neighbors = torch.topk(dists, self.nn, largest=False, dim=1)
nX = self.trainX[neighbors]
nY = self.trainy[neighbors]
nY = nY - ymean + torch.randn_like(nY)
auto = self.kernel(nX, nX)
autoCov = torch.linalg.inv(auto)
crossCov = self.kernel(x.unsqueeze(1), nX)
kWeights = crossCov @ autoCov
y = kWeights @ nY
yVar = self.a * torch.ones(x.size(0), device=x.device) - \
(kWeights @ crossCov.transpose(1, 2)).squeeze()
return (y + ymean).squeeze(), yVar