pub struct Embedding<B>where
B: Backend,{ /* private fields */ }Expand description
A learnable lookup table mapping integer indices to dense vectors.
§Examples
ⓘ
let emb = Embedding::<CpuBackend>::new(1000, 128, DType::F32, &dev)?;
// Input: token indices [batch=2, seq_len=5]
let tokens = CpuTensor::from_f64_slice(&indices, (2, 5), DType::I64, &dev)?;
let vectors = emb.forward(&tokens)?; // [2, 5, 128]Implementations§
Source§impl<B> Embedding<B>where
B: Backend,
impl<B> Embedding<B>where
B: Backend,
Sourcepub fn new(
num_embeddings: usize,
embedding_dim: usize,
dtype: DType,
device: &<B as Backend>::Device,
) -> Result<Embedding<B>, Error>
pub fn new( num_embeddings: usize, embedding_dim: usize, dtype: DType, device: &<B as Backend>::Device, ) -> Result<Embedding<B>, Error>
Create a new Embedding layer with normally-distributed random weights.
Sourcepub fn from_tensor(weight: Tensor<B>) -> Result<Embedding<B>, Error>
pub fn from_tensor(weight: Tensor<B>) -> Result<Embedding<B>, Error>
Create from an existing weight matrix.
pub fn num_embeddings(&self) -> usize
pub fn embedding_dim(&self) -> usize
pub fn weight(&self) -> &Tensor<B>
Trait Implementations§
Source§impl<B> Module<B> for Embedding<B>where
B: Backend,
impl<B> Module<B> for Embedding<B>where
B: Backend,
Source§fn forward(&self, indices: &Tensor<B>) -> Result<Tensor<B>, Error>
fn forward(&self, indices: &Tensor<B>) -> Result<Tensor<B>, Error>
Look up embeddings for the given indices.
Input: integer tensor of any shape […] Output: tensor of shape […, embedding_dim]
Uses index_select to gather rows from the weight matrix directly
on-device (no host round-trip). For autograd, we record the operation
so gradients can flow back through the embedding table.
Source§fn parameters(&self) -> Vec<Tensor<B>>
fn parameters(&self) -> Vec<Tensor<B>>
Return all trainable parameters of this module.
The optimizer uses these to update weights during training.
Source§fn named_parameters(&self) -> Vec<(String, Tensor<B>)>
fn named_parameters(&self) -> Vec<(String, Tensor<B>)>
Return all trainable parameters with human-readable names. Read more
Source§fn set_training(&self, _training: bool)
fn set_training(&self, _training: bool)
Set training or evaluation mode. Read more
Source§fn is_training(&self) -> bool
fn is_training(&self) -> bool
Whether the module is in training mode (default: true).
Source§fn num_parameters(&self) -> usize
fn num_parameters(&self) -> usize
Total number of scalar parameters in this module.
Source§fn trainable_params_count(&self) -> usize
fn trainable_params_count(&self) -> usize
Number of trainable (variable) parameters.
Auto Trait Implementations§
impl<B> Freeze for Embedding<B>
impl<B> RefUnwindSafe for Embedding<B>
impl<B> Send for Embedding<B>
impl<B> Sync for Embedding<B>
impl<B> Unpin for Embedding<B>
impl<B> UnwindSafe for Embedding<B>
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> 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