rau.unidirectional¶
- class rau.unidirectional.Unidirectional¶
Bases:
ModuleAn API for unidirectional sequential neural networks (including RNNs and transformer decoders).
Let \(B\) be batch size, and \(n\) be the length of the input sequence.
- class State¶
Bases:
objectRepresents the hidden state of the module after processing a certain number of inputs.
- fastforward(input_sequence)¶
Feed a sequence of inputs to this state and return the resulting state.
- forward(input_sequence, include_first, return_state=False, return_output=True)¶
Like
Unidirectional.forward(), but start with this state as the initial state.This can often be done more efficiently than using
next()iteratively.- Parameters:
input_sequence (
Tensor) – A \(B \times n \times \cdots\) tensor, representing \(n\) input tensors.return_state (
bool) – Whether to return the lastStateof the module.include_first (
bool) – Whether to prepend an extra tensor to the beginning of the output corresponding to an output from this state, before reading the first input.
- Return type:
- Returns:
- next(input_tensor)¶
Feed an input to this hidden state and produce the next hidden state.
- output()¶
Get the output associated with this state.
For example, this can be the hidden state vector itself, or the hidden state passed through an affine transformation.
The return value is either a tensor or a tuple whose first element is a tensor. The other elements of the tuple can be used to return extra outputs.
- Return type:
- Returns:
A \(B \times \cdots\) tensor, or a tuple whose first element is a tensor. The other elements of the tuple can contain extra outputs. If there are any extra outputs, then the output of
forward()andUnidirectional.forward()will contain the same number of extra outputs, where each extra output is alistcontaining all the outputs across all timesteps.
- slice_batch(s)¶
Return a copy of this state with only certain batch elements included, determined by the slice
s.
- class StatefulComposedState¶
Bases:
StateStatefulComposedState(parent: ‘Unidirectional’, first_is_main: bool, first_state: ‘Unidirectional.State’, second_state: ‘Unidirectional.State’)
- __init__(parent, first_is_main, first_state, second_state)¶
- forward(input_sequence, include_first, return_state=False, return_output=True)¶
- Return type:
-
parent:
Unidirectional¶
- __init__(main=False, tags=None)¶
- as_composable()¶
- Return type:
- forward(input_sequence, *args, initial_state=None, return_state=False, include_first=True, **kwargs)¶
Run this module on an entire sequence of inputs all at once.
This can often be done more efficiently than processing each input one by one.
- Parameters:
input_sequence (
Tensor) – A \(B \times n \times \cdots\) tensor representing a sequence of \(n\) input tensors.initial_state (
State|None) – An optional initial state to use instead of the default initial state created byinitial_state().return_state (
bool) – Whether to return the lastStateof the module as an additional output. This state can be used to initialize a subsequent run.include_first (
bool) – Whether to prepend an extra tensor to the beginning of the output corresponding to a prediction for the first element in the input. Ifinclude_firstis true, then the length of the output tensor will be \(n + 1\). Otherwise, it will be \(n\).args (
Any) – Extra arguments passed toinitial_state().kwargs (
Any) – Extra arguments passed toinitial_state().
- Return type:
- Returns:
A
Tensoror aForwardResultthat contains the output tensor. The output tensor will be of size \(B \times n+1 \times \cdots\) ifinclude_firstis true and \(B \times n \times \cdots\) otherwise. IfUnidirectional.State.output()returns extra outputs at each timestep, then they will be aggregated over all timesteps and returned aslists inForwardResult.extra_outputs. Ifreturn_stateis true, then the finalStatewill be returned inForwardResult.state. If there are no extra outputs and there is no state to return, just the output tensor is returned.
- initial_composed_state(input_module, input_state, *args, **kwargs)¶
- initial_state(batch_size, *args, **kwargs)¶
Get the initial state of the model.
- main()¶
Mark this module as main.
- Return type:
- Returns:
Self.
- class rau.unidirectional.ForwardResult¶
Bases:
objectThe output of a call to
Unidirectional.forward()orUnidirectional.State.forward().- __init__(output, extra_outputs, state)¶
- class rau.unidirectional.StatelessUnidirectional¶
Bases:
UnidirectionalA sequential module that has no temporal recurrence, but applies some function to every timestep.
- class ComposedState¶
Bases:
StateComposedState(parent: ‘StatelessUnidirectional’, args: list[typing.Any], kwargs: dict[str, typing.Any], input_is_main: bool, use_initial_output: bool, input_state: rau.unidirectional.unidirectional.Unidirectional.State)
- __init__(parent, args, kwargs, input_is_main, use_initial_output, input_state)¶
- forward(input_sequence, include_first, return_state=False, return_output=True)¶
- Return type:
-
parent:
StatelessUnidirectional¶
- class State¶
Bases:
StateState(parent: ‘StatelessUnidirectional’, args: list[typing.Any], kwargs: dict[str, typing.Any], _batch_size: int | None, input_tensor: torch.Tensor | None)
- __init__(parent, args, kwargs, _batch_size, input_tensor)¶
- forward(input_sequence, include_first, return_state=False, return_output=True)¶
- Return type:
-
parent:
StatelessUnidirectional¶
- forward_sequence(input_sequence, *args, **kwargs)¶
Transform a sequence of tensors.
- forward_single(input_tensor, *args, **kwargs)¶
Transform an input tensor for a single timestep.
- initial_output(batch_size, *args, **kwargs)¶
Get the output of the initial state. By default, this simply raises an error.
- class rau.unidirectional.StatelessLayerUnidirectional¶
Bases:
StatelessUnidirectional- __init__(func)¶
- class rau.unidirectional.StatelessReshapingLayerUnidirectional¶
Bases:
StatelessLayerUnidirectional
- class rau.unidirectional.PositionalUnidirectional¶
Bases:
Unidirectional- class State¶
Bases:
StateState(parent: ‘PositionalUnidirectional’, position: int, _batch_size: int | None, input_tensor: torch.Tensor | None)
- __init__(parent, position, _batch_size, input_tensor)¶
- forward(input_sequence, include_first, return_state=False, return_output=True)¶
- Return type:
-
parent:
PositionalUnidirectional¶
- forward_at_position(input_tensor, position)¶
Compute the output for a single input at a certain position.
- Parameters:
- Return type:
- Returns:
A tensor of size \(B \times \cdots\) representing the output tensor corresponding to the input tensor.
- forward_from_position(input_sequence, position)¶
Compute the outputs for a sequence of inputs, starting at a certain position.
- Parameters:
- Return type:
- Returns:
A tensor of size \(B \times n' \times \cdots\) representing a sequence of output tensors.
- class rau.unidirectional.ComposedUnidirectional¶
Bases:
UnidirectionalStacks one undirectional model on another, so that the outputs of the first are fed as inputs to the second.
- __init__(first, second)¶
- forward(input_sequence, *args, initial_state=None, return_state=False, include_first=True, tag_kwargs=None, **kwargs)¶
- Return type:
- class rau.unidirectional.DropoutUnidirectional¶
Bases:
StatelessLayerUnidirectional- __init__(dropout)¶
- class rau.unidirectional.EmbeddingUnidirectional¶
Bases:
StatelessLayerUnidirectional- __init__(vocabulary_size, output_size, use_padding, shared_embeddings=None)¶
- class rau.unidirectional.OutputUnidirectional¶
Bases:
StatelessLayerUnidirectional- __init__(input_size, vocabulary_size, shared_embeddings=None, bias=True)¶
- class rau.unidirectional.ResidualUnidirectional¶
Bases:
Unidirectional- class State¶
Bases:
StateState(input_tensor: torch.Tensor | None, wrapped_state: rau.unidirectional.unidirectional.Unidirectional.State)
- __init__(input_tensor, wrapped_state)¶
- forward(input_sequence, include_first, return_state=False, return_output=True)¶
- Return type:
- __init__(module)¶