jefftherover commited on
Commit
b29c2e0
Β·
verified Β·
1 Parent(s): eceddee

Upload train_ner.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. train_ner.py +25 -16
train_ner.py CHANGED
@@ -88,19 +88,26 @@ def tokenize_and_align(examples):
88
  for tok_s, tok_e in offsets:
89
  if tok_s == tok_e:
90
  labels.append(-100); prev_end = None
91
- elif prev_end is None or tok_s > prev_end:
92
- # ModernBERT tokenizer includes the preceding space in the
93
- # token's offset (e.g. " Grey" β†’ offset starts at the space).
94
- # Advance past any leading spaces so we look up the actual
95
- # first character of the word in the char-label array.
96
  real_s = tok_s
97
  while real_s < tok_e and text[real_s] == ' ':
98
  real_s += 1
99
- lbl = cl[real_s] if real_s < len(cl) else "O"
100
- labels.append(label2id.get(lbl, label2id["O"]))
 
 
 
 
 
 
 
 
 
101
  prev_end = tok_e
102
- else:
103
- labels.append(-100); prev_end = tok_e
104
  all_labels.append(labels)
105
  enc.pop("offset_mapping")
106
  enc["labels"] = all_labels
@@ -140,15 +147,18 @@ model = AutoModelForTokenClassification.from_pretrained(
140
  )
141
 
142
  # 7. Trackio
143
- trackio.init(project="modernbert-pii-ner", name="modernbert-pii-ner-43k-v4")
144
 
145
  # ── 8. Training args ─────────────────────────────────────────────────────────
146
- # v4: alignment bug fixed (space-stripped char-label lookup).
147
- # Hyperparams revert to v2 best (5 epochs, no label smoothing β€” v3's
148
- # label_smoothing_factor=0.1 hurt precision more than it helped recall).
 
 
 
149
  args = TrainingArguments(
150
  output_dir=OUTPUT_DIR,
151
- num_train_epochs=5, # v2 peaked at epoch 4.09; give full budget
152
  per_device_train_batch_size=16,
153
  per_device_eval_batch_size=32,
154
  gradient_accumulation_steps=2, # effective batch = 32
@@ -156,7 +166,6 @@ args = TrainingArguments(
156
  weight_decay=0.01,
157
  warmup_ratio=0.2,
158
  lr_scheduler_type="cosine_with_restarts",
159
- # label_smoothing_factor removed β€” hurt v3 precision by -1.93%
160
  eval_strategy="steps",
161
  eval_steps=500,
162
  save_strategy="steps",
@@ -169,7 +178,7 @@ args = TrainingArguments(
169
  hub_model_id=HUB_MODEL_ID,
170
  hub_strategy="every_save",
171
  report_to="trackio",
172
- run_name="modernbert-pii-ner-43k-v4",
173
  fp16=True,
174
  logging_steps=100,
175
  dataloader_num_workers=2,
 
88
  for tok_s, tok_e in offsets:
89
  if tok_s == tok_e:
90
  labels.append(-100); prev_end = None
91
+ else:
92
+ # ModernBERT (RoBERTa-style) tokenizer absorbs the preceding
93
+ # space into the next token's offset (e.g. " Grey" has ts at
94
+ # the space, not at 'G'). Strip leading spaces to find the
95
+ # true first character of the word.
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 this is the very first token
100
+ # (prev_end is None) OR the token had a leading space stripped
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
+ labels.append(-100)
110
  prev_end = tok_e
 
 
111
  all_labels.append(labels)
112
  enc.pop("offset_mapping")
113
  enc["labels"] = all_labels
 
147
  )
148
 
149
  # 7. Trackio
150
+ trackio.init(project="modernbert-pii-ner", name="modernbert-pii-ner-43k-v5")
151
 
152
  # ── 8. Training args ─────────────────────────────────────────────────────────
153
+ # v5: word-boundary detection fixed (real_s > tok_s instead of tok_s > prev_end).
154
+ # The old check never fired for RoBERTa-style tokenizers where spaces are
155
+ # absorbed into the next token's offset β€” only the first token per sentence
156
+ # was ever labeled (effectively 1 label per example). With the fix every
157
+ # word gets labeled, giving the model a proper training signal for in-sentence
158
+ # and multi-word entities.
159
  args = TrainingArguments(
160
  output_dir=OUTPUT_DIR,
161
+ num_train_epochs=5,
162
  per_device_train_batch_size=16,
163
  per_device_eval_batch_size=32,
164
  gradient_accumulation_steps=2, # effective batch = 32
 
166
  weight_decay=0.01,
167
  warmup_ratio=0.2,
168
  lr_scheduler_type="cosine_with_restarts",
 
169
  eval_strategy="steps",
170
  eval_steps=500,
171
  save_strategy="steps",
 
178
  hub_model_id=HUB_MODEL_ID,
179
  hub_strategy="every_save",
180
  report_to="trackio",
181
+ run_name="modernbert-pii-ner-43k-v5",
182
  fp16=True,
183
  logging_steps=100,
184
  dataloader_num_workers=2,