File size: 9,311 Bytes
098de9c
 
 
 
 
7b5123e
 
098de9c
7b5123e
8727f6d
098de9c
c255ac5
098de9c
2730fd2
7d14ffe
 
72c9685
7d14ffe
 
 
 
 
 
14cdecf
7d14ffe
 
14cdecf
 
 
7d14ffe
 
 
 
 
 
 
 
 
 
 
 
14cdecf
7d14ffe
 
 
 
 
 
7b5123e
2730fd2
6ac124b
 
7b5123e
7d14ffe
6ac124b
 
 
 
7b5123e
6ac124b
 
7b5123e
6ac124b
7d14ffe
 
 
 
 
7b5123e
 
7d14ffe
7b5123e
7d14ffe
 
7b5123e
7d14ffe
 
 
 
7b5123e
7d14ffe
7b5123e
7d14ffe
 
 
6ac124b
7d14ffe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7b5123e
7d14ffe
72c9685
7d14ffe
 
 
 
14cdecf
 
2730fd2
7d14ffe
72c9685
14cdecf
 
 
 
 
 
 
 
 
 
 
 
52c1f80
7ef25b0
72c9685
7d14ffe
6ac124b
7d14ffe
 
 
 
 
 
 
 
 
 
 
 
 
14cdecf
7d14ffe
 
 
 
 
 
 
 
 
 
 
14cdecf
6ac124b
8727f6d
6ac124b
14cdecf
 
 
 
 
 
 
 
7d14ffe
 
 
 
6ac124b
7d14ffe
 
 
 
6ac124b
52c1f80
 
 
 
 
 
 
 
 
 
 
 
 
7ef25b0
 
 
 
 
 
 
 
 
 
 
8727f6d
6ac124b
52c1f80
14cdecf
 
6ac124b
098de9c
2730fd2
7d14ffe
 
098de9c
 
 
7b5123e
8727f6d
 
098de9c
2730fd2
7b5123e
 
 
7d14ffe
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
---
language: en
license: mit
library_name: pytorch
tags:
  - text-generation
  - number-to-text
  - pytorch
  - transformer
  - stratified-sampling
---

# Namer

[![HuggingFace](https://img.shields.io/badge/πŸ€—_HuggingFace-Model_Card-yellow)](https://huggingface.co/edwinhere/namer)
[![GitHub](https://img.shields.io/badge/πŸ™_GitHub-Source_Code-blue)](https://github.com/edwinhere/namer)

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").

> πŸ”— **This repository is mirrored on both [HuggingFace](https://huggingface.co/edwinhere/namer) and [GitHub](https://github.com/edwinhere/namer). Use whichever you prefer!**

## Model Description

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 9,223,372,036,854,775,807** (INT64_MAX), learning the patterns of English number naming conventions.

**Key Features:**
- 🎯 **Stratified Training**: Uses balanced sampling across 7 number scales (units to quintillions) to ensure accurate performance on both small and large numbers
- πŸ“š **Guaranteed Training Data**: Includes all numbers 0-99,999 and exact powers of 1000 to improve accuracy on edge cases
- πŸ“ˆ **Large Range**: Handles numbers up to INT64_MAX (19 digits, ~9.2 quintillion)
- πŸš€ **Fast Inference**: Single forward pass, no autoregressive generation needed

**Example conversions:**
| Integer | English Name |
|---------|-------------|
| 0 | zero |
| 42 | forty two |
| 123 | one hundred twenty three |
| 1000 | one thousand |
| 999999 | nine hundred ninety nine thousand nine hundred ninety nine |
| 1234567890 | one billion two hundred thirty four million five hundred sixty seven thousand eight hundred ninety |
| 999999999999 | nine hundred ninety nine billion nine hundred ninety nine million nine hundred ninety nine thousand nine hundred ninety nine |
| 9223372036854775807 | nine quintillion two hundred twenty three quadrillion three hundred seventy two trillion thirty six billion eight hundred fifty four million seven hundred seventy five thousand eight hundred seven |

## Usage

### πŸš€ HuggingFace Transformers (Recommended)

Load and use the model with HuggingFace's `AutoModel` API:

```python
from transformers import AutoModel
from namer import NamerPipeline

# Load model from HuggingFace
model = AutoModel.from_pretrained(
    "edwinhere/namer",
    trust_remote_code=True
)

# Create pipeline
pipe = NamerPipeline(model)

# Generate number names
result = pipe.generate(42)           # "forty two"
result = pipe.generate(1234567890)   # "one billion two hundred thirty four million..."

# Or use callable interface (HF compatible)
result = pipe(42)  # {"generated_text": "forty two"}
```

Alternatively, use the convenience function:

```python
from namer import load_namer_pipeline

pipe = load_namer_pipeline("edwinhere/namer")
print(pipe.generate(42))  # "forty two"
print(pipe.generate(999999999999))  # "nine hundred ninety nine billion..."
```

### πŸ”„ Original API (Local)

```python
import torch
from namer import load_namer_model, predict_number_name

# Load model
model = load_namer_model("namer_model.pt")

# Convert number to name
name = predict_number_name(model, 42)
print(f"42 -> '{name}'")

# Large numbers work too!
name = predict_number_name(model, 999999999999)
print(f"999999999999 -> '{name}'")
```

### πŸ’» Interactive Mode

```bash
python -m namer infer
```

Then enter numbers to convert interactively.

## Installation

Choose either repository β€” both have identical code:

**Option 1: Clone from HuggingFace**
```bash
git clone https://huggingface.co/edwinhere/namer
cd namer
pip install -e .
```

**Option 2: Clone from GitHub**
```bash
git clone https://github.com/edwinhere/namer.git
cd namer
pip install -e .
```

**Option 3: Direct pip install (from GitHub)**
```bash
pip install git+https://github.com/edwinhere/namer.git
```

## Model Architecture

- **Type**: Sequence-to-sequence transformer with cross-attention
- **Input**: Digits of the integer (as token indices, 0-9 + padding)
- **Output**: English words representing the number
- **Vocabulary**: 41 tokens (zero-nineteen, twenty-ninety by tens, hundred, thousand, million, billion, trillion, quadrillion, quintillion, sextillion, septillion, octillion, nonillion, decillion, EOS)
- **Max Output Length**: 35 tokens (increased from 20 to support INT64_MAX)
- **Parameters**: ~870K

### Training Details

The model uses **stratified sampling** during training to ensure balanced representation across 7 scales:
- Units (0-999): ~14% of training data
- Thousands (1,000-999,999): ~14% of training data  
- Millions (1M-999M): ~14% of training data
- Billions (1B-999B): ~14% of training data
- Trillions (1T-999T): ~14% of training data
- Quadrillions (1Q-999Q): ~14% of training data
- Quintillions (1Qi-INT64_MAX): ~14% of training data

**Guaranteed Training Samples:**
- All integers from 0 to 99,999 (100,000 samples)
- Exact powers of 1000: 1,000; 1,000,000; 1,000,000,000; 1,000,000,000,000; 1,000,000,000,000,000
- Numbers just after powers of 1000 (e.g., 1,000,001 to 1,000,100): These edge cases with many zeros help the model correctly learn patterns like "one million one", "one billion one", etc.
- Zero-only sequences of all lengths: `[0]`, `[0,0]`, `[0,0,0]`, ... up to max sequence length. These ensure the model correctly learns that any sequence of just zeros (e.g., `0`, `00`, `000`) produces "zero".

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).

## Files

| File | Description |
|------|-------------|
| `model.safetensors` | HuggingFace model weights (Safetensors format) |
| `pytorch_model.bin` | HuggingFace model weights (PyTorch format) |
| `config.json` | Model configuration |
| `generation_config.json` | Generation parameters |
| `namer_model.pt` | Original PyTorch checkpoint |
| `namer/` | Source code package |

## Training

To train from scratch with default settings (30 epochs, 1000 steps/epoch, INT64_MAX range):

```bash
python -m namer train
```

To customize training:

```bash
python -m namer train --epochs 20 --steps 500 --batch-size 256 --lr 0.001
```

The training uses stratified sampling by default with guaranteed samples. To modify the training range or sampling strategy, edit `namer/data.py`.

## Version History

### v3.0 (Current)
- **Range**: 0 to 9,223,372,036,854,775,807 (INT64_MAX, 19 digits)
- **Training**: Stratified sampling with guaranteed samples (0-99,999 + powers of 1000)
- **Max output length**: 35 tokens
- **Max sequence length**: 25 tokens
- **Accuracy**: >99.9% on validation set

### v2.0 (Previous)
- **Range**: 0 to 999,999,999,999 (trillions)
- **Training**: Stratified sampling for balanced representation
- **Max output length**: 25 tokens
- **Accuracy**: >99.9% on validation set

### v1.0 (Previous)
- **Range**: 0 to 999,999 (millions)
- **Training**: Uniform random sampling
- **Max output length**: 20 tokens

## Improvements in v3.1

### Fixed: Numbers with Many Zeroes
The model now correctly handles numbers immediately following powers of 1000 (e.g., 1,000,001 β†’ "one million one", 1,000,000,001 β†’ "one billion one"). This was achieved by adding **500 additional guaranteed training samples** - 100 numbers after each major power of 1000 (thousand, million, billion, trillion, quadrillion).

| Number | Output |
|--------|--------|
| 1,000,001 | one million one βœ“ |
| 1,000,042 | one million forty two βœ“ |
| 1,000,000,001 | one billion one βœ“ |
| 1,000,000,000,001 | one trillion one βœ“ |
| 1,000,000,000,000,001 | one quadrillion one βœ“ |

### Fixed: Zero Handling
The model now correctly handles zero and single-digit inputs. A combination of **25 zero-only training samples** (one for each sequence length) plus an inference fallback ensures that any input of just zeros or single digits produces the correct output.

| Input | Output |
|-------|--------|
| 0 | zero βœ“ |
| 1 | one βœ“ |
| 5 | five βœ“ |
| 00 | zero βœ“ |
| 0000000000000000000 (19 zeros) | zero βœ“ |

## Limitations

- **Exact powers of 1000 above million**: The model may occasionally produce extra words for exact powers at higher scales (e.g., "one million million" instead of "one million" for 1,000,000). This is an edge case in EOS prediction at trillion+ scales.
- **Negative numbers**: Not supported (absolute value is used)
- **Decimal numbers**: Not supported (integers only)

## Citation

If you use this model, please cite:

```bibtex
@software{namer,
  author = {Edwin Jose Palathinkal},
  title = {Namer: Integer to English Name Converter},
  url = {https://huggingface.co/edwinhere/namer},
  year = {2025}
}
```

## Links

| Platform | URL | Purpose |
|----------|-----|---------|
| πŸ€— HuggingFace | [huggingface.co/edwinhere/namer](https://huggingface.co/edwinhere/namer) | Model card, inference API, downloads |
| πŸ™ GitHub | [github.com/edwinhere/namer](https://github.com/edwinhere/namer) | Source code, issues, development |

---

*Model trained with PyTorch on an NVIDIA RTX 3070.*