|
|
|
|
|
import torch |
|
import torch.nn as nn |
|
from transformers import BertModel |
|
from config import DROPOUT_RATE, BERT_MODEL_NAME |
|
|
|
class BertMultiOutputModel(nn.Module): |
|
""" |
|
BERT-based model for multi-output classification. |
|
It uses a pre-trained BERT model as its backbone and adds a dropout layer |
|
followed by separate linear classification heads for each target label. |
|
""" |
|
|
|
tokenizer_name = BERT_MODEL_NAME |
|
|
|
def __init__(self, num_labels): |
|
""" |
|
Initializes the BertMultiOutputModel. |
|
|
|
Args: |
|
num_labels (list): A list where each element is the number of classes |
|
for a corresponding label column. |
|
""" |
|
super(BertMultiOutputModel, self).__init__() |
|
|
|
|
|
self.bert = BertModel.from_pretrained(BERT_MODEL_NAME) |
|
self.dropout = nn.Dropout(DROPOUT_RATE) |
|
|
|
|
|
|
|
self.classifiers = nn.ModuleList([ |
|
nn.Linear(self.bert.config.hidden_size, n_classes) for n_classes in num_labels |
|
]) |
|
|
|
def forward(self, input_ids, attention_mask): |
|
""" |
|
Performs the forward pass of the model. |
|
|
|
Args: |
|
input_ids (torch.Tensor): Tensor of token IDs (from tokenizer). |
|
attention_mask (torch.Tensor): Tensor indicating attention (from tokenizer). |
|
|
|
Returns: |
|
list: A list of logit tensors, one for each classification head. |
|
Each tensor has shape (batch_size, num_classes_for_that_label). |
|
""" |
|
|
|
|
|
|
|
pooled_output = self.bert(input_ids=input_ids, attention_mask=attention_mask).pooler_output |
|
|
|
|
|
pooled_output = self.dropout(pooled_output) |
|
|
|
|
|
|
|
return [classifier(pooled_output) for classifier in self.classifiers] |