embedding 0.1.4

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
use ndarray::Array;
use ndarray_rand::rand_distr::Uniform;
use rand::Rng;
use std::collections::HashMap;
use tracing::info;
use crate::{EmbeddingModel, TrainingData};

impl EmbeddingModel {
    pub(crate) fn train_skipgram(&mut self, data: &TrainingData) -> Result<(), String> {
        let mut rng = rand::thread_rng();
        let mut best_loss = f32::MAX;
        let mut patience_counter = 0;

        for epoch in 0..self.config.epochs {
            info!("Epoch {}/{}", epoch + 1, self.config.epochs);

            let current_lr = self.get_learning_rate(epoch, self.config.epochs);
            info!("Current learning rate: {:.6}", current_lr);

            let mut total_loss = 0.0;
            let mut num_updates = 0;
            let mut batch_count = 0;
            let mut batch_loss = 0.0;
            let mut num_batches = 0;
            let mut accum = self.new_gradient_accumulator();

            for sentence in data.sentences.iter() {
                for (target_idx, target_word) in sentence.iter().enumerate() {
                    if let Some(&target_id) = data.vocab.get(target_word) {
                        let start = target_idx.saturating_sub(self.config.context_window);
                        let end = std::cmp::min(target_idx + self.config.context_window + 1, sentence.len());

                        // Get negative samples
                        let negative_samples = get_negative_samples(
                            self.vocab_size,
                            self.config.negative_samples,
                            target_id,
                            &mut rng
                        );

                        for (context_idx, context_word) in sentence.iter().enumerate().skip(start).take(end - start) {
                            if context_idx != target_idx
                                && let Some(&context_id) = data.vocab.get(context_word) {
                                    let loss = self.compute_skipgram_gradients(
                                        target_id, context_id, &negative_samples, current_lr, &mut accum
                                    );
                                    total_loss += loss;
                                    batch_loss += loss;
                                    num_updates += 1;
                                    batch_count += 1;

                                    if batch_count >= self.config.batch_size {
                                        self.apply_gradient_batch(&mut accum, batch_count);
                                        num_batches += 1;
                                        if num_batches % 10 == 0 {
                                            info!("  Batch {} avg loss: {:.4}", num_batches, batch_loss / batch_count as f32);
                                        }
                                        batch_loss = 0.0;
                                        batch_count = 0;
                                    }
                                }
                        }
                    }
                }
            }

            // Apply any remaining gradients
            if batch_count > 0 {
                self.apply_gradient_batch(&mut accum, batch_count);
            }

            let avg_loss = total_loss / num_updates.max(1) as f32;
            info!("Epoch {} completed. Average loss: {:.4}", epoch + 1, avg_loss);
            self.training_history.record_epoch(epoch + 1, avg_loss, current_lr as f64);

            // Early stopping check
            if self.should_early_stop(epoch + 1, avg_loss, best_loss, &mut patience_counter) {
                info!("Early stopping triggered at epoch {}", epoch + 1);
                break;
            }

            if avg_loss < best_loss {
                best_loss = avg_loss;
                patience_counter = 0;
            }
        }

        Ok(())
    }

    pub(crate) fn train_cbow(&mut self, data: &TrainingData) -> Result<(), String> {
        let mut rng = rand::thread_rng();
        let mut best_loss = f32::MAX;
        let mut patience_counter = 0;

        for epoch in 0..self.config.epochs {
            info!("Epoch {}/{}", epoch + 1, self.config.epochs);

            let current_lr = self.get_learning_rate(epoch, self.config.epochs);
            info!("Current learning rate: {:.6}", current_lr);

            let mut total_loss = 0.0;
            let mut num_updates = 0;
            let mut batch_count = 0;
            let mut batch_loss = 0.0;
            let mut num_batches = 0;
            let mut accum = self.new_gradient_accumulator();

            for sentence in data.sentences.iter() {
                for (target_idx, target_word) in sentence.iter().enumerate() {
                    if let Some(&target_id) = data.vocab.get(target_word) {
                        let start = target_idx.saturating_sub(self.config.context_window);
                        let end = std::cmp::min(target_idx + self.config.context_window + 1, sentence.len());

                        let mut context_ids = Vec::new();
                        for (context_idx, context_word) in sentence.iter().enumerate().skip(start).take(end - start) {
                            if context_idx != target_idx
                                && let Some(&context_id) = data.vocab.get(context_word) {
                                    context_ids.push(context_id);
                                }
                        }

                        if !context_ids.is_empty() {
                            // Get negative samples
                            let negative_samples = get_negative_samples(
                                self.vocab_size,
                                self.config.negative_samples,
                                target_id,
                                &mut rng
                            );

                            let loss = self.compute_cbow_gradients(
                                target_id, &context_ids, &negative_samples, current_lr, &mut accum
                            );
                            total_loss += loss;
                            batch_loss += loss;
                            num_updates += 1;
                            batch_count += 1;

                            if batch_count >= self.config.batch_size {
                                self.apply_gradient_batch(&mut accum, batch_count);
                                num_batches += 1;
                                if num_batches % 10 == 0 {
                                    info!("  Batch {} avg loss: {:.4}", num_batches, batch_loss / batch_count as f32);
                                }
                                batch_loss = 0.0;
                                batch_count = 0;
                            }
                        }
                    }
                }
            }

            // Apply any remaining gradients
            if batch_count > 0 {
                self.apply_gradient_batch(&mut accum, batch_count);
            }

            let avg_loss = total_loss / num_updates.max(1) as f32;
            info!("Epoch {} completed. Average loss: {:.4}", epoch + 1, avg_loss);
            self.training_history.record_epoch(epoch + 1, avg_loss, current_lr as f64);

            // Early stopping check
            if self.should_early_stop(epoch + 1, avg_loss, best_loss, &mut patience_counter) {
                info!("Early stopping triggered at epoch {}", epoch + 1);
                break;
            }

            if avg_loss < best_loss {
                best_loss = avg_loss;
                patience_counter = 0;
            }
        }

        Ok(())
    }

    fn new_gradient_accumulator(&self) -> HashMap<usize, Vec<f32>> {
        HashMap::new()
    }

    fn accumulate_gradient(&self, accum: &mut HashMap<usize, Vec<f32>>, word_id: usize, grad: f32, dim_idx: usize) {
        accum.entry(word_id)
            .or_insert_with(|| vec![0.0; self.config.embedding_dim])[dim_idx] += grad;
    }

    fn apply_gradient_batch(&mut self, accum: &mut HashMap<usize, Vec<f32>>, batch_size: usize) {
        let scale = 1.0 / batch_size.max(1) as f32;
        for (&word_id, grads) in accum.iter() {
            for (i, &grad) in grads.iter().enumerate() {
                self.embeddings[[word_id, i]] -= self.clip_gradient(grad * scale);
            }
        }
        accum.clear();
    }

    fn compute_skipgram_gradients(
        &self,
        target_id: usize,
        context_id: usize,
        negative_samples: &[usize],
        learning_rate: f32,
        accum: &mut HashMap<usize, Vec<f32>>,
    ) -> f32 {
        let target_embedding: Vec<f32> = self.embeddings.row(target_id).to_vec();
        let context_embedding: Vec<f32> = self.embeddings.row(context_id).to_vec();

        let dot_product: f32 = target_embedding.iter().zip(context_embedding.iter()).map(|(&a, &b)| a * b).sum();
        let prob_positive = sigmoid(dot_product);
        let grad_positive = prob_positive - 1.0;

        for (i, (&t, &c)) in target_embedding.iter().zip(context_embedding.iter()).enumerate() {
            let grad = self.l2_grad(learning_rate * grad_positive * c, learning_rate, t);
            self.accumulate_gradient(accum, target_id, grad, i);

            let grad_context = self.l2_grad(learning_rate * grad_positive * t, learning_rate, c);
            self.accumulate_gradient(accum, context_id, grad_context, i);
        }

        let mut loss = -prob_positive.ln();
        for &neg_id in negative_samples {
            let neg_embedding: Vec<f32> = self.embeddings.row(neg_id).to_vec();
            let dot_product_neg: f32 = target_embedding.iter().zip(neg_embedding.iter()).map(|(&a, &b)| a * b).sum();
            let prob_negative = sigmoid(dot_product_neg);
            let grad_negative = prob_negative;
            loss += -(1.0 - prob_negative).ln();

            for (i, (&t, &n)) in target_embedding.iter().zip(neg_embedding.iter()).enumerate() {
                let grad = self.l2_grad(learning_rate * grad_negative * n, learning_rate, t);
                self.accumulate_gradient(accum, target_id, grad, i);

                let grad_neg = self.l2_grad(learning_rate * grad_negative * t, learning_rate, n);
                self.accumulate_gradient(accum, neg_id, grad_neg, i);
            }
        }
        loss
    }

    fn compute_cbow_gradients(
        &self,
        target_id: usize,
        context_ids: &[usize],
        negative_samples: &[usize],
        learning_rate: f32,
        accum: &mut HashMap<usize, Vec<f32>>,
    ) -> f32 {
        let mut context_vector = Array::zeros(self.config.embedding_dim);
        let mut context_embeddings: Vec<Vec<f32>> = Vec::new();

        for &context_id in context_ids {
            let context_embedding: Vec<f32> = self.embeddings.row(context_id).to_vec();
            context_embeddings.push(context_embedding.clone());
            let context_arr = Array::from_shape_vec((self.config.embedding_dim,), context_embedding).unwrap();
            context_vector += &context_arr;
        }
        context_vector /= context_ids.len() as f32;

        let target_embedding: Vec<f32> = self.embeddings.row(target_id).to_vec();
        let target_arr = Array::from_shape_vec((self.config.embedding_dim,), target_embedding.clone()).unwrap();
        let dot_product = context_vector.dot(&target_arr);
        let prob_positive = sigmoid(dot_product);
        let grad_positive = prob_positive - 1.0;

        for (context_idx, &context_id) in context_ids.iter().enumerate() {
            let _context_embedding = &context_embeddings[context_idx];
            for (i, &target_val) in target_embedding.iter().enumerate() {
                let weight = self.embeddings[[context_id, i]];
                let grad = self.l2_grad(learning_rate * grad_positive * target_val, learning_rate, weight);
                self.accumulate_gradient(accum, context_id, grad, i);
            }
        }

        for (i, &context_val) in context_vector.iter().enumerate() {
            let grad = self.l2_grad(learning_rate * grad_positive * context_val, learning_rate, target_embedding[i]);
            self.accumulate_gradient(accum, target_id, grad, i);
        }

        let mut loss = -prob_positive.ln();
        for &neg_id in negative_samples {
            let neg_embedding: Vec<f32> = self.embeddings.row(neg_id).to_vec();
            let neg_arr = Array::from_shape_vec((self.config.embedding_dim,), neg_embedding.clone()).unwrap();
            let dot_product_neg = context_vector.dot(&neg_arr);
            let prob_negative = sigmoid(dot_product_neg);
            let grad_negative = prob_negative;
            loss += -(1.0 - prob_negative).ln();

            for &context_id in context_ids {
                for (i, &neg_val) in neg_embedding.iter().enumerate() {
                    let weight = self.embeddings[[context_id, i]];
                    let grad = self.l2_grad(learning_rate * grad_negative * neg_val, learning_rate, weight);
                    self.accumulate_gradient(accum, context_id, grad, i);
                }
            }

            for (i, &context_val) in context_vector.iter().enumerate() {
                let grad = self.l2_grad(learning_rate * grad_negative * context_val, learning_rate, neg_embedding[i]);
                self.accumulate_gradient(accum, neg_id, grad, i);
            }
        }
        loss
    }

    fn get_learning_rate(&self, epoch: usize, total_epochs: usize) -> f32 {
        match self.config.lr_schedule {
            crate::config::LearningRateSchedule::Constant => self.config.learning_rate as f32,
            crate::config::LearningRateSchedule::Exponential { decay_rate } => {
                (self.config.learning_rate * decay_rate.powi(epoch as i32)) as f32
            }
            crate::config::LearningRateSchedule::Step { step_size, gamma } => {
                let num_steps = epoch / step_size;
                (self.config.learning_rate * gamma.powi(num_steps as i32)) as f32
            }
            crate::config::LearningRateSchedule::Cosine { t_max } => {
                let t = epoch as f32 / std::cmp::min(t_max, total_epochs) as f32;
                let lr = 0.5 * (1.0 + (std::f32::consts::PI * t).cos());
                self.config.learning_rate as f32 * lr
            }
        }
    }
    
    fn should_early_stop(&self, _epoch: usize, current_loss: f32, best_loss: f32, patience_counter: &mut usize) -> bool {
        if let Some(config) = &self.config.early_stopping {
            if current_loss < best_loss - config.min_delta as f32 {
                *patience_counter = 0;
                false
            } else {
                *patience_counter += 1;
                *patience_counter >= config.patience
            }
        } else {
            false
        }
    }
    
    fn l2_grad(&self, grad: f32, learning_rate: f32, weight: f32) -> f32 {
        if let Some(l2_reg) = self.config.l2_regularization {
            grad + learning_rate * l2_reg as f32 * weight
        } else {
            grad
        }
    }

    fn clip_gradient(&self, grad: f32) -> f32 {
        if let Some(max_norm) = self.config.gradient_clip {
            grad.clamp(-max_norm, max_norm)
        } else {
            grad
        }
    }
}

fn sigmoid(x: f32) -> f32 {
    1.0 / (1.0 + (-x).exp())
}

fn get_negative_samples(vocab_size: usize, num_samples: usize, target_id: usize, rng: &mut rand::rngs::ThreadRng) -> Vec<usize> {
    let mut samples = Vec::new();
    let dist = Uniform::new(0, vocab_size);

    while samples.len() < num_samples {
        let candidate = rng.sample(dist);
        if candidate != target_id && !samples.contains(&candidate) {
            samples.push(candidate);
        }
    }

    samples
}

/// Supports real-time incremental training by updating a model with new
/// sentences as they arrive, without requiring a full retrain.
pub struct IncrementalTrainer;

impl IncrementalTrainer {
    /// Updates `model` by training on a batch of new sentences for a small
    /// number of epochs. New vocabulary words are added automatically.
    ///
    /// Returns the updated `TrainingData` reflecting any new vocabulary.
    pub fn update(
        model: &mut EmbeddingModel,
        data: &mut TrainingData,
        new_sentences: &[Vec<String>],
        mini_epochs: usize,
    ) -> Result<(), String> {
        // Update vocabulary with any new words
        let new_words: Vec<String> = new_sentences
            .iter()
            .flat_map(|s| s.iter().cloned())
            .collect::<std::collections::HashSet<String>>()
            .into_iter()
            .collect();
        model.incremental_vocab_update(&new_words, data)?;

        // Append new sentences to existing data
        data.sentences.extend(new_sentences.iter().cloned());

        // Temporarily reduce epochs for quick incremental update
        let original_epochs = model.config.epochs;
        model.config.epochs = mini_epochs;
        model.train(data)?;
        model.config.epochs = original_epochs;

        Ok(())
    }

    /// Streams sentences from an iterator and trains the model incrementally
    /// in micro-batches. Useful for real-time data feeds.
    pub fn stream_train<I>(
        model: &mut EmbeddingModel,
        data: &mut TrainingData,
        sentences: I,
        batch_size: usize,
        mini_epochs: usize,
    ) -> Result<(), String>
    where
        I: Iterator<Item = Vec<String>>,
    {
        let mut batch = Vec::with_capacity(batch_size);
        for sentence in sentences {
            batch.push(sentence);
            if batch.len() >= batch_size {
                Self::update(model, data, &batch, mini_epochs)?;
                batch.clear();
            }
        }
        if !batch.is_empty() {
            Self::update(model, data, &batch, mini_epochs)?;
        }
        Ok(())
    }
}