# PackedSequence

Source: https://www.tensorplay.cn/docs/generated/tensorplay.nn.utils.rnn.PackedSequence.html

# PackedSequence

class tensorplay.nn.utils.rnn.PackedSequence(data, batch_sizes=None, sorted_indices=None, unsorted_indices=None)[[source]](../_modules/tensorplay/nn/utils/rnn.html#PackedSequence)

Holds the data and list of [batch_sizes](#id0) 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()](tensorplay.nn.utils.rnn.pack_padded_sequence.html#tensorplay.nn.utils.rnn.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()](tensorplay.nn.utils.rnn.pack_padded_sequence.html#tensorplay.nn.utils.rnn.pack_padded_sequence).  For instance, given data abc and x
the [PackedSequence](#tensorplay.nn.utils.rnn.PackedSequence) would contain data axbc with
batch_sizes=[2,1,1].

data

Tensor containing packed sequence

Type:

Tensor

batch_sizes

Tensor of integers holding
information about the batch size at each sequence step

Type:

Tensor

sorted_indices

Tensor of integers holding how this
[PackedSequence](#tensorplay.nn.utils.rnn.PackedSequence) is constructed from sequences.

Type:

Tensor, optional

unsorted_indices

Tensor of integers holding how
this to recover the original sequences with correct order.

Type:

Tensor, optional

Note

[data](#id1) can be on arbitrary device and of arbitrary dtype.
[sorted_indices](#id2) and [unsorted_indices](#id3) must be int64
tensors on the same device as [data](#id1).

However, [batch_sizes](#id0) should always be a CPU int64 tensor.

This invariant is maintained throughout [PackedSequence](#tensorplay.nn.utils.rnn.PackedSequence) class,
and all functions that construct a [PackedSequence](#tensorplay.nn.utils.rnn.PackedSequence) in TensorPlay
(i.e. they only pass in tensors conforming to this constraint).

batch_sizes: TensorBase

Alias for field number 1

count(value, /)

Return number of occurrences of value.

data: TensorBase

Alias for field number 0

index(value, start=0, stop=9223372036854775807, /)

Return first index of value.

Raises ValueError if the value is not present.

property is_cuda: [bool](https://docs.python.org/3/library/functions.html#bool)

Return true if self.data stored on a gpu.

is_pinned() &#x2192; [bool](https://docs.python.org/3/library/functions.html#bool)[[source]](../_modules/tensorplay/nn/utils/rnn.html#PackedSequence.is_pinned)

Return true if self.data stored on in pinned memory.

sorted_indices: TensorBase | [None](https://docs.python.org/3/library/constants.html#None)

Alias for field number 2

to(*args: [Any](https://docs.python.org/3/library/typing.html#typing.Any), **kwargs: [Any](https://docs.python.org/3/library/typing.html#typing.Any))[[source]](../_modules/tensorplay/nn/utils/rnn.html#PackedSequence.to)

Perform dtype and/or device conversion on self.data.

It has similar signature as tensorplay.Tensor.to()

Note

If the self.data Tensor already has the correct
[tensorplay.DType](tensorplay.nn.functional.DType.html#tensorplay.nn.functional.DType) and [tensorplay.Device](tensorplay.Device.html#tensorplay.Device), then
self is returned.  Otherwise, returns a copy with the desired
configuration.

unsorted_indices: TensorBase | [None](https://docs.python.org/3/library/constants.html#None)

Alias for field number 3
