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

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

class DebertaMultiOutputModel(nn.Module):
    tokenizer_name = DEBERTA_MODEL_NAME

    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):
        last_hidden_state = self.deberta(input_ids=input_ids, attention_mask=attention_mask).last_hidden_state
        pooled_output = last_hidden_state[:, 0]  # [CLS] token representation
        pooled_output = self.dropout(pooled_output)
        return [classifier(pooled_output) for classifier in self.classifiers]