embedding_example/
embedding_example.rs

1use ndarray::{Array, IxDyn};
2use scirs2_neural::layers::{
3    Embedding, EmbeddingConfig, Layer, PatchEmbedding, PositionalEmbedding,
4};
5
6fn main() -> Result<(), Box<dyn std::error::Error>> {
7    println!("Running embedding examples...");
8
9    // Example 1: Basic Embedding
10    println!("\n--- Basic Embedding Example ---");
11    let config = EmbeddingConfig {
12        num_embeddings: 10,
13        embedding_dim: 5,
14        padding_idx: Some(0),
15        max_norm: None,
16        norm_type: 2.0,
17        scale_grad_by_freq: false,
18        sparse: false,
19    };
20
21    let embedding = Embedding::<f32>::new(config)?;
22
23    // Create input indices
24    let indices = Array::from_shape_vec(IxDyn(&[2, 3]), vec![1, 2, 0, 3, 0, 4])?;
25
26    // Forward pass
27    let output = embedding.forward(&indices.mapv(|x| x as f32))?;
28
29    println!("Input indices shape: {:?}", indices.shape());
30    println!("Output embeddings shape: {:?}", output.shape());
31    println!(
32        "First embedding vector: {:?}",
33        output.slice(ndarray::s![0, 0, ..]).to_owned()
34    );
35
36    // Example 2: Positional Embedding
37    println!("\n--- Positional Embedding Example ---");
38
39    // Create fixed sinusoidal positional embeddings
40    let pos_embedding = PositionalEmbedding::<f32>::new(10, 8, false)?;
41
42    // Create dummy input (like token embeddings)
43    let token_embeddings = Array::from_shape_fn(IxDyn(&[2, 5, 8]), |_| 1.0f32);
44
45    // Add positional information
46    let output = pos_embedding.forward(&token_embeddings)?;
47
48    println!("Token embeddings shape: {:?}", token_embeddings.shape());
49    println!(
50        "Output with positional encoding shape: {:?}",
51        output.shape()
52    );
53    println!(
54        "First token before positional encoding: {:?}",
55        token_embeddings.slice(ndarray::s![0, 0, ..]).to_owned()
56    );
57    println!(
58        "First token after positional encoding: {:?}",
59        output.slice(ndarray::s![0, 0, ..]).to_owned()
60    );
61
62    // Example 3: Patch Embedding (for Vision Transformers)
63    println!("\n--- Patch Embedding Example ---");
64
65    // Create patch embedding for a vision transformer
66    let patch_embedding = PatchEmbedding::<f32>::new((32, 32), (8, 8), 3, 96, true)?;
67
68    // Create random image input
69    let image_input = Array::from_shape_fn(IxDyn(&[1, 3, 32, 32]), |_| rand::random::<f32>());
70
71    // Extract patch embeddings
72    let output = patch_embedding.forward(&image_input)?;
73
74    println!("Input image shape: {:?}", image_input.shape());
75    println!("Patch embeddings shape: {:?}", output.shape());
76    println!("Number of patches: {}", patch_embedding.num_patches());
77    println!("Embedding dimension: {}", patch_embedding.embedding_dim);
78
79    // Print first patch embedding
80    println!(
81        "First patch embedding (first 5 values): {:?}",
82        output.slice(ndarray::s![0, 0, ..5]).to_owned()
83    );
84
85    println!("\nAll embedding examples completed successfully!");
86
87    Ok(())
88}