File size: 7,349 Bytes
4f5540c |
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 |
import torch
from torch_geometric.nn import SAGEConv, GATConv, Sequential, BatchNorm
from torch_geometric.nn import SAGPooling
class PolymerGNN_IV_EXPLAIN(torch.nn.Module):
def __init__(self, input_feat, hidden_channels, num_additional = 0):
super(PolymerGNN_IV_EXPLAIN, self).__init__()
self.hidden_channels = hidden_channels
self.Asage = Sequential('x, edge_index, batch', [
(GATConv(input_feat, hidden_channels, aggr = 'max'), 'x, edge_index -> x'),
BatchNorm(hidden_channels, track_running_stats=False),
torch.nn.PReLU(),
(SAGEConv(hidden_channels, hidden_channels, aggr = 'max'), 'x, edge_index -> x'),
BatchNorm(hidden_channels, track_running_stats=False),
torch.nn.PReLU(),
(SAGPooling(hidden_channels), 'x, edge_index, batch=batch -> x'),
])
self.Gsage = Sequential('x, edge_index, batch', [
(GATConv(input_feat, hidden_channels, aggr = 'max'), 'x, edge_index -> x'),
BatchNorm(hidden_channels, track_running_stats=False),
torch.nn.PReLU(),
(SAGEConv(hidden_channels, hidden_channels, aggr = 'max'), 'x, edge_index -> x'),
BatchNorm(hidden_channels, track_running_stats=False),
torch.nn.PReLU(),
(SAGPooling(hidden_channels), 'x, edge_index, batch=batch -> x'),
])
self.fc1 = torch.nn.Linear(hidden_channels * 2 + num_additional, hidden_channels)
self.leaky1 = torch.nn.PReLU()
self.fc2 = torch.nn.Linear(hidden_channels, 1)
def forward(self,
Abatch_X: torch.Tensor,
Abatch_edge_index: torch.Tensor,
Abatch_batch: torch.Tensor,
Gbatch_X: torch.Tensor,
Gbatch_edge_index: torch.Tensor,
Gbatch_batch: torch.Tensor,
add_features: torch.Tensor):
'''
Only thing that's different is the forward method
'''
# Decompose X into acid and glycol
Aembeddings = self.Asage(Abatch_X, Abatch_edge_index, Abatch_batch)[0]
Gembeddings = self.Gsage(Gbatch_X, Gbatch_edge_index, Gbatch_batch)[0]
# self.saveAembed = Aembeddings.clone()
# self.saveGembed = Gembeddings.clone()
Aembed, _ = torch.max(Aembeddings, dim=0)
Gembed, _ = torch.max(Gembeddings, dim=0)
# Aggregate pooled vectors
if add_features is not None:
poolAgg = torch.cat([Aembed, Gembed, add_features])
else:
poolAgg = torch.cat([Aembed, Gembed])
x = self.leaky1(self.fc1(poolAgg))
x = self.fc2(x)
# Because we're predicting log:
return torch.exp(x)
class PolymerGNN_IVMono_EXPLAIN(torch.nn.Module):
def __init__(self, input_feat, hidden_channels, num_additional = 0):
super(PolymerGNN_IVMono_EXPLAIN, self).__init__()
self.hidden_channels = hidden_channels
self.sage = Sequential('x, edge_index, batch', [
(GATConv(input_feat, hidden_channels, aggr = 'max'), 'x, edge_index -> x'),
BatchNorm(hidden_channels, track_running_stats=False),
torch.nn.PReLU(),
(SAGEConv(hidden_channels, hidden_channels, aggr = 'max'), 'x, edge_index -> x'),
BatchNorm(hidden_channels, track_running_stats=False),
torch.nn.PReLU(),
(SAGPooling(hidden_channels), 'x, edge_index, batch=batch -> x'),
])
self.fc1 = torch.nn.Linear(hidden_channels * 2 + num_additional, hidden_channels)
self.leaky1 = torch.nn.PReLU()
self.fc2 = torch.nn.Linear(hidden_channels, 1)
def forward(self,
Abatch_X: torch.Tensor,
Abatch_edge_index: torch.Tensor,
Abatch_batch: torch.Tensor,
Gbatch_X: torch.Tensor,
Gbatch_edge_index: torch.Tensor,
Gbatch_batch: torch.Tensor,
add_features: torch.Tensor):
'''
Only thing that's different is the forward method
'''
# Decompose X into acid and glycol
Aembeddings = self.sage(Abatch_X, Abatch_edge_index, Abatch_batch)[0]
Gembeddings = self.sage(Gbatch_X, Gbatch_edge_index, Gbatch_batch)[0]
# self.saveAembed = Aembeddings.clone()
# self.saveGembed = Gembeddings.clone()
Aembed, _ = torch.max(Aembeddings, dim=0)
Gembed, _ = torch.max(Gembeddings, dim=0)
# Aggregate pooled vectors
if add_features is not None:
poolAgg = torch.cat([Aembed, Gembed, add_features])
else:
poolAgg = torch.cat([Aembed, Gembed])
x = self.leaky1(self.fc1(poolAgg))
x = self.fc2(x)
# Because we're predicting log:
return x
class PolymerGNN_Tg_EXPLAIN(torch.nn.Module):
def __init__(self, input_feat, hidden_channels, num_additional = 0):
super(PolymerGNN_Tg_EXPLAIN, self).__init__()
self.hidden_channels = hidden_channels
self.Asage = Sequential('x, edge_index, batch', [
(GATConv(input_feat, hidden_channels, aggr = 'max'), 'x, edge_index -> x'),
BatchNorm(hidden_channels, track_running_stats=False),
torch.nn.PReLU(),
(SAGEConv(hidden_channels, hidden_channels, aggr = 'max'), 'x, edge_index -> x'),
BatchNorm(hidden_channels, track_running_stats=False),
torch.nn.PReLU(),
(SAGPooling(hidden_channels), 'x, edge_index, batch=batch -> x'),
])
self.Gsage = Sequential('x, edge_index, batch', [
(GATConv(input_feat, hidden_channels, aggr = 'max'), 'x, edge_index -> x'),
BatchNorm(hidden_channels, track_running_stats=False),
torch.nn.PReLU(),
(SAGEConv(hidden_channels, hidden_channels, aggr = 'max'), 'x, edge_index -> x'),
BatchNorm(hidden_channels, track_running_stats=False),
torch.nn.PReLU(),
(SAGPooling(hidden_channels), 'x, edge_index, batch=batch -> x'),
])
self.fc1 = torch.nn.Linear(hidden_channels * 2 + num_additional, hidden_channels)
self.leaky1 = torch.nn.PReLU()
self.fc2 = torch.nn.Linear(hidden_channels, 1)
self.mult_factor = torch.nn.Linear(hidden_channels, 1)
def forward(self,
Abatch_X: torch.Tensor,
Abatch_edge_index: torch.Tensor,
Abatch_batch: torch.Tensor,
Gbatch_X: torch.Tensor,
Gbatch_edge_index: torch.Tensor,
Gbatch_batch: torch.Tensor,
add_features: torch.Tensor):
'''
'''
# Decompose X into acid and glycol
Aembeddings = self.Asage(Abatch_X, Abatch_edge_index, Abatch_batch)[0]
Gembeddings = self.Gsage(Gbatch_X, Gbatch_edge_index, Gbatch_batch)[0]
Aembed, _ = torch.max(Aembeddings, dim=0)
Gembed, _ = torch.max(Gembeddings, dim=0)
# Aggregate pooled vectors
if add_features is not None:
poolAgg = torch.cat([Aembed, Gembed, add_features])
else:
poolAgg = torch.cat([Aembed, Gembed])
x = self.leaky1(self.fc1(poolAgg))
pred = self.fc2(x)
factor = self.mult_factor(x).tanh()
# Because we're predicting log:
return torch.exp(pred) * factor |