embedding 0.1.2

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
# 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/)
[![Build Status](https://img.shields.io/badge/build-passing-green.svg)](https://github.com/yourusername/embedding-trainer)

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 and evaluation.

## โœจ 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
- Batch processing capabilities

### ๐Ÿ”ง **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

### ๐Ÿ’พ **Data Support**
- Text file processing
- Vocabulary management
- Model persistence
- Multiple export formats
- Streaming support for large datasets

## ๐Ÿš€ Quick Start

### Installation

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

# Build the project
cargo build --release

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

### 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
    let text = "the quick brown fox jumps over the lazy dog";
    let sentences = load_text_data(text);
    let (vocab, reverse_vocab) = build_vocab(&sentences);

    let training_data = TrainingData {
        sentences,
        vocab,
        reverse_vocab,
    };

    // Configure training
    let config = TrainingConfig {
        embedding_dim: 300,
        learning_rate: 0.025,
        epochs: 10,
        batch_size: 32,
        context_window: 5,
        negative_samples: 5,
        model_type: ModelType::SkipGram,
        lr_schedule: LearningRateSchedule::Constant,
        early_stopping: None,
        l2_regularization: None,
        gradient_clip: None,
        validation_ratio: None,
    };
    
    // Train model
    let mut model = EmbeddingModel::new(config, training_data.vocab.len());
    model.train(&training_data)?;
    
    // Calculate similarity
    if let Some(similarity) = model.similarity("fox", "dog", &training_data) {
        println!("Similarity: {:.4}", similarity);
    }
    
    // Save model
    model.save_embeddings("embeddings.txt", &training_data)?;
    
    Ok(())
}
```

### Advanced Usage

```rust
use embedding::*;
use std::fs;

fn advanced_example() -> Result<(), String> {
    // Load large dataset with streaming
    let text = fs::read_to_string("large_dataset.txt")?;
    let sentences = load_text_data(&text);

    // Build vocabulary with size limit
    let (vocab, reverse_vocab) = build_vocab(&sentences);
    println!("Vocabulary size: {}", vocab.len());

    let training_data = TrainingData {
        sentences,
        vocab,
        reverse_vocab,
    };

    // Configure advanced training parameters
    let config = TrainingConfig {
        embedding_dim: 500,
        learning_rate: 0.01,
        epochs: 50,
        batch_size: 128,
        context_window: 10,
        negative_samples: 10,
        model_type: ModelType::Cbow,
        lr_schedule: LearningRateSchedule::Constant,
        early_stopping: None,
        l2_regularization: None,
        gradient_clip: None,
        validation_ratio: None,
    };
    
    // Train with multiple epochs
    let mut model = EmbeddingModel::new(config, training_data.vocab.len());
    
    // Train in chunks for large datasets
    for epoch in 0..10 {
        println!("Training epoch {}/10", epoch + 1);
        model.train(&training_data)?;
    }
    
    // Export to multiple formats
    model.save_embeddings("embeddings.txt", &training_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/yourusername/embedding-trainer.git
cd embedding-trainer

# 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 (Planned)
- GPU acceleration
- Advanced tokenization improvements
- Cross-validation support
- Learning curve visualization

### Version 2.0 (Future)
- Transformer-based models
- Multi-modal embeddings
- Real-time training
- Standard word similarity benchmarks integration

## ๐Ÿ› 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/

## ๐Ÿ“ž Support

- ๐Ÿ“ง **Email**: your.email@example.com
- ๐Ÿ’ฌ **Discussions**: [GitHub Discussions]https://github.com/yourusername/embedding-trainer/discussions
- ๐Ÿ› **Issues**: [GitHub Issues]https://github.com/yourusername/embedding-trainer/issues
- ๐Ÿ“– **Documentation**: [docs.rs/embedding-trainer]https://docs.rs/embedding-trainer

---

**Made with โค๏ธ by the Embedding Trainer Team**

*For the latest updates, check our [GitHub repository](https://github.com/yourusername/embedding-trainer)*