Struct PatchEmbedding

Source
pub struct PatchEmbedding<F: Float + Debug + ScalarOperand + Send + Sync> {
    pub image_size: (usize, usize),
    pub patch_size: (usize, usize),
    pub in_channels: usize,
    pub embedding_dim: usize,
    pub weight: Array<F, IxDyn>,
    pub bias: Option<Array<F, IxDyn>>,
    /* private fields */
}
Expand description

Patch Embedding layer for vision transformers

This layer converts image patches into embeddings for vision transformers. It applies a convolution to extract patches and flatten them into embedding vectors.

Fields§

§image_size: (usize, usize)

Size of input images (height, width)

§patch_size: (usize, usize)

Size of patches (height, width)

§in_channels: usize

Number of input channels (e.g., 3 for RGB)

§embedding_dim: usize

Dimension of each embedding vector

§weight: Array<F, IxDyn>

Weight matrix for patch extraction

§bias: Option<Array<F, IxDyn>>

Bias vector

Implementations§

Source§

impl<F: Float + Debug + ScalarOperand + Send + Sync> PatchEmbedding<F>

Source

pub fn new( image_size: (usize, usize), patch_size: (usize, usize), in_channels: usize, embedding_dim: usize, use_bias: bool, ) -> Result<Self>

Create a new PatchEmbedding layer

Examples found in repository?
examples/embedding_example.rs (line 66)
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}
Source

pub fn num_patches(&self) -> usize

Calculate the number of patches

Examples found in repository?
examples/embedding_example.rs (line 76)
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}

Trait Implementations§

Source§

impl<F: Clone + Float + Debug + ScalarOperand + Send + Sync> Clone for PatchEmbedding<F>

Source§

fn clone(&self) -> PatchEmbedding<F>

Returns a duplicate of the value. Read more
1.0.0 · Source§

const fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<F: Debug + Float + Debug + ScalarOperand + Send + Sync> Debug for PatchEmbedding<F>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<F: Float + Debug + ScalarOperand + Send + Sync> Layer<F> for PatchEmbedding<F>

Source§

fn forward(&self, input: &Array<F, IxDyn>) -> Result<Array<F, IxDyn>>

Forward pass of the layer Read more
Source§

fn backward( &self, _input: &Array<F, IxDyn>, grad_output: &Array<F, IxDyn>, ) -> Result<Array<F, IxDyn>>

Backward pass of the layer to compute gradients Read more
Source§

fn update(&mut self, learning_rate: F) -> Result<()>

Update the layer parameters with the given gradients Read more
Source§

fn as_any(&self) -> &dyn Any

Get the layer as a dyn Any for downcasting Read more
Source§

fn as_any_mut(&mut self) -> &mut dyn Any

Get the layer as a mutable dyn Any for downcasting Read more
Source§

fn params(&self) -> Vec<Array<F, IxDyn>>

Get the parameters of the layer Read more
Source§

fn gradients(&self) -> Vec<Array<F, IxDyn>>

Get the gradients of the layer parameters Read more
Source§

fn set_gradients(&mut self, _gradients: &[Array<F, IxDyn>]) -> Result<()>

Set the gradients of the layer parameters Read more
Source§

fn set_params(&mut self, _params: &[Array<F, IxDyn>]) -> Result<()>

Set the parameters of the layer Read more
Source§

fn set_training(&mut self, _training: bool)

Set the layer to training mode (true) or evaluation mode (false) Read more
Source§

fn is_training(&self) -> bool

Get the current training mode Read more
Source§

fn layer_type(&self) -> &str

Get the type of the layer (e.g., “Dense”, “Conv2D”) Read more
Source§

fn parameter_count(&self) -> usize

Get the number of trainable parameters in this layer Read more
Source§

fn layer_description(&self) -> String

Get a detailed description of this layer Read more

Auto Trait Implementations§

§

impl<F> Freeze for PatchEmbedding<F>

§

impl<F> RefUnwindSafe for PatchEmbedding<F>
where F: RefUnwindSafe,

§

impl<F> Send for PatchEmbedding<F>

§

impl<F> Sync for PatchEmbedding<F>

§

impl<F> Unpin for PatchEmbedding<F>

§

impl<F> UnwindSafe for PatchEmbedding<F>
where F: RefUnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V