File size: 1,130 Bytes
bcfaedb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import torch
import torch.nn as nn
from transformers import AutoModel, AutoConfig

# CLASSIFIER (original - used by all models EXCEPT SDG)
class Classifier(nn.Module):
    def __init__(self, model_name, num_labels, dropout_rate):
        super().__init__()

        self.encoder = AutoModel.from_pretrained(model_name)
        config = AutoConfig.from_pretrained(model_name)

        self.cls_size = config.hidden_size
        self.dropout = nn.Dropout(dropout_rate)
        self.fc = nn.Linear(self.cls_size, num_labels)

    def forward(self, input_ids, attention_mask):
        outputs = self.encoder(
            input_ids=input_ids,
            attention_mask=attention_mask
        )

        token_embeddings = outputs.last_hidden_state
        input_mask_expanded = attention_mask.unsqueeze(-1).expand(token_embeddings.size()).float()

        sum_embeddings = torch.sum(token_embeddings * input_mask_expanded, dim=1)
        sum_mask = torch.clamp(input_mask_expanded.sum(dim=1), min=1e-9)
        cls = sum_embeddings / sum_mask

        cls = self.dropout(cls)
        logits = self.fc(cls)

        return logits, cls