Upload 3 files
Browse files- config.json +20 -0
- model.safetensors +3 -0
- modeling_cap.py +38 -0
config.json
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"architectures": [
|
| 3 |
+
"CAPModel"
|
| 4 |
+
],
|
| 5 |
+
"base_model_name": "GroNLP/hateBERT",
|
| 6 |
+
"dropout": 0.1,
|
| 7 |
+
"dtype": "float32",
|
| 8 |
+
"id2label": {
|
| 9 |
+
"0": "LABEL_0",
|
| 10 |
+
"1": "LABEL_1",
|
| 11 |
+
"2": "LABEL_2"
|
| 12 |
+
},
|
| 13 |
+
"label2id": {
|
| 14 |
+
"LABEL_0": 0,
|
| 15 |
+
"LABEL_1": 1,
|
| 16 |
+
"LABEL_2": 2
|
| 17 |
+
},
|
| 18 |
+
"model_type": "cap",
|
| 19 |
+
"transformers_version": "5.14.1"
|
| 20 |
+
}
|
model.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:bca3a92057ad1958f6f42d300e9760b4f429253cd30dc7b402ba532179341a43
|
| 3 |
+
size 437962508
|
modeling_cap.py
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import torch.nn as nn
|
| 3 |
+
from transformers import AutoModel, AutoConfig, PreTrainedModel, PretrainedConfig
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
class CAPConfig(PretrainedConfig):
|
| 7 |
+
model_type = "cap"
|
| 8 |
+
|
| 9 |
+
def __init__(self, base_model_name="roberta-base", num_labels=3, dropout=0.1, **kwargs):
|
| 10 |
+
super().__init__(**kwargs)
|
| 11 |
+
self.base_model_name = base_model_name
|
| 12 |
+
self.num_labels = num_labels
|
| 13 |
+
self.dropout = dropout
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
class CAPModel(PreTrainedModel):
|
| 17 |
+
config_class = CAPConfig
|
| 18 |
+
base_model_prefix = "backbone"
|
| 19 |
+
|
| 20 |
+
def __init__(self, config):
|
| 21 |
+
super().__init__(config)
|
| 22 |
+
backbone_config = AutoConfig.from_pretrained(config.base_model_name)
|
| 23 |
+
self.backbone = AutoModel.from_config(backbone_config)
|
| 24 |
+
hidden_size = self.backbone.config.hidden_size
|
| 25 |
+
self.num_labels = config.num_labels
|
| 26 |
+
self.dropout = nn.Dropout(config.dropout)
|
| 27 |
+
self.head = nn.Linear(hidden_size, config.num_labels)
|
| 28 |
+
self.post_init()
|
| 29 |
+
|
| 30 |
+
def forward(self, input_ids, attention_mask, token_valid_mask):
|
| 31 |
+
outputs = self.backbone(input_ids=input_ids, attention_mask=attention_mask)
|
| 32 |
+
subword_states = self.dropout(outputs.last_hidden_state)
|
| 33 |
+
token_logits = self.head(subword_states)
|
| 34 |
+
valid_mask = token_valid_mask.unsqueeze(-1)
|
| 35 |
+
masked_token_logits = token_logits * valid_mask
|
| 36 |
+
valid_counts = token_valid_mask.sum(dim=1, keepdim=True).clamp(min=1e-9)
|
| 37 |
+
sequence_logits = masked_token_logits.sum(dim=1) / valid_counts
|
| 38 |
+
return sequence_logits, token_logits
|