|
|
|
|
|
import torch.nn as nn |
|
from transformers import DebertaModel |
|
from config import DROPOUT_RATE, DEBERTA_MODEL_NAME |
|
|
|
class DebertaMultiOutputModel(nn.Module): |
|
""" |
|
DeBERTa-based model for multi-output classification. |
|
Similar structure to the BERT model, using a pre-trained DeBERTa model |
|
as the backbone for text feature extraction. |
|
""" |
|
|
|
tokenizer_name = DEBERTA_MODEL_NAME |
|
|
|
def __init__(self, num_labels): |
|
""" |
|
Initializes the DebertaMultiOutputModel. |
|
|
|
Args: |
|
num_labels (list): A list where each element is the number of classes |
|
for a corresponding label column. |
|
""" |
|
super(DebertaMultiOutputModel, self).__init__() |
|
|
|
|
|
self.deberta = DebertaModel.from_pretrained(DEBERTA_MODEL_NAME) |
|
self.dropout = nn.Dropout(DROPOUT_RATE) |
|
|
|
|
|
|
|
self.classifiers = nn.ModuleList([ |
|
nn.Linear(self.deberta.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. |
|
attention_mask (torch.Tensor): Tensor indicating attention. |
|
|
|
Returns: |
|
list: A list of logit tensors, one for each classification head. |
|
""" |
|
|
|
|
|
pooled_output = self.deberta(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] |