Upload train_ner.py with huggingface_hub
Browse files- train_ner.py +21 -15
train_ner.py
CHANGED
|
@@ -96,17 +96,23 @@ def tokenize_and_align(examples):
|
|
| 96 |
real_s = tok_s
|
| 97 |
while real_s < tok_e and text[real_s] == ' ':
|
| 98 |
real_s += 1
|
| 99 |
-
# Word-boundary rule: new word if
|
| 100 |
-
#
|
| 101 |
-
# (real_s > tok_s). The old check (tok_s > prev_end) was wrong
|
| 102 |
-
# for RoBERTa tokenizers: spaces are inside the token so there
|
| 103 |
-
# is never a gap between consecutive tokens, meaning only the
|
| 104 |
-
# very first token per sentence was ever labeled.
|
| 105 |
if prev_end is None or real_s > tok_s:
|
|
|
|
| 106 |
lbl = cl[real_s] if real_s < len(cl) else "O"
|
| 107 |
labels.append(label2id.get(lbl, label2id["O"]))
|
| 108 |
else:
|
| 109 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
prev_end = tok_e
|
| 111 |
all_labels.append(labels)
|
| 112 |
enc.pop("offset_mapping")
|
|
@@ -147,15 +153,15 @@ model = AutoModelForTokenClassification.from_pretrained(
|
|
| 147 |
)
|
| 148 |
|
| 149 |
# 7. Trackio
|
| 150 |
-
trackio.init(project="modernbert-pii-ner", name="modernbert-pii-ner-43k-
|
| 151 |
|
| 152 |
# ββ 8. Training args βββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 153 |
-
#
|
| 154 |
-
#
|
| 155 |
-
#
|
| 156 |
-
#
|
| 157 |
-
#
|
| 158 |
-
#
|
| 159 |
args = TrainingArguments(
|
| 160 |
output_dir=OUTPUT_DIR,
|
| 161 |
num_train_epochs=5,
|
|
@@ -178,7 +184,7 @@ args = TrainingArguments(
|
|
| 178 |
hub_model_id=HUB_MODEL_ID,
|
| 179 |
hub_strategy="every_save",
|
| 180 |
report_to="trackio",
|
| 181 |
-
run_name="modernbert-pii-ner-43k-
|
| 182 |
fp16=True,
|
| 183 |
logging_steps=100,
|
| 184 |
dataloader_num_workers=2,
|
|
|
|
| 96 |
real_s = tok_s
|
| 97 |
while real_s < tok_e and text[real_s] == ' ':
|
| 98 |
real_s += 1
|
| 99 |
+
# Word-boundary rule: new word if first token (prev_end None)
|
| 100 |
+
# OR token had a leading space stripped (real_s > tok_s).
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
if prev_end is None or real_s > tok_s:
|
| 102 |
+
# Word-start: assign label from char array (B-, I-, or O).
|
| 103 |
lbl = cl[real_s] if real_s < len(cl) else "O"
|
| 104 |
labels.append(label2id.get(lbl, label2id["O"]))
|
| 105 |
else:
|
| 106 |
+
# Subword continuation (no leading space, within the same
|
| 107 |
+
# word as the previous token). v6: if this subword falls
|
| 108 |
+
# inside an entity span give it I-<type> so the model
|
| 109 |
+
# learns to sustain entity spans across subword boundaries.
|
| 110 |
+
# Non-entity subword continuations keep -100 (ignored).
|
| 111 |
+
lbl = cl[real_s] if real_s < len(cl) else "O"
|
| 112 |
+
if lbl != "O":
|
| 113 |
+
labels.append(label2id.get(f"I-{lbl[2:]}", label2id["O"]))
|
| 114 |
+
else:
|
| 115 |
+
labels.append(-100)
|
| 116 |
prev_end = tok_e
|
| 117 |
all_labels.append(labels)
|
| 118 |
enc.pop("offset_mapping")
|
|
|
|
| 153 |
)
|
| 154 |
|
| 155 |
# 7. Trackio
|
| 156 |
+
trackio.init(project="modernbert-pii-ner", name="modernbert-pii-ner-43k-v6")
|
| 157 |
|
| 158 |
# ββ 8. Training args βββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 159 |
+
# v6: subword continuation labeling fixed.
|
| 160 |
+
# Previously only the first subword of each word was labeled; within-word
|
| 161 |
+
# continuations got -100. Now entity subword continuations receive I-<type>,
|
| 162 |
+
# so the model learns to sustain entity spans across subword boundaries.
|
| 163 |
+
# This directly targets the BOUNDARY fragmentation errors seen in v5 results
|
| 164 |
+
# (e.g. "Hadley_Larson" β Had/ley/_/Larson each emitting B- instead of one span).
|
| 165 |
args = TrainingArguments(
|
| 166 |
output_dir=OUTPUT_DIR,
|
| 167 |
num_train_epochs=5,
|
|
|
|
| 184 |
hub_model_id=HUB_MODEL_ID,
|
| 185 |
hub_strategy="every_save",
|
| 186 |
report_to="trackio",
|
| 187 |
+
run_name="modernbert-pii-ner-43k-v6",
|
| 188 |
fp16=True,
|
| 189 |
logging_steps=100,
|
| 190 |
dataloader_num_workers=2,
|