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
Encode positions for a sequence of given length
§Arguments
seq_len - Length of the sequence
§Returns
Position encodings of shape [seq_len, d_model]
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
Clone the positional encoding into a boxed trait object
Get the maximum sequence length supported
Forward pass with dynamic array
§Arguments
input - Input tensor of dynamic shape [batch_size, seq_len, d_model]
§Returns
Tensor with positional encoding added
Update trainable parameters (for learned encodings)
§Arguments
_learning_rate - Learning rate for parameter updates
§Returns
Result indicating success or failure