File size: 11,610 Bytes
269b7a0 304ac66 269b7a0 6a160bd 304ac66 269b7a0 |
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 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 |
# coding=utf-8
import warnings
import logging
from typing import Optional, Tuple, Union
import os
import torch
import torch.nn as nn
import torch.nn.functional as F
from transformers import MegatronBertConfig, MegatronBertModel, MegatronBertForMaskedLM, MegatronBertPreTrainedModel, PreTrainedModel
from transformers.modeling_outputs import SequenceClassifierOutput
from transformers.utils.hub import cached_file
#from prokbert.training_utils import compute_metrics_eval_prediction
class BertForBinaryClassificationWithPooling(nn.Module):
"""
ProkBERT model for binary classification with custom pooling.
This model extends a pre-trained `MegatronBertModel` by adding a weighting layer
to compute a weighted sum over the sequence outputs, followed by a classifier.
Attributes:
base_model (MegatronBertModel): The base BERT model.
weighting_layer (nn.Linear): Linear layer to compute weights for each token.
dropout (nn.Dropout): Dropout layer.
classifier (nn.Linear): Linear layer for classification.
"""
def __init__(self, base_model: MegatronBertModel):
"""
Initialize the BertForBinaryClassificationWithPooling model.
Args:
base_model (MegatronBertModel): A pre-trained `MegatronBertModel` instance.
"""
super(BertForBinaryClassificationWithPooling, self).__init__()
self.base_model = base_model
self.base_model_config_dict = base_model.config.to_dict()
self.hidden_size = self.base_model_config_dict['hidden_size']
self.dropout_rate = self.base_model_config_dict['hidden_dropout_prob']
self.weighting_layer = nn.Linear(self.hidden_size, 1)
self.dropout = nn.Dropout(self.dropout_rate)
self.classifier = nn.Linear(self.hidden_size, 2)
def forward(self, input_ids, attention_mask=None, labels=None, output_hidden_states=False, output_pooled_output=False):
# Modified call to base model to include output_hidden_states
outputs = self.base_model(input_ids, attention_mask=attention_mask, output_hidden_states=output_hidden_states)
sequence_output = outputs[0]
# Compute weights for each position in the sequence
weights = self.weighting_layer(sequence_output)
weights = torch.nn.functional.softmax(weights, dim=1)
# Compute weighted sum
pooled_output = torch.sum(weights * sequence_output, dim=1)
# Classification head
pooled_output = self.dropout(pooled_output)
logits = self.classifier(pooled_output)
# Prepare the output as a dictionary
output = {"logits": logits}
# Include hidden states in output if requested
if output_hidden_states:
output["hidden_states"] = outputs.hidden_states
if output_pooled_output:
output["pooled_output"] = pooled_output
# If labels are provided, compute the loss
if labels is not None:
loss_fct = torch.nn.CrossEntropyLoss()
loss = loss_fct(logits.view(-1, 2), labels.view(-1))
output["loss"] = loss
return output
def save_pretrained(self, save_directory):
"""
Save the model weights and configuration in a directory.
Args:
save_directory (str): Directory where the model and configuration can be saved.
"""
print('The save pretrained is called!')
if not os.path.exists(save_directory):
os.makedirs(save_directory)
model_path = os.path.join(save_directory, "pytorch_model.bin")
torch.save(self.state_dict(), model_path)
print(f'The save directory is: {save_directory}')
self.base_model.config.save_pretrained(save_directory)
@classmethod
def from_pretrained(cls, pretrained_model_name_or_path, *model_args, **kwargs):
"""
Load the model weights and configuration from a local directory or Hugging Face Hub.
Args:
pretrained_model_name_or_path (str): Directory path where the model and configuration were saved, or name of the model in Hugging Face Hub.
Returns:
model: Instance of BertForBinaryClassificationWithPooling.
"""
# Determine if the path is local or from Hugging Face Hub
if os.path.exists(pretrained_model_name_or_path):
# Path is local
if 'config' in kwargs:
print('Config is in the parameters')
config = kwargs['config']
else:
config = MegatronBertConfig.from_pretrained(pretrained_model_name_or_path, **kwargs)
base_model = MegatronBertModel(config=config)
model = cls(base_model=base_model)
model_path = os.path.join(pretrained_model_name_or_path, "pytorch_model.bin")
model.load_state_dict(torch.load(model_path, map_location=torch.device('cpu'), weights_only=True))
else:
# Path is from Hugging Face Hub
config = kwargs.pop('config', None)
if config is None:
config = MegatronBertConfig.from_pretrained(pretrained_model_name_or_path, **kwargs)
base_model = MegatronBertModel(config=config)
model = cls(base_model=base_model)
model_file = cached_file(pretrained_model_name_or_path, "pytorch_model.bin")
model.load_state_dict(torch.load(model_file, map_location=torch.device('cpu'), weights_only=True))
return model
class ProkBertConfig(MegatronBertConfig):
model_type = "prokbert"
def __init__(
self,
kmer: int = 6,
shift: int = 1,
num_labels: int = 2,
classification_dropout_rate: float = 0.1,
**kwargs,
):
super().__init__(**kwargs)
self.kmer = kmer
self.shift = shift
self.num_labels = num_labels
self.classification_dropout_rate = classification_dropout_rate
class ProkBertClassificationConfig(ProkBertConfig):
model_type = "prokbert"
def __init__(
self,
num_labels: int = 2,
classification_dropout_rate: float = 0.1,
**kwargs,
):
super().__init__(**kwargs)
# Ide jön majd némi extra lépés, egyelőre csak próbálkozunk a sima configgal.
self.num_labels = num_labels
self.classification_dropout_rate = classification_dropout_rate
class ProkBertPreTrainedModel(PreTrainedModel):
"""
An abstract class to handle weights initialization and a simple interface for downloading and loading pretrained
models.
"""
config_class = ProkBertConfig
base_model_prefix = "bert"
supports_gradient_checkpointing = True
def _init_weights(self, module):
"""Initialize the weights"""
if isinstance(module, (nn.Linear, nn.Embedding)):
# Slightly different from the TF version which uses truncated_normal for initialization
# cf https://github.com/pytorch/pytorch/pull/5617
module.weight.data.normal_(mean=0.0, std=self.config.initializer_range)
elif isinstance(module, nn.LayerNorm):
module.bias.data.zero_()
module.weight.data.fill_(1.0)
if isinstance(module, nn.Linear) and module.bias is not None:
module.bias.data.zero_()
class ProkBertModel(MegatronBertModel):
config_class = ProkBertConfig
def __init__(self, config: ProkBertConfig, **kwargs):
if not isinstance(config, ProkBertConfig):
raise ValueError(f"Expected `ProkBertConfig`, got {config.__class__.__module__}.{config.__class__.__name__}")
super().__init__(config, **kwargs)
self.config = config
# One should check if it is a prper prokbert config, if not crafting one.
class ProkBertForMaskedLM(MegatronBertForMaskedLM):
config_class = ProkBertConfig
def __init__(self, config: ProkBertConfig, **kwargs):
if not isinstance(config, ProkBertConfig):
raise ValueError(f"Expected `ProkBertConfig`, got {config.__class__.__module__}.{config.__class__.__name__}")
super().__init__(config, **kwargs)
self.config = config
# One should check if it is a prper prokbert config, if not crafting one.
class ProkBertForSequenceClassification(ProkBertPreTrainedModel):
config_class = ProkBertConfig
base_model_prefix = "bert"
def __init__(self, config):
super().__init__(config)
self.config = config
self.bert = ProkBertModel(config)
self.weighting_layer = nn.Linear(self.config.hidden_size, 1)
self.dropout = nn.Dropout(self.config.classification_dropout_rate)
self.classifier = nn.Linear(self.config.hidden_size, self.config.num_labels)
self.loss_fct = torch.nn.CrossEntropyLoss()
self.post_init()
def forward(
self,
input_ids: Optional[torch.LongTensor] = None,
attention_mask: Optional[torch.FloatTensor] = None,
token_type_ids: Optional[torch.LongTensor] = None,
position_ids: Optional[torch.LongTensor] = None,
head_mask: Optional[torch.FloatTensor] = None,
inputs_embeds: Optional[torch.FloatTensor] = None,
labels: Optional[torch.LongTensor] = None,
output_attentions: Optional[bool] = None,
output_hidden_states: Optional[bool] = None,
return_dict: Optional[bool] = None,
) -> Union[Tuple, SequenceClassifierOutput]:
r"""
labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*):
Labels for computing the sequence classification/regression loss. Indices should be in `[0, ...,
config.num_labels - 1]`. If `config.num_labels == 1` a regression loss is computed (Mean-Square loss), If
`config.num_labels > 1` a classification loss is computed (Cross-Entropy).
"""
return_dict = return_dict if return_dict is not None else self.config.use_return_dict
outputs = self.bert(
input_ids,
attention_mask=attention_mask,
token_type_ids=token_type_ids,
position_ids=position_ids,
head_mask=head_mask,
inputs_embeds=inputs_embeds,
output_attentions=output_attentions,
output_hidden_states=output_hidden_states,
return_dict=return_dict,
)
sequence_output = outputs[0]
# Compute weights for each position in the sequence
weights = self.weighting_layer(sequence_output)
weights = torch.nn.functional.softmax(weights, dim=1)
# Compute weighted sum
pooled_output = torch.sum(weights * sequence_output, dim=1)
# Classification head
pooled_output = self.dropout(pooled_output)
logits = self.classifier(pooled_output)
loss = None
if labels is not None:
loss = self.loss_fct(logits.view(-1, self.config.num_labels), labels.view(-1))
classification_output = SequenceClassifierOutput(
loss=loss,
logits=logits,
hidden_states=outputs.hidden_states,
attentions=outputs.attentions,
)
return classification_output
|