Spaces:
Runtime error
Runtime error
Create models/deberta_model.py
Browse files- models/deberta_model.py +22 -0
models/deberta_model.py
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import torch
|
| 3 |
+
import torch.nn as nn
|
| 4 |
+
from transformers import DebertaModel
|
| 5 |
+
from config import DROPOUT_RATE, DEBERTA_MODEL_NAME
|
| 6 |
+
|
| 7 |
+
class DebertaMultiOutputModel(nn.Module):
|
| 8 |
+
tokenizer_name = DEBERTA_MODEL_NAME
|
| 9 |
+
|
| 10 |
+
def __init__(self, num_labels):
|
| 11 |
+
super(DebertaMultiOutputModel, self).__init__()
|
| 12 |
+
self.deberta = DebertaModel.from_pretrained(DEBERTA_MODEL_NAME)
|
| 13 |
+
self.dropout = nn.Dropout(DROPOUT_RATE)
|
| 14 |
+
self.classifiers = nn.ModuleList([
|
| 15 |
+
nn.Linear(self.deberta.config.hidden_size, n_classes) for n_classes in num_labels
|
| 16 |
+
])
|
| 17 |
+
|
| 18 |
+
def forward(self, input_ids, attention_mask):
|
| 19 |
+
last_hidden_state = self.deberta(input_ids=input_ids, attention_mask=attention_mask).last_hidden_state
|
| 20 |
+
pooled_output = last_hidden_state[:, 0] # [CLS] token representation
|
| 21 |
+
pooled_output = self.dropout(pooled_output)
|
| 22 |
+
return [classifier(pooled_output) for classifier in self.classifiers]
|