rau.unidirectional
¶
- class rau.unidirectional.Unidirectional¶
Bases:
Module
An 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:
object
Represents 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, return_state, include_first)¶
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 lastState
of the module.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.
- 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 alist
containing 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
.
- states(input_sequence, include_first)¶
Feed a sequence of inputs to this state and generate all the states produced after each input.
- Parameters:
- Return type:
- Returns:
Sequence of states produced by reading
input_sequence
.
- __init__(tags=None)¶
Initialize internal Module state, shared by both nn.Module and ScriptModule.
- as_composable()¶
- Return type:
Composable
- 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 lastState
of 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_first
is 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
Tensor
or aForwardResult
that contains the output tensor. The output tensor will be of size \(B \times n+1 \times \cdots\) ifinclude_first
is 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 aslist
s inForwardResult.extra_outputs
. Ifreturn_state
is true, then the finalState
will be returned inForwardResult.state
. If there are no extra outputs and there is no state to return, just the output tensor is returned.
- initial_state(batch_size, *args, **kwargs)¶
Get the initial state of the RNN.
- main()¶
- Return type:
- tag(tag)¶
- Return type:
- class rau.unidirectional.ForwardResult¶
Bases:
object
The output of a call to
Unidirectional.forward()
orUnidirectional.State.forward()
.- __init__(output, extra_outputs, state)¶
- class rau.unidirectional.SimpleUnidirectional¶
Bases:
Unidirectional
A sequential module that has no temporal recurrence, but applies some function to every timestep.
- class State¶
Bases:
State
- __init__(parent, input_tensor, batch_size, args, kwargs)¶
- fastforward(input_sequence)¶
Feed a sequence of inputs to this state and return the resulting state.
- forward(input_sequence, return_state, include_first)¶
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 lastState
of the module.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.
- 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 alist
containing all the outputs across all timesteps.
- outputs(input_sequence, include_first)¶
Like
states()
, but return the states’ outputs.
- transform_tensors(func)¶
Return a copy of this state with all tensors passed through a function.
-
parent:
SimpleUnidirectional
¶
- 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.
- initial_state(batch_size, *args, **kwargs)¶
Get the initial state of the RNN.
- class rau.unidirectional.SimpleLayerUnidirectional¶
Bases:
SimpleUnidirectional
- __init__(func)¶
Initialize internal Module state, shared by both nn.Module and ScriptModule.
- forward_sequence(input_sequence, *args, **kwargs)¶
Transform a sequence of tensors.
- class rau.unidirectional.SimpleReshapingLayerUnidirectional¶
Bases:
SimpleLayerUnidirectional
- class rau.unidirectional.PositionalUnidirectional¶
Bases:
Unidirectional
- class State¶
Bases:
State
- __init__(parent, position, input_tensor)¶
- fastforward(input_sequence)¶
Feed a sequence of inputs to this state and return the resulting state.
- forward(input_sequence, return_state, include_first)¶
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 lastState
of the module.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.
- 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 alist
containing all the outputs across all timesteps.
- outputs(input_sequence, include_first)¶
Like
states()
, but return the states’ outputs.
- transform_tensors(func)¶
Return a copy of this state with all tensors passed through a function.
-
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:
Unidirectional
Stacks one undirectional model on another, so that the outputs of the first are fed as inputs to the second.
- class State¶
Bases:
State
State(first_state: rau.unidirectional.unidirectional.Unidirectional.State, second_state: rau.unidirectional.unidirectional.Unidirectional.State)
- __init__(first_state, second_state)¶
- fastforward(input_sequence)¶
Feed a sequence of inputs to this state and return the resulting state.
- forward(input_sequence, return_state, include_first)¶
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 lastState
of the module.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.
- 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 alist
containing all the outputs across all timesteps.
- outputs(input_sequence, include_first)¶
Like
states()
, but return the states’ outputs.
- slice_batch(s)¶
Return a copy of this state with only certain batch elements included, determined by the slice
s
.
- transform_tensors(func)¶
Return a copy of this state with all tensors passed through a function.
- __init__(first, second)¶
Initialize internal Module state, shared by both nn.Module and ScriptModule.
- forward(input_sequence, *args, initial_state=None, return_state=False, include_first=True, tag_kwargs=None, **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 lastState
of 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_first
is 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
Tensor
or aForwardResult
that contains the output tensor. The output tensor will be of size \(B \times n+1 \times \cdots\) ifinclude_first
is 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 aslist
s inForwardResult.extra_outputs
. Ifreturn_state
is true, then the finalState
will be returned inForwardResult.state
. If there are no extra outputs and there is no state to return, just the output tensor is returned.
- class rau.unidirectional.DropoutUnidirectional¶
Bases:
SimpleLayerUnidirectional
- __init__(dropout)¶
Initialize internal Module state, shared by both nn.Module and ScriptModule.
- class rau.unidirectional.EmbeddingUnidirectional¶
Bases:
SimpleLayerUnidirectional
- __init__(vocabulary_size, output_size, use_padding, shared_embeddings=None)¶
Initialize internal Module state, shared by both nn.Module and ScriptModule.
- class rau.unidirectional.OutputUnidirectional¶
Bases:
SimpleLayerUnidirectional
- __init__(input_size, vocabulary_size, shared_embeddings=None, bias=True)¶
Initialize internal Module state, shared by both nn.Module and ScriptModule.
- class rau.unidirectional.ResidualUnidirectional¶
Bases:
Unidirectional
- class State¶
Bases:
State
State(input_tensor: torch.Tensor | None, wrapped_state: rau.unidirectional.unidirectional.Unidirectional.State)
- __init__(input_tensor, wrapped_state)¶
- fastforward(input_sequence)¶
Feed a sequence of inputs to this state and return the resulting state.
- forward(input_sequence, return_state, include_first)¶
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 lastState
of the module.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.
- 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 alist
containing all the outputs across all timesteps.
- states(input_sequence, include_first)¶
Feed a sequence of inputs to this state and generate all the states produced after each input.
- Parameters:
- Return type:
- Returns:
Sequence of states produced by reading
input_sequence
.
- transform_tensors(func)¶
Return a copy of this state with all tensors passed through a function.
- __init__(module)¶
Initialize internal Module state, shared by both nn.Module and ScriptModule.
- forward(input_sequence, 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 lastState
of 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_first
is true, then the length of the output tensor will be \(n + 1\). Otherwise, it will be \(n\).args – Extra arguments passed to
initial_state()
.kwargs (
Any
) – Extra arguments passed toinitial_state()
.
- Return type:
- Returns:
A
Tensor
or aForwardResult
that contains the output tensor. The output tensor will be of size \(B \times n+1 \times \cdots\) ifinclude_first
is 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 aslist
s inForwardResult.extra_outputs
. Ifreturn_state
is true, then the finalState
will be returned inForwardResult.state
. If there are no extra outputs and there is no state to return, just the output tensor is returned.