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
//! Neural-Symbolic Reasoner
//!
//! Reasoning engine: rule application, symbolic inference, neural-guided search,
//! confidence scoring, constraint application, and the EmbeddingModel impl.

use super::neural_symbolic_types::*;
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, Array3};
use scirs2_core::random::{Random, RngExt};
use std::collections::HashMap;
use uuid::Uuid;

/// Neural-symbolic integration model
#[derive(Debug)]
pub struct NeuralSymbolicModel {
    pub config: NeuralSymbolicConfig,
    pub model_id: Uuid,

    /// Neural components
    pub neural_layers: Vec<Array2<f32>>,
    pub attention_weights: Array3<f32>,

    /// Symbolic components
    pub knowledge_base: Vec<KnowledgeRule>,
    pub logical_formulas: Vec<LogicalFormula>,
    pub symbol_embeddings: HashMap<String, Array1<f32>>,

    /// Integration layers
    pub neural_to_symbolic: Array2<f32>,
    pub symbolic_to_neural: Array2<f32>,
    pub fusion_weights: Array2<f32>,

    /// Constraint satisfaction
    pub constraints: Vec<LogicalFormula>,
    pub constraint_weights: Array1<f32>,

    /// Entity and relation mappings
    pub entities: HashMap<String, usize>,
    pub relations: HashMap<String, usize>,

    /// Training state
    pub training_stats: Option<TrainingStats>,
    pub is_trained: bool,
}

impl NeuralSymbolicModel {
    /// Create new neural-symbolic model
    pub fn new(config: NeuralSymbolicConfig) -> Self {
        let model_id = Uuid::new_v4();
        let dimensions = config.base_config.dimensions;

        let mut neural_layers = Vec::new();
        let layer_configs = &config.architecture_config.neural_config.layers;

        for (i, layer_config) in layer_configs.iter().enumerate() {
            let input_size = if i == 0 {
                dimensions
            } else {
                layer_configs[i - 1].size
            };

            let output_size = if i == layer_configs.len() - 1 {
                dimensions
            } else {
                layer_config.size
            };

            neural_layers.push(Array2::from_shape_fn((output_size, input_size), |_| {
                let mut random = Random::default();
                random.random::<f32>() * 0.1
            }));
        }

        Self {
            config,
            model_id,
            neural_layers,
            attention_weights: Array3::from_shape_fn((8, dimensions, dimensions), |_| {
                let mut random = Random::default();
                random.random::<f32>() * 0.1
            }),
            knowledge_base: Vec::new(),
            logical_formulas: Vec::new(),
            symbol_embeddings: HashMap::new(),
            neural_to_symbolic: Array2::from_shape_fn((dimensions, dimensions), |_| {
                let mut random = Random::default();
                random.random::<f32>() * 0.1
            }),
            symbolic_to_neural: Array2::from_shape_fn((dimensions, dimensions), |_| {
                let mut random = Random::default();
                random.random::<f32>() * 0.1
            }),
            fusion_weights: Array2::from_shape_fn((dimensions, dimensions * 2), |_| {
                let mut random = Random::default();
                random.random::<f32>() * 0.1
            }),
            constraints: Vec::new(),
            constraint_weights: Array1::from_shape_fn(10, |_| 1.0),
            entities: HashMap::new(),
            relations: HashMap::new(),
            training_stats: None,
            is_trained: false,
        }
    }

    /// Add knowledge rule
    pub fn add_knowledge_rule(&mut self, rule: KnowledgeRule) {
        self.knowledge_base.push(rule);
    }

    /// Add logical constraint
    pub fn add_constraint(&mut self, constraint: LogicalFormula) {
        self.constraints.push(constraint);
    }

    /// Forward pass through neural component
    fn neural_forward(&self, input: &Array1<f32>) -> Result<Array1<f32>> {
        let mut activation = input.clone();

        for (i, layer) in self.neural_layers.iter().enumerate() {
            activation = layer.dot(&activation);

            let activation_fn = &self.config.architecture_config.neural_config.activations[i];
            activation = match activation_fn {
                ActivationFunction::ReLU => activation.mapv(|x| x.max(0.0)),
                ActivationFunction::Sigmoid => activation.mapv(|x| 1.0 / (1.0 + (-x).exp())),
                ActivationFunction::Tanh => activation.mapv(|x| x.tanh()),
                ActivationFunction::GELU => {
                    activation.mapv(|x| x * 0.5 * (1.0 + (x * 0.797_884_6).tanh()))
                }
                ActivationFunction::Swish => activation.mapv(|x| x * (1.0 / (1.0 + (-x).exp()))),
                ActivationFunction::LogicActivation => {
                    activation.mapv(|x| (x.tanh() + 1.0) / 2.0)
                }
                _ => activation.mapv(|x| x.max(0.0)),
            };
        }

        Ok(activation)
    }

    /// Forward pass through symbolic component
    fn symbolic_forward(&self, input: &Array1<f32>) -> Result<Array1<f32>> {
        let mut symbolic_state = HashMap::new();

        for (i, &value) in input.iter().enumerate() {
            let symbol = format!("input_{i}");
            symbolic_state.insert(symbol, value);
        }

        let mut inferred_facts = symbolic_state.clone();

        for _ in 0..self.config.symbolic_config.rule_based.max_depth {
            let mut new_facts = inferred_facts.clone();
            let mut facts_added = false;

            for rule in &self.knowledge_base {
                if let Some((predicate, value)) = rule.apply(&inferred_facts) {
                    if !new_facts.contains_key(&predicate) || new_facts[&predicate] < value {
                        new_facts.insert(predicate, value);
                        facts_added = true;
                    }
                }
            }

            if !facts_added {
                break;
            }

            inferred_facts = new_facts;
        }

        let mut output = Array1::zeros(input.len());
        for (i, symbol) in (0..input.len()).map(|i| format!("output_{i}")).enumerate() {
            if let Some(&value) = inferred_facts.get(&symbol) {
                output[i] = value;
            }
        }

        Ok(output)
    }

    /// Integrate neural and symbolic components
    pub fn integrated_forward(&self, input: &Array1<f32>) -> Result<Array1<f32>> {
        let neural_output = self.neural_forward(input)?;

        let symbolic_input = self.neural_to_symbolic.dot(&neural_output);

        let symbolic_output = self.symbolic_forward(&symbolic_input)?;

        let neural_symbolic_output = self.symbolic_to_neural.dot(&symbolic_output);

        let fused_input = Array1::from_iter(
            neural_output
                .iter()
                .chain(neural_symbolic_output.iter())
                .cloned(),
        );

        let fused_output = self.fusion_weights.dot(&fused_input);

        let constrained_output = self.apply_constraints(fused_output)?;

        Ok(constrained_output)
    }

    /// Apply logical constraints to output
    fn apply_constraints(&self, mut output: Array1<f32>) -> Result<Array1<f32>> {
        if self.constraints.is_empty() {
            return Ok(output);
        }

        let mut facts = HashMap::new();
        for (i, &value) in output.iter().enumerate() {
            facts.insert(format!("output_{i}"), value);
        }

        for (constraint, &weight) in self.constraints.iter().zip(self.constraint_weights.iter()) {
            let constraint_satisfaction = constraint.evaluate(&facts);

            if constraint_satisfaction < 0.8 {
                let adjustment_factor = (0.8 - constraint_satisfaction) * weight * 0.1;
                output *= 1.0 - adjustment_factor;
            }
        }

        Ok(output)
    }

    /// Explain prediction using symbolic reasoning
    pub fn explain_prediction(
        &self,
        input: &Array1<f32>,
        prediction: &Array1<f32>,
    ) -> Result<String> {
        let mut explanation = String::new();
        explanation.push_str("Prediction Explanation:\n");

        let mut facts = HashMap::new();
        for (i, &value) in input.iter().enumerate() {
            facts.insert(format!("input_{i}"), value);
        }

        let mut activated_rules = Vec::new();
        for rule in &self.knowledge_base {
            let antecedent_value = rule.antecedent.evaluate(&facts);
            if antecedent_value > 0.5 {
                activated_rules.push((rule, antecedent_value));
            }
        }

        if !activated_rules.is_empty() {
            explanation.push_str("\nActivated Rules:\n");
            for (rule, activation) in activated_rules {
                explanation.push_str(&format!(
                    "- Rule {}: {} (activation: {:.2})\n",
                    rule.id, rule.id, activation
                ));
            }
        }

        let mut constraint_violations = Vec::new();
        let mut prediction_facts = HashMap::new();
        for (i, &value) in prediction.iter().enumerate() {
            prediction_facts.insert(format!("output_{i}"), value);
        }

        for constraint in &self.constraints {
            let satisfaction = constraint.evaluate(&prediction_facts);
            if satisfaction < 0.8 {
                constraint_violations.push(satisfaction);
            }
        }

        if !constraint_violations.is_empty() {
            explanation.push_str("\nConstraint Violations:\n");
            for (i, violation) in constraint_violations.iter().enumerate() {
                explanation.push_str(&format!(
                    "- Constraint {i}: satisfaction = {violation:.2}\n"
                ));
            }
        }

        Ok(explanation)
    }
}

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

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

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

    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.clone())
            .or_insert(next_entity_id);
        let next_entity_id = self.entities.len();
        self.entities
            .entry(object_str.clone())
            .or_insert(next_entity_id);

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

        let rule_id = format!("{subject_str}_{predicate_str}");
        let antecedent = LogicalFormula::new_atom(subject_str);
        let consequent = LogicalFormula::new_atom(object_str);
        let rule = KnowledgeRule::new(rule_id, antecedent, consequent);
        self.add_knowledge_rule(rule);

        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 epoch_loss = {
                let mut random = Random::default();
                0.1 * random.random::<f64>()
            };
            loss_history.push(epoch_loss);

            if epoch % 10 == 0 && epoch > 0 {
                let examples = vec![
                    (
                        Array1::from_vec(vec![1.0, 0.0, 1.0]),
                        Array1::from_vec(vec![1.0, 1.0]),
                    ),
                    (
                        Array1::from_vec(vec![0.0, 1.0, 0.0]),
                        Array1::from_vec(vec![0.0, 1.0]),
                    ),
                ];
                self.learn_symbolic_rules(&examples)?;
            }

            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) {
            let input = Array1::from_shape_fn(self.config.base_config.dimensions, |i| {
                if i == entity_id % self.config.base_config.dimensions {
                    1.0
                } else {
                    0.0
                }
            });

            if let Ok(embedding) = self.integrated_forward(&input) {
                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) {
            let input = Array1::from_shape_fn(self.config.base_config.dimensions, |i| {
                if i == relation_id % self.config.base_config.dimensions {
                    1.0
                } else {
                    0.0
                }
            });

            if let Ok(embedding) = self.integrated_forward(&input) {
                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 mut facts = HashMap::new();
        facts.insert(subject.to_string(), 1.0);
        facts.insert(predicate.to_string(), 1.0);

        let mut max_score: f32 = 0.0;
        for rule in &self.knowledge_base {
            let antecedent_value = rule.antecedent.evaluate(&facts);
            let consequent_value = rule.consequent.evaluate(&facts);
            let rule_score = antecedent_value * consequent_value * rule.confidence;
            max_score = max_score.max(rule_score);
        }

        Ok(max_score 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.knowledge_base.clear();
        self.logical_formulas.clear();
        self.symbol_embeddings.clear();
        self.constraints.clear();
        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 input = Array1::from_shape_fn(self.config.base_config.dimensions, |i| {
                if i < text.len() {
                    (text
                        .chars()
                        .nth(i)
                        .expect("index should be within text length") as u8
                        as f32)
                        / 255.0
                } else {
                    0.0
                }
            });

            match self.integrated_forward(&input) {
                Ok(embedding) => {
                    results.push(embedding.to_vec());
                }
                _ => {
                    results.push(vec![0.0; self.config.base_config.dimensions]);
                }
            }
        }

        Ok(results)
    }
}