Struct Lstm
pub struct Lstm {
pub input_gate: GateController,
pub forget_gate: Option<GateController>,
pub output_gate: GateController,
pub cell_gate: GateController,
pub d_hidden: usize,
pub batch_first: bool,
pub reverse: bool,
pub clip: Option<f64>,
pub input_forget: bool,
pub gate_activation: Activation,
pub cell_activation: Activation,
pub hidden_activation: Activation,
}Expand description
The Lstm module. This implementation is for a unidirectional, stateless, Lstm.
Introduced in the paper: Long Short-Term Memory.
Should be created with LstmConfig.
§Checkpoint compatibility
Initialize with the same input_forget setting used during training before loading weights.
Older coupled LSTM checkpoints contain unused forget-gate tensors. Load these with
ModuleRecord::allow_unused(true), then save the
module again to omit those tensors. Uncoupled checkpoints retain the same parameter paths.
Fields§
§input_gate: GateControllerThe input gate regulates which information to update and store in the cell state at each time step.
forget_gate: Option<GateController>The forget gate is used to control which information to discard or keep in the memory cell at each time step.
Absent when input_forget is true (forget = 1 - input).
Access an uncoupled gate with as_ref() or as_mut(), and replace it with Some(gate).
output_gate: GateControllerThe output gate determines which information from the cell state to output at each time step.
cell_gate: GateControllerThe cell gate is used to compute the cell state that stores and carries information through time.
The hidden state of the LSTM.
batch_first: boolIf true, input is [batch_size, seq_length, input_size].
If false, input is [seq_length, batch_size, input_size].
reverse: boolIf true, process the sequence in reverse order.
clip: Option<f64>Optional cell state clip threshold.
input_forget: boolIf true, couples input and forget gates: f_t = 1 - i_t. Set through LstmConfig::input_forget at initialization so the forget gate is omitted.
gate_activation: ActivationActivation function for gates (input, forget, output).
cell_activation: ActivationActivation function for cell gate (candidate cell state).
Activation function for hidden output.
Implementations§
§impl Lstm
impl Lstm
pub fn forward(
&self,
batched_input: Tensor<3>,
state: Option<LstmState<2>>,
) -> (Tensor<3>, LstmState<2>)
pub fn forward( &self, batched_input: Tensor<3>, state: Option<LstmState<2>>, ) -> (Tensor<3>, LstmState<2>)
Applies the forward pass on the input tensor. This LSTM implementation returns the state for each element in a sequence (i.e., across seq_length) and a final state.
§Parameters:
- batched_input: The input tensor of shape:
[batch_size, sequence_length, input_size]ifbatch_firstis true (default)[sequence_length, batch_size, input_size]ifbatch_firstis false
- state: An optional
LstmStaterepresenting the initial cell state and hidden state. Each state tensor has shape[batch_size, hidden_size]. If no initial state is provided, these tensors are initialized to zeros.
§Returns:
- output: A tensor represents the output features of LSTM. Shape:
[batch_size, sequence_length, hidden_size]ifbatch_firstis true[sequence_length, batch_size, hidden_size]ifbatch_firstis false
- state: A
LstmStaterepresents the final states. Bothstate.cellandstate.hiddenhave the shape[batch_size, hidden_size].
pub fn forward_iter<I>(
&self,
input_timestep_iter: I,
state: Option<LstmState<2>>,
batch_size: usize,
seq_length: usize,
device: &Device,
) -> (Tensor<3>, LstmState<2>)
pub fn forward_iter<I>( &self, input_timestep_iter: I, state: Option<LstmState<2>>, batch_size: usize, seq_length: usize, device: &Device, ) -> (Tensor<3>, LstmState<2>)
Applies the forward iteration over input timesteps for the LSTM. This method is called
internally by the forward function to process each timestep of the sequence.
§Parameters:
input_timestep_iter: An iterator where each item is a pair consisting of the timestep index (usize) and the corresponding input tensor of shape[batch_size, 1, d_input].state: An optionalLstmStaterepresenting the initial cell and hidden states. IfNone, the states are initialized to zeros with shapes[batch_size, d_hidden].batch_size: The number of sequences in the batch.seq_length: The length of the input sequence (number of timesteps).device: The device where computations will run.
§Returns:
- A pair where:
- The first element is a tensor of shape
[batch_size, seq_length, d_hidden], containing the output features for all timesteps. - The second element is the final
LstmState, which includes the final cell and hidden states, both with the shape[batch_size, d_hidden].
- The first element is a tensor of shape
Trait Implementations§
§impl Module for Lstm
impl Module for Lstm
§fn num_params(&self) -> usize
fn num_params(&self) -> usize
§fn visit<Visitor>(&self, visitor: &mut Visitor)where
Visitor: ModuleVisitor,
fn visit<Visitor>(&self, visitor: &mut Visitor)where
Visitor: ModuleVisitor,
§fn map<Mapper>(self, mapper: &mut Mapper) -> Lstmwhere
Mapper: ModuleMapper,
fn map<Mapper>(self, mapper: &mut Mapper) -> Lstmwhere
Mapper: ModuleMapper,
§fn collect_devices(&self, devices: Vec<Device>) -> Vec<Device>
fn collect_devices(&self, devices: Vec<Device>) -> Vec<Device>
§fn to_device(self, device: &Device) -> Lstm
fn to_device(self, device: &Device) -> Lstm
§fn fork(self, device: &Device) -> Lstm
fn fork(self, device: &Device) -> Lstm
§fn valid(&self) -> Lstm
fn valid(&self) -> Lstm
§fn train(self) -> Lstm
fn train(self) -> Lstm
§fn materialize(self) -> Lstm
fn materialize(self) -> Lstm
§fn devices(&self) -> Vec<Device>
fn devices(&self) -> Vec<Device>
§fn set_require_grad(self, require_grad: bool) -> Self
fn set_require_grad(self, require_grad: bool) -> Self
§fn set_require_grad_group(self, group: ParamGroup, require_grad: bool) -> Self
fn set_require_grad_group(self, group: ParamGroup, require_grad: bool) -> Self
§fn no_grad(self) -> Self
fn no_grad(self) -> Self
§fn freeze_group(self, group: ParamGroup) -> Self
fn freeze_group(self, group: ParamGroup) -> Self
§fn unfreeze_group(self, group: ParamGroup) -> Self
fn unfreeze_group(self, group: ParamGroup) -> Self
§fn quantize_weights(self, quantizer: &mut Quantizer) -> Self
fn quantize_weights(self, quantizer: &mut Quantizer) -> Self
§fn quantize_weights_group(
self,
quantizer: &mut Quantizer,
group: ParamGroup,
) -> Self
fn quantize_weights_group( self, quantizer: &mut Quantizer, group: ParamGroup, ) -> Self
§fn apply_reparameterization<R>(self, reparameterizer: R) -> Selfwhere
Self: Sized,
R: Reparameterizer,
fn apply_reparameterization<R>(self, reparameterizer: R) -> Selfwhere
Self: Sized,
R: Reparameterizer,
Reparameterizer. Read more§fn apply_lora(self, lora: Lora) -> Selfwhere
Self: Sized,
fn apply_lora(self, lora: Lora) -> Selfwhere
Self: Sized,
§fn apply_qlora(self, qlora: QLora) -> Selfwhere
Self: Sized,
fn apply_qlora(self, qlora: QLora) -> Selfwhere
Self: Sized,
§fn into_record(self) -> ModuleRecordwhere
Self: Sized,
fn into_record(self) -> ModuleRecordwhere
Self: Sized,
ModuleRecord. Read more§fn into_record_group(self, group: ParamGroup) -> ModuleRecordwhere
Self: Sized,
fn into_record_group(self, group: ParamGroup) -> ModuleRecordwhere
Self: Sized,
§fn try_load_record(self, record: ModuleRecord) -> Result<Self, RecordError>where
Self: Sized,
fn try_load_record(self, record: ModuleRecord) -> Result<Self, RecordError>where
Self: Sized,
ModuleRecord to this module, returning the loaded
module. Read more§fn load_record(self, record: ModuleRecord) -> Selfwhere
Self: Sized,
fn load_record(self, record: ModuleRecord) -> Selfwhere
Self: Sized,
ModuleRecord to this module, consuming and returning
it. Read more§fn save_file<P>(self, path: P) -> Result<(), RecordError>
fn save_file<P>(self, path: P) -> Result<(), RecordError>
§fn load_file<P>(self, path: P) -> Self
fn load_file<P>(self, path: P) -> Self
§fn try_load_file<P>(self, path: P) -> Result<Self, RecordError>
fn try_load_file<P>(self, path: P) -> Result<Self, RecordError>
§impl ModuleDisplay for Lstm
impl ModuleDisplay for Lstm
§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
Auto Trait Implementations§
impl !RefUnwindSafe for Lstm
impl !UnwindSafe for Lstm
impl Freeze for Lstm
impl Send for Lstm
impl Sync for Lstm
impl Unpin for Lstm
impl UnsafeUnpin for Lstm
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<K, Q> Comparable<Q> for K
impl<K, Q> Comparable<Q> for K
§impl<K, Q> Equivalent<Q> for K
impl<K, Q> Equivalent<Q> for K
§fn equivalent(&self, key: &Q) -> bool
fn equivalent(&self, key: &Q) -> bool
key and return true if they are equal.impl<T> ErasedDestructor for Twhere
T: 'static,
§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> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
§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