pub struct PatchEmbedding<F: Float + Debug + ScalarOperand + Send + Sync + NumAssign> { /* private fields */ }Expand description
Patch Embedding layer.
Divides an image into fixed-size patches and projects each patch to an embedding vector via a learned linear projection (optionally including a bias term).
§Shape
- Input:
[batch_size, in_channels, image_height, image_width] - Output:
[batch_size, num_patches, embed_dim]wherenum_patches = (image_height / patch_size) * (image_width / patch_size)
§Parameters
weight: shape[embed_dim, in_channels * patch_h * patch_w]— the projection matrixbias(optional): shape[embed_dim]— per-embedding bias
Implementations§
Source§impl<F: Float + Debug + ScalarOperand + Send + Sync + NumAssign + 'static> PatchEmbedding<F>
impl<F: Float + Debug + ScalarOperand + Send + Sync + NumAssign + 'static> PatchEmbedding<F>
Sourcepub fn new<R: Rng>(
image_size: (usize, usize),
patch_size: (usize, usize),
in_channels: usize,
embed_dim: usize,
use_bias: bool,
rng: &mut R,
) -> Result<Self>
pub fn new<R: Rng>( image_size: (usize, usize), patch_size: (usize, usize), in_channels: usize, embed_dim: usize, use_bias: bool, rng: &mut R, ) -> Result<Self>
Create a new PatchEmbedding layer.
§Arguments
image_size— expected input image dimensions(height, width)patch_size— patch dimensions(height, width)— must divideimage_sizeevenlyin_channels— number of input image channelsembed_dim— output embedding dimensionuse_bias— whether to include a learnable bias termrng— random number generator for weight initialisation
Sourcepub fn num_patches(&self) -> usize
pub fn num_patches(&self) -> usize
Total number of patches produced from one image
Trait Implementations§
Source§impl<F: Float + Debug + ScalarOperand + Send + Sync + NumAssign + 'static> Clone for PatchEmbedding<F>
impl<F: Float + Debug + ScalarOperand + Send + Sync + NumAssign + 'static> Clone for PatchEmbedding<F>
Source§impl<F: Float + Debug + ScalarOperand + Send + Sync + NumAssign + 'static> Debug for PatchEmbedding<F>
impl<F: Float + Debug + ScalarOperand + Send + Sync + NumAssign + 'static> Debug for PatchEmbedding<F>
Source§impl<F: Float + Debug + ScalarOperand + Send + Sync + NumAssign + 'static> Layer<F> for PatchEmbedding<F>
impl<F: Float + Debug + ScalarOperand + Send + Sync + NumAssign + 'static> 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 learning rate
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
Source§fn layer_type(&self) -> &str
fn layer_type(&self) -> &str
Get the type of the layer (e.g., “Dense”, “Conv2D”)
Source§fn parameter_count(&self) -> usize
fn parameter_count(&self) -> usize
Get the number of trainable parameters in this layer
Source§fn layer_description(&self) -> String
fn layer_description(&self) -> String
Get a detailed description of this layer
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
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
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)
Source§fn is_training(&self) -> bool
fn is_training(&self) -> bool
Get the current training mode
Source§impl<F: Float + Debug + ScalarOperand + Send + Sync + NumAssign + 'static> ParamLayer<F> for PatchEmbedding<F>
impl<F: Float + Debug + ScalarOperand + Send + Sync + NumAssign + 'static> ParamLayer<F> for PatchEmbedding<F>
impl<F: Float + Debug + ScalarOperand + Send + Sync + NumAssign> Send for PatchEmbedding<F>
impl<F: Float + Debug + ScalarOperand + Send + Sync + NumAssign> Sync for PatchEmbedding<F>
Auto Trait Implementations§
impl<F> !Freeze for PatchEmbedding<F>
impl<F> RefUnwindSafe for PatchEmbedding<F>where
F: RefUnwindSafe,
impl<F> Unpin for PatchEmbedding<F>
impl<F> UnsafeUnpin 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 moreSource§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
The inverse inclusion map: attempts to construct
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
Checks if
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
Use with care! Same as
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
The inclusion map: converts
self to the equivalent element of its superset.