binga commited on
Commit
bbed34a
Β·
verified Β·
1 Parent(s): 916d062

Update README with head-only doc clf results (test_acc=0.478)

Browse files
Files changed (1) hide show
  1. README.md +113 -45
README.md CHANGED
@@ -19,108 +19,176 @@ model-index:
19
  results:
20
  - task:
21
  type: token-classification
22
- name: PII Detection
23
  dataset:
24
  name: ai4privacy/pii-masking-400k
25
  type: ai4privacy/pii-masking-400k
26
  metrics:
27
  - type: f1
28
  value: 0.4925
 
29
  - type: precision
30
  value: 0.6968
31
  - type: recall
32
  value: 0.3809
33
  - task:
34
  type: text-classification
35
- name: Document Classification
36
  dataset:
37
  name: yahoo_answers_topics
38
  type: community-datasets/yahoo_answers_topics
39
  metrics:
40
  - type: accuracy
41
- value: 0.2482
 
42
  ---
43
 
44
  # Privacy Filter Multi-Task πŸ”’πŸ“„
45
 
46
  A **single model** for simultaneous **PII Detection (NER)** and **Document Classification (10 categories)**.
47
 
48
- Adapted from [openai/privacy-filter](https://huggingface.co/openai/privacy-filter) (1.4B Sparse MoE, ~50M active params/token).
49
 
50
  ## Architecture
51
 
52
  ```
53
- Input β†’ BPE Tokenizer (200K vocab)
54
  ↓
55
- 8-layer Sparse MoE Transformer (128 experts, top-4)
56
- ↓ ↓
57
- NER Head (640β†’33) Doc Head (mean-pool β†’ 640β†’10)
58
- ↓ ↓
59
- BIOES PII tags 10 categories
 
 
 
 
60
  ```
61
 
62
  ## Results
63
 
64
- | Task | Metric | Value |
65
- |------|--------|-------|
66
- | PII NER | F1 (strict, span) | **0.493** |
67
- | PII NER | Precision | 0.697 |
68
- | PII NER | Recall | 0.381 |
69
- | PII NER | Token Accuracy | 0.944 |
70
- | Doc Clf | Val Accuracy | 0.255 |
71
- | Doc Clf | Test Accuracy | **0.248** |
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
72
 
73
  ### Inference Speed
 
74
  | Device | Latency |
75
  |--------|---------|
76
- | GPU A10G (bf16) | 178 ms |
77
- | CPU (fp32) | 202 ms |
 
 
 
78
 
79
- ## PII Entity Types
80
- `private_person` β€’ `private_email` β€’ `private_phone` β€’ `private_address` β€’ `private_date` β€’ `private_url` β€’ `account_number` β€’ `secret`
81
 
82
- ## Document Categories
83
- Society & Culture β€’ Science & Math β€’ Health β€’ Education β€’ Computers & Internet β€’ Sports β€’ Business & Finance β€’ Entertainment β€’ Family β€’ Politics
 
 
84
 
85
  ## Usage
86
 
87
  ```python
88
- import torch, torch.nn as nn
 
89
  from transformers import AutoModelForTokenClassification, AutoTokenizer
90
  from huggingface_hub import hf_hub_download
91
 
 
92
  tokenizer = AutoTokenizer.from_pretrained("binga/privacy-filter-multitask")
93
  model = AutoModelForTokenClassification.from_pretrained(
94
  "binga/privacy-filter-multitask", dtype=torch.bfloat16, device_map="auto"
95
  )
 
 
96
  doc_head = nn.Linear(640, 10)
97
  doc_head.load_state_dict(torch.load(
98
  hf_hub_download("binga/privacy-filter-multitask", "doc_head.pt"),
99
  weights_only=True, map_location=model.device
100
  ))
101
  doc_head = doc_head.to(dtype=torch.bfloat16, device=model.device)
 
102
 
103
- text = "John Smith (SSN: 123-45-6789) emailed john@corp.com"
 
104
  inputs = tokenizer(text, return_tensors="pt").to(model.device)
 
105
  with torch.no_grad():
106
- out = model(**inputs, output_hidden_states=True)
107
-
108
- # PII
109
- for t, p in zip(tokenizer.convert_ids_to_tokens(inputs["input_ids"][0]),
110
- out.logits.argmax(-1)[0]):
111
- label = model.config.id2label[p.item()]
112
- if label != "O": print(f" {t} β†’ {label}")
113
-
114
- # Doc class
115
- cats = ["Society", "Science", "Health", "Education", "Computers",
116
- "Sports", "Business", "Entertainment", "Family", "Politics"]
117
- h = out.hidden_states[-1]
118
- m = inputs["attention_mask"].unsqueeze(-1).to(h.dtype)
119
- pooled = (h * m).sum(1) / m.sum(1).clamp(min=1)
120
- print(f"Category: {cats[doc_head(pooled).argmax().item()]}")
 
 
 
 
 
 
 
 
 
121
  ```
122
 
123
- ## Training
124
- - Partial fine-tune: last 4/8 MoE layers + heads (636M/1.4B trainable)
125
- - NER: ai4privacy/pii-masking-400k (20K en), Doc: yahoo_answers_topics (20K)
126
- - Loss: NERΓ—1.0 + DocΓ—0.5, AdamW LR=2e-5, cosine, 2 epochs, BS=16
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
  results:
20
  - task:
21
  type: token-classification
22
+ name: PII Detection (NER)
23
  dataset:
24
  name: ai4privacy/pii-masking-400k
25
  type: ai4privacy/pii-masking-400k
26
  metrics:
27
  - type: f1
28
  value: 0.4925
29
+ name: F1 (strict span-level)
30
  - type: precision
31
  value: 0.6968
32
  - type: recall
33
  value: 0.3809
34
  - task:
35
  type: text-classification
36
+ name: Document Classification (10 classes)
37
  dataset:
38
  name: yahoo_answers_topics
39
  type: community-datasets/yahoo_answers_topics
40
  metrics:
41
  - type: accuracy
42
+ value: 0.4776
43
+ name: Test Accuracy
44
  ---
45
 
46
  # Privacy Filter Multi-Task πŸ”’πŸ“„
47
 
48
  A **single model** for simultaneous **PII Detection (NER)** and **Document Classification (10 categories)**.
49
 
50
+ Adapted from [openai/privacy-filter](https://huggingface.co/openai/privacy-filter) β€” a 1.4B Sparse MoE transformer with only ~50M active parameters per token.
51
 
52
  ## Architecture
53
 
54
  ```
55
+ Input β†’ BPE Tokenizer (o200k_base, 200K vocab)
56
  ↓
57
+ 8-layer Sparse MoE Transformer
58
+ β€’ 128 experts, top-4 routing (~50M active params/token)
59
+ β€’ Banded sliding-window attention (window=128)
60
+ β€’ GQA: 14 query heads, 2 KV heads, head_dim=64
61
+ β€’ Hidden size: 640
62
+ ↓ ↓
63
+ NER Head (640β†’33) Doc Head (mean-pool β†’ 640β†’10)
64
+ ↓ ↓
65
+ BIOES PII tags 10-class document category
66
  ```
67
 
68
  ## Results
69
 
70
+ ### PII Detection (NER)
71
+
72
+ | Metric | Value |
73
+ |--------|-------|
74
+ | **F1 (strict span-level)** | **0.493** |
75
+ | Precision | 0.697 |
76
+ | Recall | 0.381 |
77
+ | Token Accuracy | 0.944 |
78
+
79
+ 8 entity types: `private_person` Β· `private_email` Β· `private_phone` Β· `private_address` Β· `private_date` Β· `private_url` Β· `account_number` Β· `secret`
80
+
81
+ ### Document Classification (10 classes)
82
+
83
+ | Split | Accuracy |
84
+ |-------|----------|
85
+ | Val | 0.470 |
86
+ | **Test** | **0.478** |
87
+
88
+ Per-class test accuracy:
89
+
90
+ | Category | Accuracy |
91
+ |----------|----------|
92
+ | Computers & Internet | 0.688 |
93
+ | Family & Relationships | 0.615 |
94
+ | Science & Mathematics | 0.556 |
95
+ | Health | 0.524 |
96
+ | Sports | 0.523 |
97
+ | Politics & Government | 0.493 |
98
+ | Entertainment & Music | 0.444 |
99
+ | Society & Culture | 0.363 |
100
+ | Education & Reference | 0.310 |
101
+ | Business & Finance | 0.263 |
102
 
103
  ### Inference Speed
104
+
105
  | Device | Latency |
106
  |--------|---------|
107
+ | **GPU (A10G, bf16)** | **~154 ms/sample** |
108
+
109
+ ## Training Strategy
110
+
111
+ Two-phase training approach:
112
 
113
+ 1. **Phase 1 β€” Multi-task fine-tuning**: Partially unfroze last 4 MoE layers + both task heads. Trained on 20K NER examples (ai4privacy) + 20K doc examples (Yahoo Answers). Multi-task loss (NERΓ—1.0 + DocΓ—0.5). 2 epochs, LR=2e-5.
 
114
 
115
+ 2. **Phase 2 β€” Doc head retraining** (head-only): Froze entire backbone + NER head. Pre-computed 640-dim pooled features for 100K Yahoo Answers examples. Trained fresh `Linear(640β†’10)` classifier for 10 epochs, LR=1e-3, cosine decay. This approach:
116
+ - Preserves NER performance exactly (backbone untouched)
117
+ - Is extremely fast (~seconds per epoch on cached features)
118
+ - Achieves **47.8% test accuracy** (up from 24.8% in phase 1)
119
 
120
  ## Usage
121
 
122
  ```python
123
+ import torch
124
+ import torch.nn as nn
125
  from transformers import AutoModelForTokenClassification, AutoTokenizer
126
  from huggingface_hub import hf_hub_download
127
 
128
+ # Load model + tokenizer
129
  tokenizer = AutoTokenizer.from_pretrained("binga/privacy-filter-multitask")
130
  model = AutoModelForTokenClassification.from_pretrained(
131
  "binga/privacy-filter-multitask", dtype=torch.bfloat16, device_map="auto"
132
  )
133
+
134
+ # Load document classification head
135
  doc_head = nn.Linear(640, 10)
136
  doc_head.load_state_dict(torch.load(
137
  hf_hub_download("binga/privacy-filter-multitask", "doc_head.pt"),
138
  weights_only=True, map_location=model.device
139
  ))
140
  doc_head = doc_head.to(dtype=torch.bfloat16, device=model.device)
141
+ doc_head.eval()
142
 
143
+ # Inference
144
+ text = "John Smith (SSN: 123-45-6789) emailed john@corp.com about Q3 earnings."
145
  inputs = tokenizer(text, return_tensors="pt").to(model.device)
146
+
147
  with torch.no_grad():
148
+ outputs = model(**inputs, output_hidden_states=True)
149
+
150
+ # === PII Detection ===
151
+ print("PII entities:")
152
+ for tok, pred in zip(
153
+ tokenizer.convert_ids_to_tokens(inputs["input_ids"][0]),
154
+ outputs.logits.argmax(-1)[0]
155
+ ):
156
+ label = model.config.id2label[pred.item()]
157
+ if label != "O":
158
+ print(f" {tok} β†’ {label}")
159
+
160
+ # === Document Classification ===
161
+ categories = [
162
+ "Society & Culture", "Science & Math", "Health", "Education",
163
+ "Computers & Internet", "Sports", "Business & Finance",
164
+ "Entertainment", "Family", "Politics"
165
+ ]
166
+ hidden = outputs.hidden_states[-1]
167
+ mask = inputs["attention_mask"].unsqueeze(-1).to(hidden.dtype)
168
+ pooled = (hidden * mask).sum(1) / mask.sum(1).clamp(min=1)
169
+ probs = torch.softmax(doc_head(pooled)[0].float(), dim=-1)
170
+ top = probs.argmax().item()
171
+ print(f"\nCategory: {categories[top]} ({probs[top]:.1%})")
172
  ```
173
 
174
+ ## Example Outputs
175
+
176
+ | Input | PII Detected | Category (confidence) |
177
+ |-------|-------------|----------------------|
178
+ | "My name is John Smith... email john@example.com" | βœ… John Smith, john@example.com, 123 Main St | Computers & Internet (56%) |
179
+ | "Liverpool FC defeated Manchester City 3-1" | ❌ None | **Sports (98%)** |
180
+ | "Federal Reserve announced a rate cut" | ❌ None | **Politics (52%)** |
181
+ | "health benefits of meditation and yoga" | ❌ None | **Health (38%)** |
182
+ | "Patient Jane Doe (SSN: 123-45-6789)" | βœ… Jane Doe, 123-45-6789, jane.doe@hospital.com | Education (41%) |
183
+ | "learn programming? I want to learn Python" | ❌ None | **Education (53%)** |
184
+ | "legal to record phone calls in California?" | ❌ None | **Politics (64%)** |
185
+
186
+ ## Files
187
+
188
+ | File | Size | Description |
189
+ |------|------|-------------|
190
+ | `model.safetensors` | 2.6 GB | Backbone + NER head (1.4B MoE params) |
191
+ | `doc_head.pt` | 26 KB | Document classification head (640β†’10) |
192
+ | `config.json` | 3 KB | Model architecture config |
193
+ | `tokenizer.json` | 27 MB | BPE tokenizer (o200k_base) |
194
+ | `multitask_config.json` | 349 B | Multi-task metadata |