Shortcuts

Class PackedSequence

Page Contents

Class Documentation

class PackedSequence

Holds the data and list of batch_sizes of a packed sequence.

All RNN modules accept packed sequences as inputs.

Note: Instances of this class should never be created manually. They are meant to be instantiated by functions like pack_padded_sequence.

Batch sizes represent the number elements at each sequence step in the batch, not the varying sequence lengths passed to pack_padded_sequence. For instance, given data abc and x the :class:PackedSequence would contain data axbc with batch_sizes=[2,1,1].

Attributes: data (Tensor): Tensor containing packed sequence batch_sizes (Tensor): Tensor of integers holding information about the batch size at each sequence step sorted_indices (Tensor, optional): Tensor of integers holding how this :class:PackedSequence is constructed from sequences. unsorted_indices (Tensor, optional): Tensor of integers holding how this to recover the original sequences with correct order.

.. note:: data can be on arbitrary device and of arbitrary dtype. sorted_indices and unsorted_indices must be torch::kInt64 tensors on the same device as data.

However, batch_sizes should always be a CPU torch::kInt64 tensor.

This invariant is maintained throughout PackedSequence class, and all functions that construct a PackedSequence in libtorch (i.e., they only pass in tensors conforming to this constraint).

Public Functions

inline explicit PackedSequence(Tensor data, Tensor batch_sizes, Tensor sorted_indices = {}, Tensor unsorted_indices = {})
inline const Tensor &data() const
inline const Tensor &batch_sizes() const
inline const Tensor &sorted_indices() const
inline const Tensor &unsorted_indices() const
inline PackedSequence pin_memory() const
inline PackedSequence to(TensorOptions options) const
inline PackedSequence cuda() const
inline PackedSequence cpu() const
inline bool is_cuda() const

Returns true if data_ stored on a gpu.

inline bool is_pinned() const

Returns true if data_ stored on in pinned memory.

Docs

Access comprehensive developer documentation for PyTorch

View Docs

Tutorials

Get in-depth tutorials for beginners and advanced developers

View Tutorials

Resources

Find development resources and get your questions answered

View Resources