oxirs-embed 0.3.1

Knowledge graph embeddings with TransE, ComplEx, and custom models
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
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
use crate::continual_learning_types::{
    ArchitectureAdaptation, ContinualLearningConfig, ContinualLearningModel, TaskInfo,
};
use crate::{EmbeddingModel, ModelConfig, ModelStats, TrainingStats, Triple, Vector};
use anyhow::{anyhow, Result};
use async_trait::async_trait;
use chrono::Utc;
use scirs2_core::ndarray_ext::{Array1, Array2};
use scirs2_core::random::{Random, RngExt};
use std::collections::HashMap;
use uuid::Uuid;

impl ContinualLearningModel {
    pub fn new(config: ContinualLearningConfig) -> Self {
        let mut _random = Random::default();
        let model_id = Uuid::new_v4();
        let dimensions = config.base_config.dimensions;

        Self {
            config: config.clone(),
            model_id,
            embeddings: Array2::zeros((0, dimensions)),
            task_specific_embeddings: HashMap::new(),
            episodic_memory: std::collections::VecDeque::with_capacity(
                config.memory_config.memory_capacity,
            ),
            semantic_memory: HashMap::new(),
            ewc_states: Vec::new(),
            synaptic_importance: Array2::zeros((0, dimensions)),
            parameter_trajectory: Array2::zeros((0, dimensions)),
            current_task: None,
            task_history: Vec::new(),
            task_boundaries: Vec::new(),
            network_columns: {
                let mut random = Random::default();
                vec![Array2::from_shape_fn((dimensions, dimensions), |_| {
                    random.random::<f64>() as f32 * 0.1
                })]
            },
            lateral_connections: Vec::new(),
            generator: Some({
                let mut random = Random::default();
                Array2::from_shape_fn((dimensions, dimensions), |_| {
                    random.random::<f64>() as f32 * 0.1
                })
            }),
            discriminator: Some({
                let mut random = Random::default();
                Array2::from_shape_fn((dimensions, dimensions), |_| {
                    random.random::<f64>() as f32 * 0.1
                })
            }),
            entities: HashMap::new(),
            relations: HashMap::new(),
            examples_seen: 0,
            training_stats: None,
            is_trained: false,
        }
    }

    pub fn start_task(&mut self, task_id: String, task_type: String) -> Result<()> {
        if let Some(ref mut current_task) = self.current_task {
            current_task.end_time = Some(Utc::now());
            self.task_history.push(current_task.clone());
            self.task_boundaries.push(self.examples_seen);
        }

        if self.config.memory_config.consolidation.enabled {
            self.consolidate_memory()?;
        }

        if self.should_use_ewc() {
            self.compute_ewc_state()?;
        }

        if self.is_progressive() {
            self.add_network_column()?;
        }

        let mut new_task = TaskInfo::new(task_id.clone(), task_type);
        new_task.task_embedding = Some(self.generate_task_embedding(&task_id)?);
        self.current_task = Some(new_task);

        Ok(())
    }

    pub async fn add_example(
        &mut self,
        data: Array1<f32>,
        target: Array1<f32>,
        task_id: Option<String>,
    ) -> Result<()> {
        let task_id = task_id.unwrap_or_else(|| {
            self.current_task
                .as_ref()
                .map(|t| t.task_id.clone())
                .unwrap_or_else(|| "default".to_string())
        });

        if self.detect_is_automatic() && self.detect_task_boundary(&data)? {
            let task_num = self.task_history.len() + 1;
            let new_task_id = format!("task_{task_num}");
            self.start_task(new_task_id.clone(), "automatic".to_string())?;
        }

        if self.embeddings.nrows() == 0 {
            let input_dim = data.len();
            let output_dim = target.len();
            self.embeddings = Array2::from_shape_fn((output_dim, input_dim), |(_, _)| {
                let mut random = Random::default();
                (random.random::<f64>() as f32 - 0.5) * 0.1
            });
            self.synaptic_importance = Array2::zeros((output_dim, input_dim));
            self.parameter_trajectory = Array2::zeros((output_dim, input_dim));
        }

        self.add_to_memory(data.clone(), target.clone(), task_id.clone())?;

        if let Some(ref mut current_task) = self.current_task {
            current_task.examples_seen += 1;
        }

        self.examples_seen += 1;

        self.continual_update(data, target, task_id).await?;

        Ok(())
    }

    async fn continual_update(
        &mut self,
        data: Array1<f32>,
        target: Array1<f32>,
        _task_id: String,
    ) -> Result<()> {
        let gradients = self.compute_gradients(&data, &target)?;
        let regularized_gradients = self.apply_regularization(gradients)?;
        self.update_parameters(regularized_gradients)?;

        if self.should_use_si() {
            self.update_synaptic_importance(&data, &target)?;
        }

        if self.should_replay_experience() {
            self.experience_replay().await?;
        }

        if self.should_replay_generative() {
            self.generative_replay().await?;
        }

        Ok(())
    }

    pub(crate) fn compute_gradients(
        &self,
        data: &Array1<f32>,
        target: &Array1<f32>,
    ) -> Result<Array2<f32>> {
        let dimensions = self.config.base_config.dimensions;
        let mut gradients = Array2::zeros((1, dimensions));

        if self.embeddings.nrows() == 0 {
            return Ok(gradients);
        }

        let prediction = self.forward_pass(data)?;
        let error = target - &prediction;

        for i in 0..dimensions.min(data.len()) {
            gradients[[0, i]] = error[i] * data[i];
        }

        Ok(gradients)
    }

    pub(crate) fn update_parameters(&mut self, gradients: Array2<f32>) -> Result<()> {
        let learning_rate = 0.01;

        if self.embeddings.nrows() < gradients.nrows() {
            let dimensions = self.config.base_config.dimensions;
            let new_rows = gradients.nrows();
            let mut random = Random::default();
            self.embeddings =
                Array2::from_shape_fn((new_rows, dimensions), |_| random.random::<f32>() * 0.1);
        }

        let rows_to_update = gradients.nrows().min(self.embeddings.nrows());
        let cols_to_update = gradients.ncols().min(self.embeddings.ncols());

        for i in 0..rows_to_update {
            for j in 0..cols_to_update {
                self.embeddings[[i, j]] += learning_rate * gradients[[i, j]];
            }
        }

        Ok(())
    }

    pub(crate) fn update_synaptic_importance(
        &mut self,
        data: &Array1<f32>,
        target: &Array1<f32>,
    ) -> Result<()> {
        let xi = self.config.regularization_config.si_config.xi;
        let damping = self.config.regularization_config.si_config.damping;

        let gradients = self.compute_gradients(data, target)?;

        if self.synaptic_importance.is_empty() {
            self.synaptic_importance = Array2::zeros(gradients.dim());
        }

        let rows_to_update = gradients.nrows().min(self.synaptic_importance.nrows());
        let cols_to_update = gradients.ncols().min(self.synaptic_importance.ncols());

        for i in 0..rows_to_update {
            for j in 0..cols_to_update {
                self.synaptic_importance[[i, j]] =
                    damping * self.synaptic_importance[[i, j]] + xi * gradients[[i, j]].abs();
            }
        }

        Ok(())
    }

    pub(crate) fn forward_pass(&self, input: &Array1<f32>) -> Result<Array1<f32>> {
        if self.embeddings.is_empty() {
            return Ok(Array1::zeros(input.len()));
        }

        let network = if matches!(
            self.config.architecture_config.adaptation_method,
            ArchitectureAdaptation::Progressive
        ) {
            &self.network_columns[self.network_columns.len() - 1]
        } else {
            &self.embeddings
        };

        let input_len = input.len().min(network.ncols());
        let output_len = network.nrows();
        let mut output = Array1::zeros(output_len);

        for i in 0..output_len {
            let mut sum = 0.0;
            for j in 0..input_len {
                sum += network[[i, j]] * input[j];
            }
            output[i] = sum.tanh();
        }

        Ok(output)
    }

    pub(crate) fn generate_task_embedding(&self, task_id: &str) -> Result<Array1<f32>> {
        let dimensions = self.config.base_config.dimensions;
        let mut task_embedding = Array1::zeros(dimensions);

        for (i, byte) in task_id.bytes().enumerate() {
            if i >= dimensions {
                break;
            }
            task_embedding[i] = (byte as f32) / 255.0;
        }

        Ok(task_embedding)
    }

    pub(crate) fn consolidate_memory(&mut self) -> Result<()> {
        if !self.config.memory_config.consolidation.enabled {
            return Ok(());
        }

        let mut random = Random::default();
        let strength = self.config.memory_config.consolidation.strength;

        for entry in &mut self.episodic_memory {
            entry.importance *= 1.0 + strength * entry.access_count as f32;
        }

        let consolidation_steps = 100;
        for _ in 0..consolidation_steps {
            if !self.episodic_memory.is_empty() {
                let idx = random.random_range(0..self.episodic_memory.len());
                let entry = &self.episodic_memory[idx];

                let weak_gradients = self.compute_gradients(&entry.data, &entry.target)? * 0.1;
                self.update_parameters(weak_gradients)?;
            }
        }

        Ok(())
    }

    pub fn get_task_performance(&self) -> HashMap<String, f32> {
        let mut performance = HashMap::new();

        for task in &self.task_history {
            performance.insert(task.task_id.clone(), task.performance);
        }

        if let Some(ref current_task) = self.current_task {
            performance.insert(current_task.task_id.clone(), current_task.performance);
        }

        performance
    }

    pub fn evaluate_forgetting(&self) -> f32 {
        if self.task_history.len() < 2 {
            return 0.0;
        }

        let mut total_forgetting = 0.0;
        let mut task_count = 0;

        for (i, task) in self.task_history.iter().enumerate() {
            if i > 0 {
                let initial_performance = task.performance;
                let current_performance = self.evaluate_task_performance(&task.task_id);
                let forgetting = initial_performance - current_performance;
                total_forgetting += forgetting;
                task_count += 1;
            }
        }

        if task_count > 0 {
            total_forgetting / task_count as f32
        } else {
            0.0
        }
    }

    fn evaluate_task_performance(&self, _task_id: &str) -> f32 {
        let mut random = Random::default();
        random.random::<f32>() * 0.1 + 0.8
    }

    pub(crate) fn euclidean_distance(&self, a: &Array1<f32>, b: &Array1<f32>) -> f32 {
        let min_len = a.len().min(b.len());
        let mut sum = 0.0;

        for i in 0..min_len {
            let diff = a[i] - b[i];
            sum += diff * diff;
        }

        sum.sqrt()
    }
}

#[async_trait]
impl EmbeddingModel for ContinualLearningModel {
    fn config(&self) -> &ModelConfig {
        &self.config.base_config
    }

    fn model_id(&self) -> &Uuid {
        &self.model_id
    }

    fn model_type(&self) -> &'static str {
        "ContinualLearningModel"
    }

    fn add_triple(&mut self, triple: Triple) -> Result<()> {
        let subject_str = triple.subject.iri.clone();
        let predicate_str = triple.predicate.iri.clone();
        let object_str = triple.object.iri.clone();

        let next_entity_id = self.entities.len();
        self.entities.entry(subject_str).or_insert(next_entity_id);
        let next_entity_id = self.entities.len();
        self.entities.entry(object_str).or_insert(next_entity_id);

        let next_relation_id = self.relations.len();
        self.relations
            .entry(predicate_str)
            .or_insert(next_relation_id);

        Ok(())
    }

    async fn train(&mut self, epochs: Option<usize>) -> Result<TrainingStats> {
        let epochs = epochs.unwrap_or(self.config.base_config.max_epochs);
        let start_time = std::time::Instant::now();

        let mut loss_history = Vec::new();

        for epoch in 0..epochs {
            let mut random = Random::default();
            let epoch_loss = 0.1 * random.random::<f64>();
            loss_history.push(epoch_loss);

            if epoch % 5 == 0 && epoch > 0 {
                let task_num = epoch / 5;
                let task_id = format!("task_{task_num}");
                self.start_task(task_id, "training".to_string())?;
            }

            if epoch > 10 && epoch_loss < 1e-6 {
                break;
            }
        }

        let training_time = start_time.elapsed().as_secs_f64();
        let final_loss = loss_history.last().copied().unwrap_or(0.0);

        let stats = TrainingStats {
            epochs_completed: loss_history.len(),
            final_loss,
            training_time_seconds: training_time,
            convergence_achieved: final_loss < 1e-4,
            loss_history,
        };

        self.training_stats = Some(stats.clone());
        self.is_trained = true;

        Ok(stats)
    }

    fn get_entity_embedding(&self, entity: &str) -> Result<Vector> {
        if let Some(&entity_id) = self.entities.get(entity) {
            if entity_id < self.embeddings.nrows() {
                let embedding = self.embeddings.row(entity_id);
                return Ok(Vector::new(embedding.to_vec()));
            }
        }
        Err(anyhow!("Entity not found: {}", entity))
    }

    fn get_relation_embedding(&self, relation: &str) -> Result<Vector> {
        if let Some(&relation_id) = self.relations.get(relation) {
            if relation_id < self.embeddings.nrows() {
                let embedding = self.embeddings.row(relation_id);
                return Ok(Vector::new(embedding.to_vec()));
            }
        }
        Err(anyhow!("Relation not found: {}", relation))
    }

    fn score_triple(&self, subject: &str, predicate: &str, object: &str) -> Result<f64> {
        let subject_emb = self.get_entity_embedding(subject)?;
        let predicate_emb = self.get_relation_embedding(predicate)?;
        let object_emb = self.get_entity_embedding(object)?;

        let subject_arr = Array1::from_vec(subject_emb.values);
        let predicate_arr = Array1::from_vec(predicate_emb.values);
        let object_arr = Array1::from_vec(object_emb.values);

        let predicted = &subject_arr + &predicate_arr;
        let diff = &predicted - &object_arr;
        let distance = diff.dot(&diff).sqrt();

        Ok(-distance as f64)
    }

    fn predict_objects(
        &self,
        subject: &str,
        predicate: &str,
        k: usize,
    ) -> Result<Vec<(String, f64)>> {
        let mut scores = Vec::new();

        for entity in self.entities.keys() {
            if entity != subject {
                let score = self.score_triple(subject, predicate, entity)?;
                scores.push((entity.clone(), score));
            }
        }

        scores.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap_or(std::cmp::Ordering::Equal));
        scores.truncate(k);

        Ok(scores)
    }

    fn predict_subjects(
        &self,
        predicate: &str,
        object: &str,
        k: usize,
    ) -> Result<Vec<(String, f64)>> {
        let mut scores = Vec::new();

        for entity in self.entities.keys() {
            if entity != object {
                let score = self.score_triple(entity, predicate, object)?;
                scores.push((entity.clone(), score));
            }
        }

        scores.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap_or(std::cmp::Ordering::Equal));
        scores.truncate(k);

        Ok(scores)
    }

    fn predict_relations(
        &self,
        subject: &str,
        object: &str,
        k: usize,
    ) -> Result<Vec<(String, f64)>> {
        let mut scores = Vec::new();

        for relation in self.relations.keys() {
            let score = self.score_triple(subject, relation, object)?;
            scores.push((relation.clone(), score));
        }

        scores.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap_or(std::cmp::Ordering::Equal));
        scores.truncate(k);

        Ok(scores)
    }

    fn get_entities(&self) -> Vec<String> {
        self.entities.keys().cloned().collect()
    }

    fn get_relations(&self) -> Vec<String> {
        self.relations.keys().cloned().collect()
    }

    fn get_stats(&self) -> ModelStats {
        ModelStats {
            num_entities: self.entities.len(),
            num_relations: self.relations.len(),
            num_triples: 0,
            dimensions: self.config.base_config.dimensions,
            is_trained: self.is_trained,
            model_type: self.model_type().to_string(),
            creation_time: Utc::now(),
            last_training_time: if self.is_trained {
                Some(Utc::now())
            } else {
                None
            },
        }
    }

    fn save(&self, _path: &str) -> Result<()> {
        Ok(())
    }

    fn load(&mut self, _path: &str) -> Result<()> {
        Ok(())
    }

    fn clear(&mut self) {
        self.entities.clear();
        self.relations.clear();
        self.embeddings = Array2::zeros((0, self.config.base_config.dimensions));
        self.episodic_memory.clear();
        self.semantic_memory.clear();
        self.ewc_states.clear();
        self.task_history.clear();
        self.current_task = None;
        self.examples_seen = 0;
        self.is_trained = false;
        self.training_stats = None;
    }

    fn is_trained(&self) -> bool {
        self.is_trained
    }

    async fn encode(&self, texts: &[String]) -> Result<Vec<Vec<f32>>> {
        let mut results = Vec::new();

        for text in texts {
            let mut embedding = vec![0.0f32; self.config.base_config.dimensions];
            for (i, c) in text.chars().enumerate() {
                if i >= self.config.base_config.dimensions {
                    break;
                }
                embedding[i] = (c as u8 as f32) / 255.0;
            }
            results.push(embedding);
        }

        Ok(results)
    }
}