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>
impl<F: Float + Debug + ScalarOperand + Send + Sync> PatchEmbedding<F>
Sourcepub fn new(
image_size: (usize, usize),
patch_size: (usize, usize),
in_channels: usize,
embedding_dim: usize,
use_bias: bool,
) -> Result<Self>
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}
Sourcepub fn num_patches(&self) -> usize
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>
impl<F: Clone + Float + Debug + ScalarOperand + Send + Sync> Clone for PatchEmbedding<F>
Source§fn clone(&self) -> PatchEmbedding<F>
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)
const fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source
. Read moreSource§impl<F: Debug + Float + Debug + ScalarOperand + Send + Sync> Debug for PatchEmbedding<F>
impl<F: Debug + Float + Debug + ScalarOperand + Send + Sync> Debug for PatchEmbedding<F>
Source§impl<F: Float + Debug + ScalarOperand + Send + Sync> Layer<F> for PatchEmbedding<F>
impl<F: Float + Debug + ScalarOperand + Send + Sync> Layer<F> for PatchEmbedding<F>
Source§fn forward(&self, input: &Array<F, IxDyn>) -> Result<Array<F, IxDyn>>
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>>
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<()>
fn update(&mut self, learning_rate: F) -> Result<()>
Update the layer parameters with the given gradients Read more
Source§fn as_any_mut(&mut self) -> &mut dyn Any
fn as_any_mut(&mut self) -> &mut dyn Any
Get the layer as a mutable dyn Any for downcasting Read more
Source§fn gradients(&self) -> Vec<Array<F, IxDyn>> ⓘ
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<()>
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<()>
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)
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
fn is_training(&self) -> bool
Get the current training mode Read more
Source§fn layer_type(&self) -> &str
fn layer_type(&self) -> &str
Get the type of the layer (e.g., “Dense”, “Conv2D”) Read more
Source§fn parameter_count(&self) -> usize
fn parameter_count(&self) -> usize
Get the number of trainable parameters in this layer Read more
Source§fn layer_description(&self) -> String
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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