Delete modeling_bbb_model.py
Browse files- modeling_bbb_model.py +0 -150
modeling_bbb_model.py
DELETED
|
@@ -1,150 +0,0 @@
|
|
| 1 |
-
import torch
|
| 2 |
-
import torch.nn as nn
|
| 3 |
-
import torch.nn.functional as F
|
| 4 |
-
from torch_geometric.nn import GATConv
|
| 5 |
-
|
| 6 |
-
from transformers import PreTrainedModel
|
| 7 |
-
from transformers.modeling_outputs import BaseModelOutputWithPooling, SequenceClassifierOutput
|
| 8 |
-
|
| 9 |
-
from .configuration_bbb_model import BBBConfig
|
| 10 |
-
|
| 11 |
-
class BBBModel(PreTrainedModel):
|
| 12 |
-
config_class = BBBConfig
|
| 13 |
-
|
| 14 |
-
def __init__(self, config: BBBConfig):
|
| 15 |
-
super().__init__(config)
|
| 16 |
-
|
| 17 |
-
self.config = config
|
| 18 |
-
|
| 19 |
-
self.activation = nn.LeakyReLU()
|
| 20 |
-
|
| 21 |
-
self.gats = nn.ModuleList()
|
| 22 |
-
self.bns = nn.ModuleList()
|
| 23 |
-
|
| 24 |
-
for i in range(config.gnn_layers):
|
| 25 |
-
if i == 0:
|
| 26 |
-
self.gats.append(GATConv(config.input_dim,
|
| 27 |
-
config.gnn_hidden,
|
| 28 |
-
heads=config.num_heads,
|
| 29 |
-
concat=True,
|
| 30 |
-
dropout=config.dropout))
|
| 31 |
-
else:
|
| 32 |
-
self.gats.append(GATConv(config.gnn_hidden * config.num_heads,
|
| 33 |
-
config.gnn_hidden,
|
| 34 |
-
heads=config.num_heads,
|
| 35 |
-
concat=True,
|
| 36 |
-
dropout=config.dropout))
|
| 37 |
-
self.bns.append(nn.BatchNorm1d(config.gnn_hidden * config.num_heads))
|
| 38 |
-
|
| 39 |
-
self.proj_gnn = nn.Sequential(
|
| 40 |
-
nn.Linear(config.gnn_hidden * config.num_heads, config.proj_dim),
|
| 41 |
-
nn.LeakyReLU(),
|
| 42 |
-
nn.Dropout(config.dropout),
|
| 43 |
-
nn.BatchNorm1d(config.proj_dim)
|
| 44 |
-
)
|
| 45 |
-
|
| 46 |
-
self.proj_feat = nn.Sequential(
|
| 47 |
-
nn.Linear(config.num_features, config.proj_dim),
|
| 48 |
-
nn.LeakyReLU(),
|
| 49 |
-
nn.Dropout(config.dropout),
|
| 50 |
-
nn.BatchNorm1d(config.proj_dim)
|
| 51 |
-
)
|
| 52 |
-
|
| 53 |
-
def forward(self,
|
| 54 |
-
x: torch.Tensor = None,
|
| 55 |
-
edge_index: torch.Tensor = None,
|
| 56 |
-
batch: torch.Tensor = None,
|
| 57 |
-
features: torch.Tensor = None,
|
| 58 |
-
# standard input from HF
|
| 59 |
-
output_attentions = None,
|
| 60 |
-
output_hidden_states=None,
|
| 61 |
-
return_dict=None):
|
| 62 |
-
|
| 63 |
-
if x is None or edge_index is None or batch is None or features is None:
|
| 64 |
-
raise ValueError('You have to specify x, edge_index, batch, and features')
|
| 65 |
-
|
| 66 |
-
for gat, bn in zip(self.gats, self.bns):
|
| 67 |
-
x = gat(x, edge_index)
|
| 68 |
-
x = bn(x)
|
| 69 |
-
x = self.activation(x)
|
| 70 |
-
|
| 71 |
-
super_nodes = []
|
| 72 |
-
for i in range(batch.max().item() + 1):
|
| 73 |
-
mask = (batch == i)
|
| 74 |
-
node_idx = mask.nonzero(as_tuple=True)[0]
|
| 75 |
-
super_node_idx = node_idx[-1]
|
| 76 |
-
super_nodes.append(x[super_node_idx])
|
| 77 |
-
|
| 78 |
-
x_super_nodes = torch.stack(super_nodes, dim=0)
|
| 79 |
-
|
| 80 |
-
gnn_proj = self.proj_gnn(x_super_nodes)
|
| 81 |
-
feat_proj = self.proj_feat(features)
|
| 82 |
-
|
| 83 |
-
combined = torch.cat([gnn_proj, feat_proj], dim=1)
|
| 84 |
-
|
| 85 |
-
return BaseModelOutputWithPooling(
|
| 86 |
-
last_hidden_state=x_super_nodes,
|
| 87 |
-
pooler_output=combined,
|
| 88 |
-
hidden_states=None,
|
| 89 |
-
attentions=None
|
| 90 |
-
)
|
| 91 |
-
|
| 92 |
-
class BBBModelForSequenceClassification(PreTrainedModel):
|
| 93 |
-
config_class = BBBConfig
|
| 94 |
-
|
| 95 |
-
def __init__(self, config: BBBConfig):
|
| 96 |
-
super().__init__(config)
|
| 97 |
-
|
| 98 |
-
self.config = config
|
| 99 |
-
|
| 100 |
-
self.base_model = BBBModel(config)
|
| 101 |
-
|
| 102 |
-
layers = []
|
| 103 |
-
input_dim = config.proj_dim * 2
|
| 104 |
-
for output_dim in config.neurons_fc:
|
| 105 |
-
layers.append(nn.Linear(input_dim, output_dim))
|
| 106 |
-
layers.append(nn.LeakyReLU())
|
| 107 |
-
layers.append(nn.Dropout(config.dropout))
|
| 108 |
-
layers.append(nn.BatchNorm1d(output_dim))
|
| 109 |
-
input_dim = output_dim
|
| 110 |
-
|
| 111 |
-
layers.append(nn.Linear(input_dim, 1))
|
| 112 |
-
self.fc = nn.Sequential(*layers)
|
| 113 |
-
|
| 114 |
-
def forward(self,
|
| 115 |
-
x: torch.Tensor = None,
|
| 116 |
-
edge_index: torch.Tensor = None,
|
| 117 |
-
batch: torch.Tensor = None,
|
| 118 |
-
features: torch.Tensor = None,
|
| 119 |
-
labels: torch.Tensor = None,
|
| 120 |
-
# standard input from HF
|
| 121 |
-
output_attentions = None,
|
| 122 |
-
output_hidden_states=None,
|
| 123 |
-
return_dict=None):
|
| 124 |
-
|
| 125 |
-
return_dict = return_dict if return_dict is not None else self.config.use_return_dict
|
| 126 |
-
|
| 127 |
-
outputs = self.base_model(x=x,
|
| 128 |
-
edge_index=edge_index,
|
| 129 |
-
batch=batch,
|
| 130 |
-
features=features,
|
| 131 |
-
return_dict=return_dict)
|
| 132 |
-
|
| 133 |
-
combined_features = outputs.pooler_output
|
| 134 |
-
logits = self.fc(combined_features)
|
| 135 |
-
|
| 136 |
-
loss = None
|
| 137 |
-
if labels is not None:
|
| 138 |
-
if self.config.task == "regression":
|
| 139 |
-
loss_fct = nn.MSELoss()
|
| 140 |
-
loss = loss_fct(logits.squeeze(-1), labels.squeeze(-1))
|
| 141 |
-
elif self.config.task == "classification":
|
| 142 |
-
loss_fct = nn.BCEWithLogitsLoss()
|
| 143 |
-
loss = loss_fct(logits, labels.float().unsqueeze(-1))
|
| 144 |
-
|
| 145 |
-
return SequenceClassifierOutput(
|
| 146 |
-
loss=loss,
|
| 147 |
-
logits=logits,
|
| 148 |
-
hidden_states=outputs.hidden_states,
|
| 149 |
-
attentions=outputs.attentions
|
| 150 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|