Skip to main content

PositionalEncoding

Trait PositionalEncoding 

Source
pub trait PositionalEncoding<F: Float + Debug + NumAssign> {
    // Required methods
    fn encode(&self, seq_len: usize) -> Array2<F>;
    fn apply(&self, input: &Array3<F>) -> Result<Array3<F>>;
    fn clone_box(&self) -> Box<dyn PositionalEncoding<F> + Send + Sync>
       where F: Send + Sync + 'static;
    fn d_model(&self) -> usize;
    fn max_len(&self) -> usize;

    // Provided methods
    fn forward(&self, input: &Array<F, IxDyn>) -> Result<Array<F, IxDyn>> { ... }
    fn update(&mut self, _learning_rate: F) -> Result<()> { ... }
}
Expand description

Trait for positional encoding implementations

Required Methods§

Source

fn encode(&self, seq_len: usize) -> Array2<F>

Encode positions for a sequence of given length

§Arguments
  • seq_len - Length of the sequence
§Returns

Position encodings of shape [seq_len, d_model]

Source

fn apply(&self, input: &Array3<F>) -> Result<Array3<F>>

Apply positional encoding to an input tensor

§Arguments
  • input - Input tensor of shape [batch_size, seq_len, d_model]
§Returns

Tensor with positional encoding added

Source

fn clone_box(&self) -> Box<dyn PositionalEncoding<F> + Send + Sync>
where F: Send + Sync + 'static,

Clone the positional encoding into a boxed trait object

Source

fn d_model(&self) -> usize

Get the model dimension

Source

fn max_len(&self) -> usize

Get the maximum sequence length supported

Provided Methods§

Source

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

Forward pass with dynamic array

§Arguments
  • input - Input tensor of dynamic shape [batch_size, seq_len, d_model]
§Returns

Tensor with positional encoding added

Source

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

Update trainable parameters (for learned encodings)

§Arguments
  • _learning_rate - Learning rate for parameter updates
§Returns

Result indicating success or failure

Implementors§