torsh-graph 0.1.3

Graph neural network components for ToRSh - powered by SciRS2
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
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
//! Heterogeneous Graph Neural Networks
//!
//! Implementation of multi-relational GNNs for heterogeneous graphs
//! with different node types and edge types, as specified in TODO.md

// Framework infrastructure - components designed for future use
#![allow(dead_code)]
use crate::parameter::Parameter;
use std::collections::HashMap;
use torsh_tensor::{
    creation::{randn, zeros},
    Tensor,
};

/// Node type identifier
pub type NodeType = String;

/// Edge type identifier (source_type, relation, target_type)
pub type EdgeType = (NodeType, String, NodeType);

/// Heterogeneous graph data structure
#[derive(Debug, Clone)]
pub struct HeteroGraphData {
    /// Node features for each node type
    pub node_features: HashMap<NodeType, Tensor>,
    /// Edge indices for each edge type
    pub edge_indices: HashMap<EdgeType, Tensor>,
    /// Edge attributes for each edge type (optional)
    pub edge_attributes: HashMap<EdgeType, Option<Tensor>>,
    /// Number of nodes per type
    pub num_nodes: HashMap<NodeType, usize>,
}

impl HeteroGraphData {
    /// Create a new heterogeneous graph
    pub fn new() -> Self {
        Self {
            node_features: HashMap::new(),
            edge_indices: HashMap::new(),
            edge_attributes: HashMap::new(),
            num_nodes: HashMap::new(),
        }
    }

    /// Add node type with features
    pub fn add_node_type(&mut self, node_type: NodeType, features: Tensor) -> &mut Self {
        let num_nodes = features.shape().dims()[0];
        self.node_features.insert(node_type.clone(), features);
        self.num_nodes.insert(node_type, num_nodes);
        self
    }

    /// Add edge type with indices
    pub fn add_edge_type(
        &mut self,
        edge_type: EdgeType,
        edge_index: Tensor,
        edge_attr: Option<Tensor>,
    ) -> &mut Self {
        self.edge_indices.insert(edge_type.clone(), edge_index);
        self.edge_attributes.insert(edge_type, edge_attr);
        self
    }

    /// Get all node types
    pub fn node_types(&self) -> Vec<&NodeType> {
        self.node_features.keys().collect()
    }

    /// Get all edge types
    pub fn edge_types(&self) -> Vec<&EdgeType> {
        self.edge_indices.keys().collect()
    }
}

/// Heterogeneous Graph Neural Network layer
#[derive(Debug)]
pub struct HeteroGNN {
    node_types: Vec<NodeType>,
    edge_types: Vec<EdgeType>,
    /// Type-specific transformation layers
    node_transformations: HashMap<NodeType, Parameter>,
    /// Relation-specific message functions
    edge_transformations: HashMap<EdgeType, Parameter>,
    /// Output dimension
    out_features: usize,
    /// Whether to use bias
    bias: bool,
    /// Bias parameters per node type
    biases: HashMap<NodeType, Option<Parameter>>,
}

impl HeteroGNN {
    /// Create a new heterogeneous GNN layer
    pub fn new(
        node_type_dims: HashMap<NodeType, usize>,
        edge_types: Vec<EdgeType>,
        out_features: usize,
        bias: bool,
    ) -> Self {
        let mut node_transformations = HashMap::new();
        let mut biases = HashMap::new();

        // Create transformation matrices for each node type
        for (node_type, in_features) in &node_type_dims {
            let weight = Parameter::new(
                randn(&[*in_features, out_features])
                    .expect("failed to create node transformation weights"),
            );
            node_transformations.insert(node_type.clone(), weight);

            let bias_param = if bias {
                Some(Parameter::new(
                    zeros(&[out_features]).expect("failed to create bias tensor"),
                ))
            } else {
                None
            };
            biases.insert(node_type.clone(), bias_param);
        }

        // Create edge transformation matrices
        let mut edge_transformations = HashMap::new();
        for edge_type in &edge_types {
            // Use output features as the message dimension
            let weight = Parameter::new(
                randn(&[out_features, out_features])
                    .expect("failed to create edge transformation weights"),
            );
            edge_transformations.insert(edge_type.clone(), weight);
        }

        Self {
            node_types: node_type_dims.keys().cloned().collect(),
            edge_types,
            node_transformations,
            edge_transformations,
            out_features,
            bias,
            biases,
        }
    }

    /// Forward pass through heterogeneous GNN
    pub fn forward(&self, hetero_graph: &HeteroGraphData) -> HeteroGraphData {
        let mut output_features = HashMap::new();

        // Step 1: Transform node features for each node type
        let mut transformed_features = HashMap::new();
        for node_type in &self.node_types {
            if let Some(features) = hetero_graph.node_features.get(node_type) {
                if let Some(transform) = self.node_transformations.get(node_type) {
                    let mut transformed = features
                        .matmul(&transform.clone_data())
                        .expect("operation should succeed");

                    // Add bias if present
                    if let Some(Some(bias)) = self.biases.get(node_type) {
                        transformed = transformed
                            .add(&bias.clone_data())
                            .expect("operation should succeed");
                    }

                    transformed_features.insert(node_type.clone(), transformed);
                }
            }
        }

        // Step 2: Message passing for each edge type
        let mut aggregated_messages = HashMap::new();

        for edge_type in &self.edge_types {
            let (src_type, relation, dst_type) = edge_type;

            if let (Some(edge_index), Some(src_features), Some(edge_transform)) = (
                hetero_graph.edge_indices.get(edge_type),
                transformed_features.get(src_type),
                self.edge_transformations.get(edge_type),
            ) {
                // Get edge connections
                let edge_flat = edge_index.to_vec().expect("conversion should succeed");
                let num_edges = edge_flat.len() / 2;

                if num_edges > 0 {
                    let src_indices = &edge_flat[0..num_edges];
                    let dst_indices = &edge_flat[num_edges..];

                    // Initialize aggregated messages for destination nodes
                    let dst_num_nodes = hetero_graph.num_nodes.get(dst_type).unwrap_or(&0);
                    let messages = zeros(&[*dst_num_nodes, self.out_features])
                        .expect("failed to create messages tensor");

                    // Compute and aggregate messages
                    for edge_idx in 0..num_edges {
                        let src_node = src_indices[edge_idx] as usize;
                        let dst_node = dst_indices[edge_idx] as usize;

                        // Extract source node features
                        let src_feat = src_features
                            .slice_tensor(0, src_node, src_node + 1)
                            .expect("failed to slice source node features")
                            .squeeze_tensor(0)
                            .expect("failed to squeeze source node features");

                        // Apply relation-specific transformation
                        let message = src_feat
                            .unsqueeze_tensor(0)
                            .expect("failed to unsqueeze source features")
                            .matmul(&edge_transform.clone_data())
                            .expect("failed to apply edge transformation")
                            .squeeze_tensor(0)
                            .expect("failed to squeeze message");

                        // Aggregate to destination node
                        let mut dst_slice = messages
                            .slice_tensor(0, dst_node, dst_node + 1)
                            .expect("failed to slice destination messages");
                        let current_msg = dst_slice
                            .squeeze_tensor(0)
                            .expect("failed to squeeze destination message");
                        let updated_msg =
                            current_msg.add(&message).expect("operation should succeed");
                        let _ = dst_slice.copy_(
                            &updated_msg
                                .unsqueeze_tensor(0)
                                .expect("failed to unsqueeze updated message"),
                        );
                    }

                    // Store aggregated messages
                    aggregated_messages.insert(
                        (src_type.clone(), relation.clone(), dst_type.clone()),
                        messages,
                    );
                }
            }
        }

        // Step 3: Combine self-features with aggregated messages
        for node_type in &self.node_types {
            let mut node_output = if let Some(self_features) = transformed_features.get(node_type) {
                self_features.clone()
            } else {
                continue;
            };

            // Add messages from all relevant edge types
            for edge_type in &self.edge_types {
                let (_, _, dst_type) = edge_type;
                if dst_type == node_type {
                    if let Some(messages) = aggregated_messages.get(edge_type) {
                        node_output = node_output.add(messages).expect("operation should succeed");
                    }
                }
            }

            // Apply activation (ReLU)
            let zero_tensor =
                zeros(node_output.shape().dims()).expect("failed to create zero tensor for ReLU");
            node_output = node_output
                .maximum(&zero_tensor)
                .expect("failed to apply ReLU activation");

            output_features.insert(node_type.clone(), node_output);
        }

        // Create output heterogeneous graph
        let mut output = HeteroGraphData::new();
        output.node_features = output_features;
        output.edge_indices = hetero_graph.edge_indices.clone();
        output.edge_attributes = hetero_graph.edge_attributes.clone();
        output.num_nodes = hetero_graph.num_nodes.clone();

        output
    }

    /// Get all parameters for optimization
    pub fn parameters(&self) -> Vec<Tensor> {
        let mut params = Vec::new();

        // Add node transformation parameters
        for transform in self.node_transformations.values() {
            params.push(transform.clone_data());
        }

        // Add edge transformation parameters
        for transform in self.edge_transformations.values() {
            params.push(transform.clone_data());
        }

        // Add bias parameters
        for bias_opt in self.biases.values() {
            if let Some(bias) = bias_opt {
                params.push(bias.clone_data());
            }
        }

        params
    }
}

/// Heterogeneous Graph Attention Network
#[derive(Debug)]
pub struct HeteroGAT {
    node_types: Vec<NodeType>,
    edge_types: Vec<EdgeType>,
    /// Type-specific query/key/value transformations
    query_transforms: HashMap<NodeType, Parameter>,
    key_transforms: HashMap<NodeType, Parameter>,
    value_transforms: HashMap<NodeType, Parameter>,
    /// Relation-specific attention parameters
    relation_attentions: HashMap<EdgeType, Parameter>,
    /// Attention heads
    heads: usize,
    /// Output features per head
    out_features: usize,
    /// Dropout rate
    dropout: f32,
}

impl HeteroGAT {
    /// Create a new heterogeneous GAT layer
    pub fn new(
        node_type_dims: HashMap<NodeType, usize>,
        edge_types: Vec<EdgeType>,
        out_features: usize,
        heads: usize,
        dropout: f32,
    ) -> Self {
        let mut query_transforms = HashMap::new();
        let mut key_transforms = HashMap::new();
        let mut value_transforms = HashMap::new();

        // Create Q, K, V transformations for each node type
        for (node_type, in_features) in &node_type_dims {
            let q = Parameter::new(
                randn(&[*in_features, heads * out_features])
                    .expect("failed to create query transformation weights"),
            );
            let k = Parameter::new(
                randn(&[*in_features, heads * out_features])
                    .expect("failed to create key transformation weights"),
            );
            let v = Parameter::new(
                randn(&[*in_features, heads * out_features])
                    .expect("failed to create value transformation weights"),
            );

            query_transforms.insert(node_type.clone(), q);
            key_transforms.insert(node_type.clone(), k);
            value_transforms.insert(node_type.clone(), v);
        }

        // Create relation-specific attention parameters
        let mut relation_attentions = HashMap::new();
        for edge_type in &edge_types {
            let attention = Parameter::new(
                randn(&[heads, 2 * out_features]).expect("failed to create attention weights"),
            );
            relation_attentions.insert(edge_type.clone(), attention);
        }

        Self {
            node_types: node_type_dims.keys().cloned().collect(),
            edge_types,
            query_transforms,
            key_transforms,
            value_transforms,
            relation_attentions,
            heads,
            out_features,
            dropout,
        }
    }

    /// Forward pass with heterogeneous attention
    pub fn forward(&self, hetero_graph: &HeteroGraphData) -> HeteroGraphData {
        let mut output_features = HashMap::new();

        // Step 1: Compute Q, K, V for all node types
        let mut queries = HashMap::new();
        let mut keys = HashMap::new();
        let mut values = HashMap::new();

        for node_type in &self.node_types {
            if let Some(features) = hetero_graph.node_features.get(node_type) {
                let q = features
                    .matmul(&self.query_transforms[node_type].clone_data())
                    .expect("operation should succeed");
                let k = features
                    .matmul(&self.key_transforms[node_type].clone_data())
                    .expect("operation should succeed");
                let v = features
                    .matmul(&self.value_transforms[node_type].clone_data())
                    .expect("operation should succeed");

                // Reshape for multi-head attention [num_nodes, heads, out_features]
                let num_nodes = features.shape().dims()[0];
                let q_reshaped = q
                    .view(&[
                        num_nodes as i32,
                        self.heads as i32,
                        self.out_features as i32,
                    ])
                    .expect("view should succeed");
                let k_reshaped = k
                    .view(&[
                        num_nodes as i32,
                        self.heads as i32,
                        self.out_features as i32,
                    ])
                    .expect("view should succeed");
                let v_reshaped = v
                    .view(&[
                        num_nodes as i32,
                        self.heads as i32,
                        self.out_features as i32,
                    ])
                    .expect("view should succeed");

                queries.insert(node_type.clone(), q_reshaped);
                keys.insert(node_type.clone(), k_reshaped);
                values.insert(node_type.clone(), v_reshaped);
            }
        }

        // Step 2: Compute attention and aggregate for each edge type
        for dst_type in &self.node_types {
            let dst_num_nodes = hetero_graph.num_nodes.get(dst_type).unwrap_or(&0);
            let aggregated_output = zeros(&[*dst_num_nodes, self.heads * self.out_features])
                .expect("failed to create aggregated output tensor");

            // Aggregate from all edge types that target this node type
            for edge_type in &self.edge_types {
                let (src_type, _relation, target_type) = edge_type;

                if target_type != dst_type {
                    continue;
                }

                if let (
                    Some(edge_index),
                    Some(_src_queries),
                    Some(_dst_keys),
                    Some(src_values),
                    Some(_attention_params),
                ) = (
                    hetero_graph.edge_indices.get(edge_type),
                    queries.get(src_type),
                    keys.get(dst_type),
                    values.get(src_type),
                    self.relation_attentions.get(edge_type),
                ) {
                    // For simplicity, use mean aggregation with attention weights
                    // In a full implementation, this would compute proper attention scores

                    let edge_flat = edge_index.to_vec().expect("conversion should succeed");
                    let num_edges = edge_flat.len() / 2;

                    if num_edges > 0 {
                        let src_indices = &edge_flat[0..num_edges];
                        let dst_indices = &edge_flat[num_edges..];

                        // Simple aggregation (placeholder for full attention mechanism)
                        for edge_idx in 0..num_edges {
                            let src_node = src_indices[edge_idx] as usize;
                            let dst_node = dst_indices[edge_idx] as usize;

                            // Extract source value for aggregation
                            let src_value = src_values
                                .slice_tensor(0, src_node, src_node + 1)
                                .expect("failed to slice source values")
                                .view(&[1, (self.heads * self.out_features) as i32])
                                .expect("view should succeed")
                                .squeeze_tensor(0)
                                .expect("failed to squeeze source values");

                            // Add to destination (simple sum for now)
                            let mut dst_slice = aggregated_output
                                .slice_tensor(0, dst_node, dst_node + 1)
                                .expect("failed to slice destination for aggregation");
                            let current = dst_slice
                                .squeeze_tensor(0)
                                .expect("failed to squeeze destination slice");
                            let updated =
                                current.add(&src_value).expect("operation should succeed");
                            let _ = dst_slice.copy_(
                                &updated
                                    .unsqueeze_tensor(0)
                                    .expect("failed to unsqueeze updated destination"),
                            );
                        }
                    }
                }
            }

            output_features.insert(dst_type.clone(), aggregated_output);
        }

        // Create output
        let mut output = HeteroGraphData::new();
        output.node_features = output_features;
        output.edge_indices = hetero_graph.edge_indices.clone();
        output.edge_attributes = hetero_graph.edge_attributes.clone();
        output.num_nodes = hetero_graph.num_nodes.clone();

        output
    }

    /// Get parameters
    pub fn parameters(&self) -> Vec<Tensor> {
        let mut params = Vec::new();

        // Add Q, K, V parameters
        for transform in self.query_transforms.values() {
            params.push(transform.clone_data());
        }
        for transform in self.key_transforms.values() {
            params.push(transform.clone_data());
        }
        for transform in self.value_transforms.values() {
            params.push(transform.clone_data());
        }

        // Add attention parameters
        for attention in self.relation_attentions.values() {
            params.push(attention.clone_data());
        }

        params
    }
}

/// Knowledge Graph Embedding layer
#[derive(Debug)]
pub struct KnowledgeGraphEmbedding {
    entity_types: Vec<NodeType>,
    relation_types: Vec<String>,
    /// Entity embeddings
    entity_embeddings: HashMap<NodeType, Parameter>,
    /// Relation embeddings
    relation_embeddings: HashMap<String, Parameter>,
    /// Embedding dimension
    embedding_dim: usize,
}

impl KnowledgeGraphEmbedding {
    /// Create new knowledge graph embeddings
    pub fn new(
        entity_types: Vec<NodeType>,
        relation_types: Vec<String>,
        num_entities: HashMap<NodeType, usize>,
        embedding_dim: usize,
    ) -> Self {
        let mut entity_embeddings = HashMap::new();
        let mut relation_embeddings = HashMap::new();

        // Create entity embeddings
        for entity_type in &entity_types {
            let num = num_entities.get(entity_type).unwrap_or(&100);
            let embeddings = Parameter::new(
                randn(&[*num, embedding_dim]).expect("failed to create entity embeddings"),
            );
            entity_embeddings.insert(entity_type.clone(), embeddings);
        }

        // Create relation embeddings
        for relation in &relation_types {
            let embeddings = Parameter::new(
                randn(&[embedding_dim, embedding_dim])
                    .expect("failed to create relation embeddings"),
            );
            relation_embeddings.insert(relation.clone(), embeddings);
        }

        Self {
            entity_types,
            relation_types,
            entity_embeddings,
            relation_embeddings,
            embedding_dim,
        }
    }

    /// Get entity embedding
    pub fn get_entity_embedding(&self, entity_type: &NodeType, entity_id: usize) -> Option<Tensor> {
        if let Some(embeddings) = self.entity_embeddings.get(entity_type) {
            Some(
                embeddings
                    .clone_data()
                    .slice_tensor(0, entity_id, entity_id + 1)
                    .expect("failed to slice entity embedding")
                    .squeeze_tensor(0)
                    .expect("failed to squeeze entity embedding"),
            )
        } else {
            None
        }
    }

    /// Compute triple score (head, relation, tail)
    pub fn triple_score(
        &self,
        head_type: &NodeType,
        head_id: usize,
        relation: &String,
        tail_type: &NodeType,
        tail_id: usize,
    ) -> Option<f64> {
        if let (Some(head_emb), Some(tail_emb), Some(rel_emb)) = (
            self.get_entity_embedding(head_type, head_id),
            self.get_entity_embedding(tail_type, tail_id),
            self.relation_embeddings.get(relation),
        ) {
            // Simple TransE-style scoring: ||h + r - t||
            let head_plus_rel = head_emb
                .unsqueeze_tensor(0)
                .expect("failed to unsqueeze head embedding")
                .matmul(&rel_emb.clone_data())
                .expect("operation should succeed")
                .squeeze_tensor(0)
                .expect("failed to squeeze head plus relation");

            let diff = head_plus_rel
                .sub(&tail_emb)
                .expect("operation should succeed");
            let score_tensor = diff
                .dot(&diff)
                .expect("failed to compute dot product for score");
            let score = score_tensor.to_vec().expect("conversion should succeed")[0] as f64;

            Some(-score) // Negative distance as score
        } else {
            None
        }
    }

    /// Get all parameters
    pub fn parameters(&self) -> Vec<Tensor> {
        let mut params = Vec::new();

        for emb in self.entity_embeddings.values() {
            params.push(emb.clone_data());
        }

        for emb in self.relation_embeddings.values() {
            params.push(emb.clone_data());
        }

        params
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use torsh_core::device::DeviceType;
    use torsh_tensor::creation::from_vec;

    #[test]
    fn test_hetero_graph_creation() {
        let mut hetero_graph = HeteroGraphData::new();

        // Add user nodes
        let user_features = from_vec(vec![1.0, 2.0, 3.0, 4.0], &[2, 2], DeviceType::Cpu)
            .expect("from vec should succeed");
        hetero_graph.add_node_type("user".to_string(), user_features);

        // Add item nodes
        let item_features = from_vec(
            vec![5.0, 6.0, 7.0, 8.0, 9.0, 10.0],
            &[2, 3],
            DeviceType::Cpu,
        )
        .expect("operation should succeed");
        hetero_graph.add_node_type("item".to_string(), item_features);

        // Add user-item edges
        let edge_index = from_vec(vec![0.0, 1.0, 0.0, 1.0], &[2, 2], DeviceType::Cpu)
            .expect("from vec should succeed");
        hetero_graph.add_edge_type(
            ("user".to_string(), "likes".to_string(), "item".to_string()),
            edge_index,
            None,
        );

        assert_eq!(hetero_graph.node_types().len(), 2);
        assert_eq!(hetero_graph.edge_types().len(), 1);
    }

    #[test]
    fn test_hetero_gnn_creation() {
        let mut node_dims = HashMap::new();
        node_dims.insert("user".to_string(), 2);
        node_dims.insert("item".to_string(), 3);

        let edge_types = vec![("user".to_string(), "likes".to_string(), "item".to_string())];

        let hetero_gnn = HeteroGNN::new(node_dims, edge_types, 8, true);
        let params = hetero_gnn.parameters();

        // Should have transformations for 2 node types + 1 edge type + biases
        assert!(params.len() >= 4);
    }

    #[test]
    fn test_knowledge_graph_embeddings() {
        let entity_types = vec!["person".to_string(), "company".to_string()];
        let relation_types = vec!["works_at".to_string(), "founded".to_string()];

        let mut num_entities = HashMap::new();
        num_entities.insert("person".to_string(), 10);
        num_entities.insert("company".to_string(), 5);

        let kg_emb = KnowledgeGraphEmbedding::new(entity_types, relation_types, num_entities, 50);

        // Test embedding retrieval
        let person_emb = kg_emb.get_entity_embedding(&"person".to_string(), 0);
        assert!(person_emb.is_some());

        let emb = person_emb.expect("operation should succeed");
        assert_eq!(emb.shape().dims(), &[50]);

        // Test triple scoring
        let score = kg_emb.triple_score(
            &"person".to_string(),
            0,
            &"works_at".to_string(),
            &"company".to_string(),
            0,
        );
        assert!(score.is_some());
        assert!(score.expect("operation should succeed").is_finite());
    }
}