Trait relearn::torch::seq_modules::SequenceModule [−][src]
pub trait SequenceModule {
fn seq_serial(&self, inputs: &Tensor, seq_lengths: &[usize]) -> Tensor;
fn seq_packed(&self, inputs: &Tensor, batch_sizes: &Tensor) -> Tensor;
}
Expand description
A network module that operates on a sequence of data.
Required methods
Apply the network over multiple sequences arranged in series one after another.
input.i(.., ..seq_lengths[0], ..)
is the first batch of sequences,
input.i(.., seq_lengths[0]..(seq_lengths[0]+seq_lengths[1]), ..)
is the second, etc.
Args
inputs
- Batched input sequences arranged in series. An f32 tensor of shape[BATCH_SIZE, TOTAL_SEQ_LENGTH, NUM_INPUT_FEATURES]
seq_lengths
- Length of each sequence. The sequence length is the same across the batch dimension.
Returns
Batched output sequences arranged in series.
A tensor of shape [BATCH_SHAPE, TOTAL_SEQ_LENGTH, NUM_OUTPUT_FEATURES]
.
fn seq_packed(&self, inputs: &Tensor, batch_sizes: &Tensor) -> Tensor
fn seq_packed(&self, inputs: &Tensor, batch_sizes: &Tensor) -> Tensor
Apply the network over multiple sequences packed together in heterogeneous batches.
input.i(0..batch_sizes[0], ..)
are the batched first steps of all sequences,
input.i(batch_sizes[0]..(batch_sizes[0]+batch_sizes[1]), ..)
are the second steps, etc.
Args
-
inputs
- Packed input sequences. An f32 tensor of shape[TOTAL_STEPS, NUM_INPUT_FEATURES]
where theTOTAL_STEPS
dimension consists of the packed and batched steps ordered first by index within a sequence, then by batch index. Sequences must be ordered from longest to shortest.If all sequences have the same length then the
TOTAL_STEPS
dimension corresponds to a flattend Tensor of shape[SEQ_LENGTH, BATCH_SIZE]
. -
batch_sizes
- The batch size of each in-sequence step index. A i64 tensor of shape[MAX_SEQ_LENGTH]
. Must be on the CPU. Must be monotonically decreasing and positive.
Returns
Packed output sequences in the same order as inputs
.
A tensor of shape [TOTAL_STEPS, NUM_OUTPUT_FEATURES]
.
Panics
Panics if:
inputs
device does not match the model deviceinputs
NUM_INPUT_FEATURES
dimension does not match the model input featuresinputs
TOTAL_STEPS
dimension does not match the sum ofbatch_size
batch_sizes
device is not CPU