File size: 876 Bytes
7a1a3c1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

import torch
import torch.nn as nn
from transformers import DebertaModel
from config import DROPOUT_RATE, DEBERTA_MODEL_NAME

class DebertaMultiOutputModel(nn.Module):
    def __init__(self, num_labels):
        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):
        outputs = self.deberta(input_ids=input_ids, attention_mask=attention_mask)
        pooled_output = outputs.last_hidden_state[:, 0, :]  # Use CLS token output
        pooled_output = self.dropout(pooled_output)
        return [classifier(pooled_output) for classifier in self.classifiers]