embedding 0.1.5

A Rust library and CLI for training embeddings from scratch
Documentation
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
# Embedding Trainer

[![License: Apache-2.0](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)
[![Rust](https://img.shields.io/badge/rust-1.70+-orange.svg)](https://www.rust-lang.org/)
[![Version](https://img.shields.io/badge/version-0.1.3-blue.svg)](https://crates.io/crates/embedding)
[![Build Status](https://img.shields.io/badge/build-passing-green.svg)](https://github.com/yingkitw/embedding)

A fast and flexible Rust library and CLI tool for training word embeddings from scratch using Skip-gram and CBOW algorithms with built-in validation, evaluation, and semantic search.

## ✨ Features

### 🚀 **Algorithms**
- **Skip-gram**: Predicts context words given target words
- **CBOW**: Predicts target words given context words

### 📊 **Training Features**
- Configurable embedding dimensions
- Adjustable learning rates and epochs
- Customizable context windows
- Negative sampling support (uniform or unigram^0.75 distribution)
- Sub-sampling of frequent words (Mikolov-style)
- Learning rate warm-up for stabler early training
- Model checkpointing (save/resume every N epochs)
- Multi-threaded parallel training over CPU cores
- Batch processing capabilities
- Learning rate scheduling (constant, exponential, step, cosine)
- Early stopping with configurable patience
- L2 regularization and gradient clipping
- Train/validation split with metrics export
- Per-epoch training history / learning curves (JSON export)
- K-fold cross-validation with averaged metrics

### 🔧 **CLI Tools**
- **Training**: Train embeddings from text data with optional validation split
- **Similarity**: Calculate semantic similarity between words
- **Inspection**: Analyze trained models and vocabulary
- **Export**: Save embeddings in multiple formats (text, JSON, binary, Word2Vec)
- **Validate**: Evaluate a saved model on held-out validation text
- **Interactive**: Query trained models interactively (similarity, analogy, search)

### **Evaluation & Analysis**
- **Benchmarks**: Evaluate against standard word similarity benchmarks (WordSim-353, SimLex-999) with Spearman correlation
- **Clustering**: K-means and hierarchical clustering of embeddings
- **Cross-validation**: K-fold cross-validation with per-fold metrics
- **Learning curves**: Per-epoch loss and learning rate tracking with JSON export

### **Data Support**
- Text file processing with Unicode normalization
- Source code preprocessing (Rust, Python, JavaScript, etc.)
- BPE subword tokenization and FastText-style character n-grams
- WordPiece subword tokenization (BERT-style)
- Vocabulary management
- Model persistence
- Multiple export formats (JSON, binary, Word2Vec, ONNX, NumPy)
- Streaming support for large datasets
- Pluggable compute backend trait (CPU implemented, GPU ready)

### 🤖 **Advanced Models**
- **Transformer encoder**: Multi-head self-attention with position encoding for contextualized embeddings
- **Multi-modal fusion**: Concatenation, weighted average, attention fusion, projection fusion, cross-modal similarity
- **Real-time training**: Incremental updates and streaming micro-batch training without full retrain

## 🚀 Quick Start

### Installation

```bash
# Clone the repository
git clone https://github.com/yingkitw/embedding.git
cd embedding

# Build the project
cargo build --release

# Or install locally
cargo install --path .
```

### GPU Acceleration (Optional)

Enable GPU compute via the `gpu` feature flag. This uses [wgpu](https://github.com/gfx-rs/wgpu) compute shaders and works on Vulkan, Metal, and DX12 backends without vendor-specific SDKs.

```bash
# Build with GPU support
cargo build --release --features gpu

# Install with GPU support
cargo install --path . --features gpu
```

When the `gpu` feature is enabled, `EmbeddingModel::new()` automatically selects the best available backend (GPU if present, otherwise CPU). You can also explicitly create a GPU backend:

```rust
use embedding::backend::{GpuBackend, Backend};

// Attempt GPU initialization; fails gracefully if no GPU is available
if let Ok(gpu) = GpuBackend::new() {
    println!("Using {} backend", gpu.name());
    let embeddings = gpu.init_embeddings(1000, 300);
}
```

> **Note:** GPU operations have CPU-GPU transfer overhead. For small models the CPU backend may still be faster. GPU acceleration shines with large batch matrix multiplications (`matmul`).

### Basic Usage

#### 1. Train Your First Embeddings

```bash
# Prepare your training data
echo "the quick brown fox jumps over the lazy dog" > data.txt

# Train embeddings using Skip-gram
embedding train \
    --input data.txt \
    --output model.json \
    --embeddings embeddings.txt \
    --dim 100 \
    --epochs 10 \
    --model-type skipgram

# Train with validation split
embedding train \
    --input data.txt \
    --output model.json \
    --embeddings embeddings.txt \
    --dim 100 \
    --epochs 10 \
    --validation-ratio 0.2 \
    --validation-output metrics.json
```

#### 2. Calculate Similarity

```bash
# Calculate similarity between words
embedding similarity fox dog \
    --model model.json

# Expected output:
# Similarity between 'fox' and 'dog': 0.8234
```

#### 3. Inspect Model

```bash
# View model information
embedding info --model model.json

# Shows vocabulary size, embedding dimension, training config
```

#### 4. Export Embeddings

```bash
# Export to different formats
embedding export \
    --model model.json \
    --output embeddings.json \
    --format json
```

## 📚 Library Usage

### Basic Example

```rust
use embedding::*;

fn main() -> Result<(), String> {
    // Load and prepare data in one step
    let data = TrainingData::from_text("the quick brown fox jumps over the lazy dog");

    // Configure training with sensible defaults and fluent setters
    let config = TrainingConfig::new(ModelType::SkipGram)
        .with_dim(300)
        .with_epochs(10);

    // Train model
    let mut model = EmbeddingModel::new(config, data.vocab.len());
    model.train(&data)?;

    // Calculate similarity
    if let Some(similarity) = model.similarity("fox", "dog", &data) {
        println!("Similarity: {:.4}", similarity);
    }

    // Save model
    model.save_embeddings("embeddings.txt", &data)?;

    Ok(())
}
```

### Advanced Usage

```rust
use embedding::*;

fn advanced_example() -> Result<(), String> {
    // Load data from a file in one step
    let data = TrainingData::from_file("large_dataset.txt")?;
    println!("Vocabulary size: {}", data.vocab.len());

    // Configure advanced training parameters with fluent setters
    let config = TrainingConfig::new(ModelType::Cbow)
        .with_dim(500)
        .with_learning_rate(0.01)
        .with_epochs(50)
        .with_batch_size(128)
        .with_window(10)
        .with_negative_samples(10)
        .with_validation_ratio(0.2)
        .with_subsample_threshold(Some(1e-5))
        .with_unigram_negative_sampling(true)
        .with_warmup_epochs(Some(3))
        .with_checkpoint_interval(Some(10))
        .with_checkpoint_path(Some("./checkpoints".to_string()))
        .with_parallel(true);

    // Train model
    let mut model = EmbeddingModel::new(config, data.vocab.len());
    model.train(&data)?;

    // Evaluate with cross-validation
    let cv = model.cross_validate(&data, 5)?;
    println!("Cross-validation accuracy: {:.4}", cv.averaged_metrics.accuracy);

    // Export to multiple formats
    model.save_embeddings("embeddings.txt", &data)?;
    println!("Training completed!");

    Ok(())
}
```

## 🔧 Configuration

### Training Parameters

| Parameter | Description | Default Value | Range |
|-----------|-------------|---------------|-------|
| `--dim` | Embedding dimension | 300 | 10-1000 |
| `--learning-rate` | Learning rate | 0.025 | 0.001-1.0 |
| `--epochs` | Number of training epochs | 10 | 1-1000 |
| `--batch-size` | Mini-batch size | 32 | 1-1000 |
| `--window` | Context window size | 5 | 1-20 |
| `--negative-samples` | Number of negative samples | 5 | 1-20 |
| `--validation-ratio` | Fraction of data for validation | 0.0 | 0.0-0.5 |
| `--validation-output` | File to write validation metrics JSON | - | - |

### Algorithm Types

- **`skipgram`**: Skip-gram algorithm (default)
- **`cbow`**: Continuous Bag of Words

### Export Formats

- **`text`**: Plain text format (default)
- **`json`**: JSON format with metadata
- **`bin`**: Binary format using bincode
- **`word2vec`**: Word2Vec/Gensim text format

## 📖 CLI Reference

### Training Command

```bash
embedding train [OPTIONS]
```

**Options:**
- `--input <FILE>` - Input text file (required)
- `--output <FILE>` - Output model file (required)
- `--embeddings <FILE>` - Embeddings output file (required)
- `--dim <SIZE>` - Embedding dimension (default: 300)
- `--learning-rate <RATE>` - Learning rate (default: 0.025)
- `--epochs <COUNT>` - Number of epochs (default: 10)
- `--batch-size <SIZE>` - Batch size (default: 32)
- `--window <SIZE>` - Context window size (default: 5)
- `--negative-samples <COUNT>` - Negative samples (default: 5)
- `--model-type <TYPE>` - Algorithm type (skipgram|cbow)
- `--validation-ratio <RATIO>` - Fraction for validation (default: 0.0)
- `--validation-output <FILE>` - Path to write validation metrics JSON

### Similarity Command

```bash
embedding similarity <WORD1> <WORD2> [OPTIONS]
```

**Options:**
- `--model <FILE>` - Model file (required)

### Info Command

```bash
embedding info [OPTIONS]
```

**Options:**
- `--model <FILE>` - Model file (required)

### Export Command

```bash
embedding export [OPTIONS]
```

**Options:**
- `--model <FILE>` - Model file (required)
- `--output <FILE>` - Output file (required)
- `--format <FORMAT>` - Export format (text|json|bin|word2vec)

### Validate Command

```bash
embedding validate [OPTIONS]
```

**Options:**
- `--model <FILE>` - Model file (required)
- `--input <FILE>` - Validation text file (required)
- `--output <FILE>` - Output metrics JSON file (optional)

## 🔍 Examples

### Example 1: Basic Word Embeddings

```bash
# Create sample data
cat > animals.txt << EOF
cat meows loudly
dog barks loudly
bird sings beautifully
fish swims quietly
horse gallops fast
EOF

# Train embeddings
embedding train \
    --input animals.txt \
    --output animal_model.json \
    --embeddings animal_embeddings.txt \
    --dim 50 \
    --epochs 20 \
    --model-type skipgram

# Test similarity
embedding similarity cat dog \
    --model animal_model.json
```

### Example 2: Document Embeddings

```bash
# Prepare document data
cat > documents.txt << EOF
Machine learning is a subset of artificial intelligence.
Deep learning uses neural networks with multiple layers.
Natural language processing deals with text and speech.
Computer vision enables computers to understand images.
EOF

# Train with CBOW and validation
embedding train \
    --input documents.txt \
    --output doc_model.json \
    --embeddings doc_embeddings.txt \
    --dim 100 \
    --epochs 15 \
    --model-type cbow \
    --validation-ratio 0.2
```

### Example 3: Large Dataset Processing

```bash
# Process large file with multiple epochs
embedding train \
    --input large_corpus.txt \
    --output large_model.json \
    --embeddings large_embeddings.txt \
    --dim 300 \
    --epochs 50 \
    --batch-size 256 \
    --window 10 \
    --model-type cbow
```

## 🧪 Development

### Building from Source

```bash
# Clone repository
git clone https://github.com/yingkitw/embedding.git
cd embedding

# Build development version
cargo build

# Run tests
cargo test

# Run benchmarks
cargo bench

# Build documentation
cargo doc --open
```

### Running Tests

```bash
# Run all tests
cargo test

# Run specific test
cargo test test_build_vocab

# Run with verbose output
cargo test -- --verbose
```

### Development Features

- **Unit Tests**: Comprehensive test coverage
- **Integration Tests**: End-to-end testing
- **Benchmarks**: Performance testing
- **Documentation**: API documentation

## 📊 Performance

### Benchmarks

| Algorithm | Vocab Size | Embed Dim | Training Time | Memory Usage |
|-----------|------------|-----------|---------------|--------------|
| Skip-gram | 10K words  | 300       | 2.3s          | 45MB         |
| CBOW      | 10K words  | 300       | 1.8s          | 42MB         |

### Optimization Tips

1. **Use appropriate batch sizes** for your dataset
2. **Adjust learning rate** based on dataset size
3. **Context window size** affects training speed and quality
4. **Use negative sampling** for large vocabularies
5. **Monitor memory usage** with large datasets

## 🤝 Contributing

We welcome contributions! Please see our [Contributing Guide](CONTRIBUTING.md) for details.

### Development Workflow

1. Fork the repository
2. Create a feature branch
3. Make your changes
4. Add tests for new functionality
5. Run the test suite
6. Submit a pull request

### Code Style

- Follow Rust formatting standards
- Use `cargo fmt` for code formatting
- Add comprehensive documentation
- Include tests for new features

## 📈 Roadmap

### Version 1.0 (Current)
- ✅ Skip-gram and CBOW algorithms
- ✅ CLI interface with train, validate, similarity, info, export
- ✅ Model persistence (JSON, binary, Word2Vec, ONNX, NumPy)
- ✅ Similarity calculations and semantic search
- ✅ Validation split and evaluation metrics (accuracy, precision, recall, F1)
- ✅ Learning rate scheduling (constant, exponential, step, cosine)
- ✅ Early stopping and L2 regularization

### Version 1.1 (Current — Features Complete)
- ✅ Backend abstraction trait for GPU acceleration (CPU implemented)
- ✅ WordPiece subword tokenization
- ✅ K-fold cross-validation support
- ✅ Per-epoch training history / learning curve JSON export
- ✅ Standard word similarity benchmark evaluation (Spearman correlation)
- ✅ K-means clustering
- CUDA/OpenCL backend implementation (planned)

### Version 2.0 (Current — Features Complete)
- ✅ Transformer encoder with multi-head self-attention and position encoding
- ✅ Enhanced multi-modal fusion (attention fusion, projection fusion, cross-modal similarity)
- ✅ Real-time incremental training (`IncrementalTrainer` with batch and stream modes)

### Version 2.1 (Current — Training Improvements Complete)
- ✅ Unigram^0.75 negative sampling distribution (Mikolov et al.)
- ✅ Sub-sampling of frequent words (`P(w) = 1 - sqrt(t / f(w))`)
- ✅ Learning rate warm-up (linear for first N epochs)
- ✅ Model checkpointing (save/resume every N epochs)
- ✅ Multi-threaded parallel training via `rayon`

## � Comparison with Alternatives

| Feature | **embedding** (this crate) | Gensim (Python) | rust-bert | fastText |
|---|---|---|---|---|
| **Language** | Rust | Python | Rust | C++ / Python |
| **Algorithms** | Skip-gram, CBOW, Transformer | Word2Vec, FastText, GloVe, LSI, LDA | BERT, RoBERTa, DistilBERT | Skip-gram, CBOW + subwords |
| **WordPiece tokenization** |||||
| **BPE tokenization** |||||
| **GPU acceleration** | ✅ (wgpu compute shaders, optional) || ✅ (via ONNX / tch) ||
| **Cross-validation** | ✅ (k-fold) ||||
| **Learning curves** | ✅ (per-epoch JSON export) ||||
| **LR warm-up** | ✅ (linear epochs) ||||
| **Checkpointing** | ✅ (save/resume) ||||
| **Parallel training** | ✅ (rayon, multi-core) ||||
| **Benchmark evaluation** | ✅ (Spearman correlation) | ✅ (similarity tasks) |||
| **K-means clustering** |||||
| **Incremental training** | ✅ (stream / batch updates) | ❌ (requires retrain) |||
| **Multi-modal fusion** | ✅ (4 fusion strategies) ||||
| **CLI tool** | ✅ (train, validate, search, export) ||||
| **Export formats** | JSON, binary, Word2Vec, ONNX, NumPy | Word2Vec, Gensim native | ONNX | `.vec`, `.bin` |
| **Memory mapping** | ✅ (binary format) ||||
| **Pre-trained models** | ✅ (Word2Vec text/binary, GloVe, fastText, mmap .bin) | ✅ (many built-in) | ✅ (Hugging Face) ||
| **Sentence embeddings** | ✅ (mean pooling) | ✅ (Doc2Vec) | ✅ (BERT pooling) ||
| **Speed** | ⚡ Fast (Rust native) | 🐌 Python overhead | ⚡ Fast (Rust native) | ⚡ Fast (C++) |
| **Zero dependencies for inference** | ✅ (after training) | ❌ (Gensim + NumPy + SciPy) | ❌ (ONNX / torch) | ✅ (`.vec` format) |

> **Legend**: ✅ = Supported | ❌ = Not supported | 🔶 = Partial / planned

## �� Troubleshooting

### Common Issues

1. **Memory Error with Large Datasets**
   - Reduce batch size
   - Use streaming processing
   - Increase system memory

2. **Poor Similarity Results**
   - Increase training epochs
   - Adjust learning rate
   - Try different algorithms

3. **Missing Words in Vocabulary**
   - Check text preprocessing
   - Verify tokenization
   - Ensure words appear in text

### Performance Issues

- **Slow Training**: Reduce batch size or use negative sampling
- **High Memory Usage**: Use smaller embedding dimensions
- **Poor Quality**: Increase epochs or adjust parameters

## 📄 License

This project is licensed under the Apache License 2.0 - see the [LICENSE](LICENSE) file for details.

## 🙏 Acknowledgments

- Inspired by Word2Vec, GloVe, and BERT
- Built with [ndarray]https://github.com/rust-ndarray/ndarray for numerical computing
- CLI powered by [clap]https://github.com/clap-rs/clap
- Serialization using [serde]https://serde.rs/