Struct burn::nn::PositionalEncoding
pub struct PositionalEncoding<B>where
B: Backend,{
pub sinusoids: Tensor<B, 3>,
pub max_sequence_size: usize,
pub max_timescale: usize,
}
Expand description
Positional encoding layer for transformer models.
This layer adds positional information to the input embeddings, allowing the transformer model to take into account the order of the sequence. The positional encoding is added to the input embeddings by computing a set of sinusoidal functions with different frequencies and phases.
Sinusoids are used for positional embedding introduced in Attention is all you need.
The reference implementation can be found here: LANGUAGE MODELING WITH NN.TRANSFORMER AND TORCHTEXT
Should be created using PositionalEncodingConfig
Fields§
§sinusoids: Tensor<B, 3>
The sinusoids used to add positional information to the input embeddings.
max_sequence_size: usize
The maximum sequence size to use.
max_timescale: usize
Max time scale to use.
Implementations§
§impl<B> PositionalEncoding<B>where
B: Backend,
impl<B> PositionalEncoding<B>where
B: Backend,
pub fn forward(&self, input: Tensor<B, 3>) -> Tensor<B, 3>
pub fn forward(&self, input: Tensor<B, 3>) -> Tensor<B, 3>
Applies the forward pass on the input tensor by adding the sinusoids to the input.
§Shapes
- input: [batch_size, seq_length, d_model]
- output: [batch_size, seq_length, d_model]
§Panics
- Panics if the input sequence length is greater than the maximum sequence size.
- Panics if the input d_model is not equal to the d_model of the sinusoids.
Trait Implementations§
§impl<B> AutodiffModule<B> for PositionalEncoding<B>
impl<B> AutodiffModule<B> for PositionalEncoding<B>
§type InnerModule = PositionalEncoding<<B as AutodiffBackend>::InnerBackend>
type InnerModule = PositionalEncoding<<B as AutodiffBackend>::InnerBackend>
§fn valid(&self) -> <PositionalEncoding<B> as AutodiffModule<B>>::InnerModule
fn valid(&self) -> <PositionalEncoding<B> as AutodiffModule<B>>::InnerModule
§impl<B> Clone for PositionalEncoding<B>where
B: Backend,
impl<B> Clone for PositionalEncoding<B>where
B: Backend,
§fn clone(&self) -> PositionalEncoding<B>
fn clone(&self) -> PositionalEncoding<B>
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more§impl<B> Debug for PositionalEncoding<B>
impl<B> Debug for PositionalEncoding<B>
§impl<B> Display for PositionalEncoding<B>where
B: Backend,
impl<B> Display for PositionalEncoding<B>where
B: Backend,
§impl<B> Module<B> for PositionalEncoding<B>where
B: Backend,
impl<B> Module<B> for PositionalEncoding<B>where
B: Backend,
§type Record = PositionalEncodingRecord<B>
type Record = PositionalEncodingRecord<B>
§fn load_record(
self,
record: <PositionalEncoding<B> as Module<B>>::Record,
) -> PositionalEncoding<B>
fn load_record( self, record: <PositionalEncoding<B> as Module<B>>::Record, ) -> PositionalEncoding<B>
§fn into_record(self) -> <PositionalEncoding<B> as Module<B>>::Record
fn into_record(self) -> <PositionalEncoding<B> as Module<B>>::Record
§fn num_params(&self) -> usize
fn num_params(&self) -> usize
§fn visit<Visitor>(&self, visitor: &mut Visitor)where
Visitor: ModuleVisitor<B>,
fn visit<Visitor>(&self, visitor: &mut Visitor)where
Visitor: ModuleVisitor<B>,
§fn map<Mapper>(self, mapper: &mut Mapper) -> PositionalEncoding<B>where
Mapper: ModuleMapper<B>,
fn map<Mapper>(self, mapper: &mut Mapper) -> PositionalEncoding<B>where
Mapper: ModuleMapper<B>,
§fn collect_devices(
&self,
devices: Vec<<B as Backend>::Device>,
) -> Vec<<B as Backend>::Device>
fn collect_devices( &self, devices: Vec<<B as Backend>::Device>, ) -> Vec<<B as Backend>::Device>
§fn to_device(self, device: &<B as Backend>::Device) -> PositionalEncoding<B>
fn to_device(self, device: &<B as Backend>::Device) -> PositionalEncoding<B>
§fn fork(self, device: &<B as Backend>::Device) -> PositionalEncoding<B>
fn fork(self, device: &<B as Backend>::Device) -> PositionalEncoding<B>
§fn devices(&self) -> Vec<<B as Backend>::Device>
fn devices(&self) -> Vec<<B as Backend>::Device>
§fn save_file<FR, PB>(
self,
file_path: PB,
recorder: &FR,
) -> Result<(), RecorderError>
fn save_file<FR, PB>( self, file_path: PB, recorder: &FR, ) -> Result<(), RecorderError>
§fn load_file<FR, PB>(
self,
file_path: PB,
recorder: &FR,
device: &<B as Backend>::Device,
) -> Result<Self, RecorderError>
fn load_file<FR, PB>( self, file_path: PB, recorder: &FR, device: &<B as Backend>::Device, ) -> Result<Self, RecorderError>
§fn quantize_weights<C>(self, quantizer: &mut Quantizer<C>) -> Selfwhere
C: Calibration,
fn quantize_weights<C>(self, quantizer: &mut Quantizer<C>) -> Selfwhere
C: Calibration,
§impl<B> ModuleDisplay for PositionalEncoding<B>where
B: Backend,
impl<B> ModuleDisplay for PositionalEncoding<B>where
B: Backend,
§fn custom_settings(&self) -> Option<DisplaySettings>
fn custom_settings(&self) -> Option<DisplaySettings>
§fn custom_content(&self, content: Content) -> Option<Content>
fn custom_content(&self, content: Content) -> Option<Content>
§fn format(&self, passed_settings: DisplaySettings) -> String
fn format(&self, passed_settings: DisplaySettings) -> String
§impl<B> ModuleDisplayDefault for PositionalEncoding<B>where
B: Backend,
impl<B> ModuleDisplayDefault for PositionalEncoding<B>where
B: Backend,
Auto Trait Implementations§
impl<B> Freeze for PositionalEncoding<B>where
<B as Backend>::FloatTensorPrimitive: Freeze,
<B as Backend>::QuantizedTensorPrimitive: Freeze,
impl<B> RefUnwindSafe for PositionalEncoding<B>where
<B as Backend>::FloatTensorPrimitive: RefUnwindSafe,
<B as Backend>::QuantizedTensorPrimitive: RefUnwindSafe,
impl<B> Send for PositionalEncoding<B>
impl<B> Sync for PositionalEncoding<B>
impl<B> Unpin for PositionalEncoding<B>
impl<B> UnwindSafe for PositionalEncoding<B>where
<B as Backend>::FloatTensorPrimitive: UnwindSafe,
<B as Backend>::QuantizedTensorPrimitive: UnwindSafe,
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
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
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>
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>
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§impl<T> Pointable for T
impl<T> Pointable for T
§impl<T> ToCompactString for Twhere
T: Display,
impl<T> ToCompactString for Twhere
T: Display,
§fn try_to_compact_string(&self) -> Result<CompactString, ToCompactStringError>
fn try_to_compact_string(&self) -> Result<CompactString, ToCompactStringError>
ToCompactString::to_compact_string()
] Read more§fn to_compact_string(&self) -> CompactString
fn to_compact_string(&self) -> CompactString
CompactString
]. Read more