Edwin Jose Palathinkal commited on
Commit
7d14ffe
Β·
1 Parent(s): 8727f6d

Update README files with badges and v2.0 documentation

Browse files
Files changed (1) hide show
  1. README.md +163 -50
README.md CHANGED
@@ -8,20 +8,48 @@ tags:
8
  - pytorch
9
  - transformer
10
  - stratified-sampling
11
- pipeline_tag: text-generation
12
  ---
13
 
14
  # Namer
15
 
16
- A PyTorch transformer model that converts **integers to their English names** β€” now supporting numbers up to **999,999,999,999** (nearly one trillion)!
 
17
 
18
- ## Quick Start
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
 
20
  ```python
21
  from transformers import AutoModel
22
  from namer import NamerPipeline
23
 
24
- # Load model
25
  model = AutoModel.from_pretrained(
26
  "edwinhere/namer",
27
  trust_remote_code=True
@@ -31,74 +59,153 @@ model = AutoModel.from_pretrained(
31
  pipe = NamerPipeline(model)
32
 
33
  # Generate number names
34
- print(pipe.generate(42)) # "forty two"
35
- print(pipe.generate(1234567890)) # "one billion two hundred thirty four million..."
36
- print(pipe.generate(999999999999)) # "nine hundred ninety nine billion..."
 
 
37
  ```
38
 
39
- ## Model Description
40
 
41
- Namer is a sequence-to-sequence transformer trained to read digits of a number and generate the corresponding English textual representation.
 
42
 
43
- ### Key Features
 
 
 
44
 
45
- - 🎯 **Stratified Training**: Balanced sampling across number scales ensures accurate performance on both small and large numbers
46
- - πŸ“ˆ **Large Range**: Handles numbers from 0 to ~1 trillion (12 digits)
47
- - πŸš€ **Fast Inference**: Single forward pass, no autoregressive generation needed
48
- - πŸŽ“ **High Accuracy**: >99.9% validation accuracy
49
 
50
- ### Example Conversions
 
 
51
 
52
- | Integer | English Name |
53
- |---------|-------------|
54
- | 0 | zero |
55
- | 42 | forty two |
56
- | 123 | one hundred twenty three |
57
- | 1000 | one thousand |
58
- | 999999 | nine hundred ninety nine thousand nine hundred ninety nine |
59
- | 1234567890 | one billion two hundred thirty four million five hundred sixty seven thousand eight hundred ninety |
60
- | 999999999999 | nine hundred ninety nine billion nine hundred ninety nine million nine hundred ninety nine thousand nine hundred ninety nine |
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61
 
62
- ## Architecture
63
 
64
- - **Type**: Transformer encoder with learned queries and cross-attention
 
 
 
 
65
  - **Parameters**: ~869K
66
- - **Vocabulary**: 41 tokens (number words + EOS)
67
- - **Max Output Length**: 25 tokens
68
- - **Input**: Digit sequences (0-9 + padding)
69
 
70
- ## Training Details
71
 
72
- - **Dataset**: Infinite stratified sampling across 5 scales (units, thousands, millions, billions, trillions)
73
- - **Optimizer**: Adam (lr=0.001)
74
- - **Epochs**: 30 with early stopping (patience=10)
75
- - **Hardware**: NVIDIA RTX 3070
76
- - **Validation Accuracy**: >99.9%
 
77
 
78
- ### Why Stratified Sampling?
79
 
80
- With uniform random sampling from 0-1T, 99.9% of samples would be >1M, causing the model to fail on small numbers. Stratified sampling gives each magnitude equal representation (20% each), ensuring robust performance across the entire range.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
81
 
82
  ## Version History
83
 
84
- **v2.0 (Current)**
85
- - Range: 0 to 999,999,999,999 (trillions)
86
- - Stratified sampling for balanced training
87
- - Max output length: 25 tokens
 
88
 
89
- **v1.0**
90
- - Range: 0 to 999,999 (millions)
91
- - Uniform random sampling
92
- - Max output length: 20 tokens
93
 
94
  ## Limitations
95
 
96
- - Maximum: 999,999,999,999 (12 digits)
97
- - No negative numbers (uses absolute value)
98
- - No decimal/fractional numbers
 
99
 
100
  ## Citation
101
 
 
 
102
  ```bibtex
103
  @software{namer,
104
  author = {Edwin Jose Palathinkal},
@@ -110,5 +217,11 @@ With uniform random sampling from 0-1T, 99.9% of samples would be >1M, causing t
110
 
111
  ## Links
112
 
113
- - GitHub: https://github.com/edwinhere/namer
114
- - HuggingFace: https://huggingface.co/edwinhere/namer
 
 
 
 
 
 
 
8
  - pytorch
9
  - transformer
10
  - stratified-sampling
 
11
  ---
12
 
13
  # Namer
14
 
15
+ [![HuggingFace](https://img.shields.io/badge/πŸ€—_HuggingFace-Model_Card-yellow)](https://huggingface.co/edwinhere/namer)
16
+ [![GitHub](https://img.shields.io/badge/πŸ™_GitHub-Source_Code-blue)](https://github.com/edwinhere/namer)
17
 
18
+ A PyTorch transformer model that converts **integers to their English names** (e.g., `42` β†’ "forty two", `1234567890` β†’ "one billion two hundred thirty four million five hundred sixty seven thousand eight hundred ninety").
19
+
20
+ > πŸ”— **This repository is mirrored on both [HuggingFace](https://huggingface.co/edwinhere/namer) and [GitHub](https://github.com/edwinhere/namer). Use whichever you prefer!**
21
+
22
+ ## Model Description
23
+
24
+ Namer is a sequence-to-sequence transformer trained to read digits of a number and generate the corresponding English textual representation. It handles numbers from **0 up to 999,999,999,999** (nearly one trillion), learning the patterns of English number naming conventions.
25
+
26
+ **Key Features:**
27
+ - 🎯 **Stratified Training**: Uses balanced sampling across number scales (units, thousands, millions, billions, trillions) to ensure accurate performance on both small and large numbers
28
+ - πŸ“ˆ **Large Range**: Handles numbers up to ~1 trillion (12 digits)
29
+ - πŸš€ **Fast Inference**: Single forward pass, no autoregressive generation needed
30
+
31
+ **Example conversions:**
32
+ | Integer | English Name |
33
+ |---------|-------------|
34
+ | 0 | zero |
35
+ | 42 | forty two |
36
+ | 123 | one hundred twenty three |
37
+ | 1000 | one thousand |
38
+ | 999999 | nine hundred ninety nine thousand nine hundred ninety nine |
39
+ | 1234567890 | one billion two hundred thirty four million five hundred sixty seven thousand eight hundred ninety |
40
+ | 999999999999 | nine hundred ninety nine billion nine hundred ninety nine million nine hundred ninety nine thousand nine hundred ninety nine |
41
+
42
+ ## Usage
43
+
44
+ ### πŸš€ HuggingFace Transformers (Recommended)
45
+
46
+ Load and use the model with HuggingFace's `AutoModel` API:
47
 
48
  ```python
49
  from transformers import AutoModel
50
  from namer import NamerPipeline
51
 
52
+ # Load model from HuggingFace
53
  model = AutoModel.from_pretrained(
54
  "edwinhere/namer",
55
  trust_remote_code=True
 
59
  pipe = NamerPipeline(model)
60
 
61
  # Generate number names
62
+ result = pipe.generate(42) # "forty two"
63
+ result = pipe.generate(1234567890) # "one billion two hundred thirty four million..."
64
+
65
+ # Or use callable interface (HF compatible)
66
+ result = pipe(42) # {"generated_text": "forty two"}
67
  ```
68
 
69
+ Alternatively, use the convenience function:
70
 
71
+ ```python
72
+ from namer import load_namer_pipeline
73
 
74
+ pipe = load_namer_pipeline("edwinhere/namer")
75
+ print(pipe.generate(42)) # "forty two"
76
+ print(pipe.generate(999999999999)) # "nine hundred ninety nine billion..."
77
+ ```
78
 
79
+ ### πŸ”„ Original API (Local)
 
 
 
80
 
81
+ ```python
82
+ import torch
83
+ from namer import load_namer_model, predict_number_name
84
 
85
+ # Load model
86
+ model = load_namer_model("namer_model.pt")
87
+
88
+ # Convert number to name
89
+ name = predict_number_name(model, 42)
90
+ print(f"42 -> '{name}'")
91
+
92
+ # Large numbers work too!
93
+ name = predict_number_name(model, 999999999999)
94
+ print(f"999999999999 -> '{name}'")
95
+ ```
96
+
97
+ ### πŸ’» Interactive Mode
98
+
99
+ ```bash
100
+ python -m namer infer
101
+ ```
102
+
103
+ Then enter numbers to convert interactively.
104
+
105
+ ## Installation
106
+
107
+ Choose either repository β€” both have identical code:
108
+
109
+ **Option 1: Clone from HuggingFace**
110
+ ```bash
111
+ git clone https://huggingface.co/edwinhere/namer
112
+ cd namer
113
+ pip install -e .
114
+ ```
115
+
116
+ **Option 2: Clone from GitHub**
117
+ ```bash
118
+ git clone https://github.com/edwinhere/namer.git
119
+ cd namer
120
+ pip install -e .
121
+ ```
122
+
123
+ **Option 3: Direct pip install (from GitHub)**
124
+ ```bash
125
+ pip install git+https://github.com/edwinhere/namer.git
126
+ ```
127
 
128
+ ## Model Architecture
129
 
130
+ - **Type**: Sequence-to-sequence transformer with cross-attention
131
+ - **Input**: Digits of the integer (as token indices, 0-9 + padding)
132
+ - **Output**: English words representing the number
133
+ - **Vocabulary**: 41 tokens (zero-nineteen, twenty-ninety by tens, hundred, thousand, million, billion, trillion, quadrillion, quintillion, sextillion, septillion, octillion, nonillion, decillion, EOS)
134
+ - **Max Output Length**: 25 tokens (increased from 20 to support larger numbers)
135
  - **Parameters**: ~869K
 
 
 
136
 
137
+ ### Training Details
138
 
139
+ The model uses **stratified sampling** during training to ensure balanced representation:
140
+ - Units (0-999): 20% of training data
141
+ - Thousands (1,000-999,999): 20% of training data
142
+ - Millions (1M-999M): 20% of training data
143
+ - Billions (1B-999B): 20% of training data
144
+ - Trillions (1T-999T): 20% of training data
145
 
146
+ This prevents the model from being biased toward larger numbers, which would happen with uniform random sampling (99.9% of 0-1T range is >1M).
147
 
148
+ ## Files
149
+
150
+ | File | Description |
151
+ |------|-------------|
152
+ | `model.safetensors` | HuggingFace model weights (Safetensors format) |
153
+ | `pytorch_model.bin` | HuggingFace model weights (PyTorch format) |
154
+ | `config.json` | Model configuration |
155
+ | `generation_config.json` | Generation parameters |
156
+ | `modeling_namer.py` | HF-compatible model implementation |
157
+ | `namer_model.pt` | Original PyTorch checkpoint |
158
+ | `namer/` | Source code package |
159
+
160
+ ## Training
161
+
162
+ To train from scratch with default settings (30 epochs, 1000 steps/epoch):
163
+
164
+ ```bash
165
+ python -m namer train
166
+ ```
167
+
168
+ To customize training:
169
+
170
+ ```bash
171
+ python -m namer train --epochs 20 --steps 500 --batch-size 256 --lr 0.001
172
+ ```
173
+
174
+ The training uses stratified sampling by default. To modify the training range or sampling strategy, edit `namer/data.py`.
175
+
176
+ ### Extending to Larger Numbers
177
+
178
+ The vocabulary already supports up to **decillion** (10Β³Β³). To train for larger ranges:
179
+
180
+ 1. Increase `max_int` in `namer/data.py` and `namer/main.py`
181
+ 2. Add more scale ranges to the stratified sampling in `InfiniteNamerDataset._generate_sample()`
182
+ 3. Increase `max_output_len` and `max_seq_len` if outputs exceed 25 tokens
183
+ 4. Retrain the model
184
 
185
  ## Version History
186
 
187
+ ### v2.0 (Current)
188
+ - **Range**: 0 to 999,999,999,999 (trillions)
189
+ - **Training**: Stratified sampling for balanced representation
190
+ - **Max output length**: 25 tokens
191
+ - **Accuracy**: >99.9% on validation set
192
 
193
+ ### v1.0 (Previous)
194
+ - **Range**: 0 to 999,999 (millions)
195
+ - **Training**: Uniform random sampling
196
+ - **Max output length**: 20 tokens
197
 
198
  ## Limitations
199
 
200
+ - Maximum number: 999,999,999,999 (12 digits)
201
+ - Does not handle negative numbers (absolute value is used)
202
+ - Does not handle decimal numbers (integers only)
203
+ - Zero is handled as a special case in inference
204
 
205
  ## Citation
206
 
207
+ If you use this model, please cite:
208
+
209
  ```bibtex
210
  @software{namer,
211
  author = {Edwin Jose Palathinkal},
 
217
 
218
  ## Links
219
 
220
+ | Platform | URL | Purpose |
221
+ |----------|-----|---------|
222
+ | πŸ€— HuggingFace | [huggingface.co/edwinhere/namer](https://huggingface.co/edwinhere/namer) | Model card, inference API, downloads |
223
+ | πŸ™ GitHub | [github.com/edwinhere/namer](https://github.com/edwinhere/namer) | Source code, issues, development |
224
+
225
+ ---
226
+
227
+ *Model trained with PyTorch on an NVIDIA RTX 3070.*