Xaver Maria Krückl commited on
Upload folder using huggingface_hub
Browse files- config.json +20 -0
- model.safetensors +3 -0
- modeling_xlmedu.py +91 -0
- run_config.json +16 -0
config.json
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"architectures": [
|
| 3 |
+
"XLMEduModelHF"
|
| 4 |
+
],
|
| 5 |
+
"encoder_name": "FacebookAI/xlm-roberta-large",
|
| 6 |
+
"id2label": {
|
| 7 |
+
"0": "B-EDU",
|
| 8 |
+
"1": "I-EDU",
|
| 9 |
+
"2": "O"
|
| 10 |
+
},
|
| 11 |
+
"label2id": {
|
| 12 |
+
"B-EDU": 0,
|
| 13 |
+
"I-EDU": 1,
|
| 14 |
+
"O": 2
|
| 15 |
+
},
|
| 16 |
+
"model_type": "xlm_edu",
|
| 17 |
+
"num_tags": 3,
|
| 18 |
+
"torch_dtype": "float32",
|
| 19 |
+
"transformers_version": "4.40.2"
|
| 20 |
+
}
|
model.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:86fb9cf8cad3bf2455dba40d0e20ac686459c52a67d9748f1e3bde9b254809d8
|
| 3 |
+
size 2239623056
|
modeling_xlmedu.py
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
HuggingFace-compatible wrapper for XLMEduModel.
|
| 3 |
+
|
| 4 |
+
Architecture: XLM-RoBERTa-large encoder → linear classifier → CRF (torchcrf)
|
| 5 |
+
Task: BIO token classification for situation-entity segmentation (B-EDU / I-EDU / O)
|
| 6 |
+
|
| 7 |
+
Loading from the Hub:
|
| 8 |
+
from modeling_xlmedu import XLMEduConfig, XLMEduModelHF
|
| 9 |
+
config = XLMEduConfig.from_pretrained("your-username/your-repo")
|
| 10 |
+
model = XLMEduModelHF.from_pretrained("your-username/your-repo", config=config)
|
| 11 |
+
|
| 12 |
+
Inference:
|
| 13 |
+
from transformers import AutoTokenizer
|
| 14 |
+
tokenizer = AutoTokenizer.from_pretrained("FacebookAI/xlm-roberta-large")
|
| 15 |
+
inputs = tokenizer("Hello world.", return_tensors="pt")
|
| 16 |
+
tag_ids = model.predict(inputs["input_ids"], inputs["attention_mask"])
|
| 17 |
+
tags = [model.config.id2label[i] for i in tag_ids[0]]
|
| 18 |
+
"""
|
| 19 |
+
|
| 20 |
+
from __future__ import annotations
|
| 21 |
+
|
| 22 |
+
from typing import Dict, List, Optional
|
| 23 |
+
|
| 24 |
+
import torch
|
| 25 |
+
from torchcrf import CRF
|
| 26 |
+
from transformers import AutoModel, PretrainedConfig, PreTrainedModel
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
LABELS = {"B-EDU": 0, "I-EDU": 1, "O": 2}
|
| 30 |
+
BIO_TAGS = [tag for tag, _ in sorted(LABELS.items(), key=lambda x: x[1])]
|
| 31 |
+
NUM_TAGS = len(BIO_TAGS)
|
| 32 |
+
O_IDX = LABELS["O"]
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
class XLMEduConfig(PretrainedConfig):
|
| 36 |
+
model_type = "xlm_edu"
|
| 37 |
+
|
| 38 |
+
def __init__(
|
| 39 |
+
self,
|
| 40 |
+
encoder_name: str = "FacebookAI/xlm-roberta-large",
|
| 41 |
+
num_tags: int = NUM_TAGS,
|
| 42 |
+
label2id: Optional[Dict[str, int]] = None,
|
| 43 |
+
id2label: Optional[Dict[int, str]] = None,
|
| 44 |
+
**kwargs,
|
| 45 |
+
):
|
| 46 |
+
super().__init__(**kwargs)
|
| 47 |
+
self.encoder_name = encoder_name
|
| 48 |
+
self.num_tags = num_tags
|
| 49 |
+
self.label2id = label2id or LABELS
|
| 50 |
+
self.id2label = id2label or {v: k for k, v in LABELS.items()}
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
class XLMEduModelHF(PreTrainedModel):
|
| 54 |
+
config_class = XLMEduConfig
|
| 55 |
+
|
| 56 |
+
def __init__(self, config: XLMEduConfig):
|
| 57 |
+
super().__init__(config)
|
| 58 |
+
self.encoder = AutoModel.from_pretrained(config.encoder_name, output_hidden_states=False)
|
| 59 |
+
self.classifier = torch.nn.Linear(self.encoder.config.hidden_size, config.num_tags)
|
| 60 |
+
self.crf = CRF(config.num_tags, batch_first=True)
|
| 61 |
+
|
| 62 |
+
def forward(
|
| 63 |
+
self,
|
| 64 |
+
input_ids: torch.Tensor,
|
| 65 |
+
attention_mask: torch.Tensor,
|
| 66 |
+
labels: Optional[torch.Tensor] = None,
|
| 67 |
+
):
|
| 68 |
+
"""
|
| 69 |
+
Returns a dict with 'loss' (if labels given) and 'logits' (emission scores).
|
| 70 |
+
logits shape: (batch, seq_len, num_tags)
|
| 71 |
+
"""
|
| 72 |
+
outputs = self.encoder(input_ids=input_ids, attention_mask=attention_mask)
|
| 73 |
+
emissions = self.classifier(outputs.last_hidden_state)
|
| 74 |
+
|
| 75 |
+
loss = None
|
| 76 |
+
if labels is not None:
|
| 77 |
+
crf_mask = attention_mask.bool()
|
| 78 |
+
labels_crf = labels.clone()
|
| 79 |
+
labels_crf[labels_crf == -100] = O_IDX
|
| 80 |
+
loss = -self.crf(emissions.float(), labels_crf, mask=crf_mask, reduction="mean")
|
| 81 |
+
|
| 82 |
+
return {"loss": loss, "logits": emissions}
|
| 83 |
+
|
| 84 |
+
def predict(
|
| 85 |
+
self,
|
| 86 |
+
input_ids: torch.Tensor,
|
| 87 |
+
attention_mask: torch.Tensor,
|
| 88 |
+
) -> List[List[int]]:
|
| 89 |
+
"""Viterbi-decode the best tag sequence for each sample in the batch."""
|
| 90 |
+
emissions = self.forward(input_ids, attention_mask)["logits"]
|
| 91 |
+
return self.crf.decode(emissions.float(), mask=attention_mask.bool())
|
run_config.json
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"data_dir": "/hnvme/workspace/v110ee23-edu_segmentation/sitentsegm/data/processed_sentences/xlm-roberta-large",
|
| 3 |
+
"output_dir": "/hnvme/workspace/v110ee23-edu_segmentation/sitentsegm/results/experiment_314223/xlm-roberta-large_lr4e-5_ep20_wd0.005_se84",
|
| 4 |
+
"model": "FacebookAI/xlm-roberta-large",
|
| 5 |
+
"epochs": 20,
|
| 6 |
+
"batch_size": 64,
|
| 7 |
+
"lr": 4e-05,
|
| 8 |
+
"weight_decay": 0.005,
|
| 9 |
+
"seed": 84,
|
| 10 |
+
"patience": 3,
|
| 11 |
+
"fp16": true,
|
| 12 |
+
"grad_accum": 1,
|
| 13 |
+
"local_rank": -1,
|
| 14 |
+
"model_slug": "xlm-roberta-large",
|
| 15 |
+
"model_resolved": "/hnvme/workspace/v110ee23-edu_segmentation/.cache/hub/models--FacebookAI--xlm-roberta-large/snapshots/c23d21b0620b635a76227c604d44e43a9f0ee389"
|
| 16 |
+
}
|