-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinaryTensorBased.py
178 lines (149 loc) · 6.6 KB
/
BinaryTensorBased.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
import rdkit
from rdkit import Chem
import torch
import torch.nn as nn
import torch.nn.functional as F
# Access the PeriodicTable
electronegativities = {
1: 2.20, # Hydrogen
6: 2.55, # Carbon
7: 3.04, # Nitrogen
8: 3.44, # Oxygen
9: 3.98, # Fluorine
# Add other elements as needed
}
def hybridization_to_int(hybridization):
hybrid_map = {
Chem.rdchem.HybridizationType.SP: 0,
Chem.rdchem.HybridizationType.SP2: 1,
Chem.rdchem.HybridizationType.SP3: 2,
Chem.rdchem.HybridizationType.SP3D: 2,
Chem.rdchem.HybridizationType.SP3D2: 2
}
return hybrid_map.get(hybridization, 2) # Default to SP3 if not found or unknown
class ConvLayer(nn.Module):
"""
Convolutional operation on graphs
"""
def __init__(self, atom_fea_len, nbr_fea_len):
super(ConvLayer, self).__init__()
self.atom_fea_len = atom_fea_len
self.nbr_fea_len = nbr_fea_len
self.fc_full = nn.Linear(2*self.atom_fea_len + self.nbr_fea_len, 2*self.atom_fea_len)
self.sigmoid = nn.Sigmoid()
self.softplus1 = nn.Softplus()
self.bn1 = nn.BatchNorm1d(2*self.atom_fea_len)
self.bn2 = nn.BatchNorm1d(self.atom_fea_len)
self.softplus2 = nn.Softplus()
def forward(self, atom_in_fea, nbr_fea, nbr_fea_idx):
N, M = nbr_fea_idx.shape
# Gather neighbor atom features
atom_nbr_fea = atom_in_fea[nbr_fea_idx.view(-1)].view(N, M, -1)
# Concatenate atom features, neighbor features, and bond features
total_nbr_fea = torch.cat(
[atom_in_fea.unsqueeze(1).expand(-1, M, -1),
atom_nbr_fea,
nbr_fea], dim=2)
# Fully connected layer followed by activation and normalization
total_gated_fea = self.fc_full(total_nbr_fea)
total_gated_fea = self.bn1(total_gated_fea.view(-1, 2*self.atom_fea_len)).view(N, M, 2*self.atom_fea_len)
# Split into filter and core features
nbr_filter, nbr_core = torch.chunk(total_gated_fea, 2, dim=2)
nbr_filter = self.sigmoid(nbr_filter)
nbr_core = self.softplus1(nbr_core)
# Summing the filtered core features
nbr_sumed = torch.sum(nbr_filter * nbr_core, dim=1)
nbr_sumed = self.bn2(nbr_sumed)
# Update atom features
out = self.softplus2(atom_in_fea + nbr_sumed)
return out
class GCN(nn.Module):
def __init__(self, input_dim, hidden_dim, output_dim, nbr_fea_len):
super(GCN, self).__init__()
# Embedding layer to project input features to hidden_dim
self.embedding = nn.Linear(input_dim, hidden_dim)
self.conv1 = ConvLayer(atom_fea_len=hidden_dim, nbr_fea_len=nbr_fea_len)
self.conv2 = ConvLayer(atom_fea_len=hidden_dim, nbr_fea_len=nbr_fea_len)
self.fc_out = nn.Linear(hidden_dim, output_dim)
def forward(self, atom_fea, nbr_fea, nbr_fea_idx):
# Embedding
atom_fea = self.embedding(atom_fea)
atom_fea = F.relu(atom_fea)
# First convolution
x = self.conv1(atom_fea, nbr_fea, nbr_fea_idx)
x = F.relu(x)
# Second convolution
x = self.conv2(x, nbr_fea, nbr_fea_idx)
x = F.relu(x)
# Pooling: averaging over all atoms to get graph representation
x = torch.mean(x, dim=0, keepdim=True)
# Final output layer
x = self.fc_out(x)
return x
# Initialize model, optimizer, and loss function
model = GCN(input_dim=3, hidden_dim=64, output_dim=2, nbr_fea_len=1)
optimizer = torch.optim.Adam(model.parameters(), lr=0.01)
criterion = torch.nn.CrossEntropyLoss()
# Example dataset: List of tuples (SMILES string, label)
dataset = [('CCO', 1), ('CC', 0), ('OCCO', 0)]
# Training the model
for epoch in range(200):
total_loss = 0
for smiles, label in dataset:
molecule = Chem.MolFromSmiles(smiles)
node_features = []
bond_features = []
bond_indices = []
# Extract atom features
for atom in molecule.GetAtoms():
atomic_num = atom.GetAtomicNum()
electronegativity = electronegativities.get(atomic_num, 2.5) # Default to 2.5 if unknown
hybrid_feat = hybridization_to_int(atom.GetHybridization())
node_features.append([atomic_num, electronegativity, hybrid_feat])
# Convert node features to tensor
node_features = torch.tensor(node_features, dtype=torch.float)
# Extract bond features (assuming a simple bond feature)
for bond in molecule.GetBonds():
bond_indices.append([bond.GetBeginAtomIdx(), bond.GetEndAtomIdx()])
bond_features.append([1]) # Placeholder for bond features
# Handle cases where there are no bonds
if bond_indices:
bond_indices = torch.tensor(bond_indices, dtype=torch.long)
bond_features = torch.tensor(bond_features, dtype=torch.float)
else:
# If no bonds, create empty tensors
bond_indices = torch.empty((0, 2), dtype=torch.long)
bond_features = torch.empty((0, 1), dtype=torch.float)
N = len(node_features)
M = max(len(bond_indices), 1) # Ensure M is at least 1 to avoid zero-dimension tensors
# Create neighbor feature indices and bond features per atom
nbr_fea_idx = torch.zeros((N, M), dtype=torch.long)
nbr_fea = torch.zeros((N, M, 1), dtype=torch.float)
# Build neighbor indices and features
for idx in range(N):
neighbors = []
bond_fea = []
for bond_idx, (a, b) in enumerate(bond_indices.tolist()):
if a == idx:
neighbors.append(b)
bond_fea.append(bond_features[bond_idx])
elif b == idx:
neighbors.append(a)
bond_fea.append(bond_features[bond_idx])
# Pad if necessary
while len(neighbors) < M:
neighbors.append(0)
bond_fea.append(torch.zeros(1))
nbr_fea_idx[idx] = torch.tensor(neighbors[:M])
nbr_fea[idx] = torch.stack(bond_fea[:M])
target = torch.tensor([label], dtype=torch.long)
# Forward pass
model.train()
optimizer.zero_grad()
out = model(node_features, nbr_fea, nbr_fea_idx)
loss = criterion(out, target)
loss.backward()
optimizer.step()
total_loss += loss.item()
if epoch % 10 == 0:
print(f"Epoch: {epoch}, Average Loss: {total_loss / len(dataset)}")