Asia/Kolkata
Posts

Training a 632 KB Name Classifier From Scratch in PyTorch and ONNX

November 27, 2026
Most text tools start by pulling down a 400 MB BERT checkpoint and tuning a head on top. That works for tests, yet it feels slow when all you need is a fast check: is this string a real name, or is it a gamer tag, roll code, or spam handle? So I built CompactNameClassifier (GitHub) from scratch in PyTorch. After INT8 ONNX export, the whole model weighs ~632 KB and runs in 0.449 ms per call on one CPU core in Node.js. That gives over 2,200 calls per second with no Python runtime needed live.

Why Pretrained Nets Missed Edge Cases

Before writing my own net, I tried a pruned 4.3 MB pretrained baseline. On clean names it looked fine, yet on messy strings it scored just 52.9% (9/17) on my edge-case test set. Two flaws caused most of the misses:
  1. Short text cutoffs: A 16-token limit clipped off prefixes or multi-word names, hiding noisy ends.
  2. Case and digit blind spots: Student roll codes like AYAN CSE3048 or handles like 112_SAYANTIKA BISWAS fooled nets built on broad 30,522-token word lists.
I wanted a net that keeps every digit and underscore clear, runs in under half a millisecond on CPU, and stays tiny.

1. Custom 4,000-Token WordPiece Split

Instead of keeping a 30k BERT word list, I built a small uncased WordPiece list with just 4,000 tokens and a 20-token window. Two setup choices made the biggest gain:
  • Full case and accent folding: Running NFKC(), Lowercase(), and StripAccents() up front keeps ALL CAPS inputs in bounds.
  • Splitting each digit: Using Digits(individual_digits=True) turns 3048 into 3, 0, 4, 8. The net spots digit noise at once without filling the word list with random numbers.

2. Inside the 532K-Weight Net

To cut weight count from 4.38M down to 532,482 weights (an 88% drop), I built a two-layer Pre-LN net with three clean tricks:
  1. Factorized vectors: Instead of mapping 4,000 tokens straight into 128 dims (4000 x 128 = 512K), token_emb maps into 64 dims and a linear step lifts it to 128 (4000 x 64 + 64 x 128 = 264K). That cuts lookup weights nearly in half.
  2. Masked dual pooling: I join Masked Mean Pool (128) and Masked Max Pool (128) into a 256-dim vector. Max Pool works like a tripwire. Even if a string starts with a real name like Sayantika, one noisy piece like _ or 3048 spikes the max channels and flags it as fake.
  3. Train-time gender head: During PyTorch training, a side gender head shares the 256-dim vector to keep features sharp. When exporting to ONNX, I drop that side head so live runs stay lean.
Here is the core PyTorch module from scripts/model.py:

3. Dataset Scale and Results

I trained the scratch model on 1,606,412 curated records (1,458,041 train and 148,371 validation). That split blends 1.18M global and regional human names with 280k non-names, gamer tags, English common words, and alphanumeric codes. On the 148k validation split and my corner-case suite, the numbers jumped sharply: | Metric | Old Pruned Baseline | CompactNameClassifier | | :--- | :--- | :--- | | INT8 ONNX Size | 2.45 MB to 4.30 MB | ~632 KB | | Parameters | 4,380,000 | 532,482 | | Single-Thread Latency | ~1.20 ms | 0.449 ms | | Node.js Throughput | ~800 qps | 2,228 qps | | Validation Accuracy / F1 | N/A | 94.45% / 96.21% | | Corner-Case Suite | 52.9% (9/17) | 94.1% (16/17) |

4. Running INT8 Inference in Python

Because the ONNX graph takes plain int32 buffers (with no token_type_ids bloat), running it with onnxruntime takes just a few lines: You can grab the weights on Hugging Face or inspect the full training and export scripts on GitHub.