LoDTensor

class paddle.fluid.LoDTensor

LoDTensor is a Tensor with optional LoD (Level of Details) information, it can be used for variable-length sequences, see LoD-Tensor User Guide for details.

LoDTensor can be converted to numpy array using numpy.array(lod_tensor).

You can skip the following explanation if you don’t need to know details of LoDTensor.

The following two examples show how to use LODtensor to represent variable-length sequences.

Example 1:

Suppose x is a LoDTensor representing a variable-length sequence. It contains two logical subsequences, the length of first logical sequence is 2 (e.g., number of samples is 2), the length of second logical sequence is 3, and the total length is 5. The data of the first logical sequence is [1, 2], [3, 4], and the data of the second logical sequence is [5, 6], [7, 8], [9, 10]. The data dimension of each sample is 2. So, the final shape of the LoDTensor is [5, 2], of which 5 is the total length and 2 is the dimension of each sample.

Logically, we can represent the variable-length sequence in two ways: one is in the form of recursive sequence lengths, that is, x.recursive_sequence_lengths=[[2, 3]]; the other is in the form of offsets, that is, x.lod=[[0, 2, 2+3]]. These two representations are equivalent, and you can set and retrieve recursive_sequence_lengths or LoD through the corresponding interfaces of LoDTensor introduced later.

Actually, in order to access sequence faster, Paddle uses offset to store different lengths of sequences. Therefore, the operations on recursive_sequence_lengths will be converted to the operations on LoD eventually.

y.data = [[1, 2], [3, 4],
          [5, 6], [7, 8],
          [9, 10], [11, 12], [13, 14]]

y.shape = [2+2+3, 2]

y.recursive_sequence_lengths = [[2, 1], [2, 2, 3]]

y.lod = [[0, 2, 3], [0, 2, 4, 7]]

Example 2:

LoD may have more than one level (for example, a paragraph may have more than one sentence and a sentence may have more than one word). Suppose y is a LoDTensor and its lod_level is 2. From level = 0, there are two logical sequences, the length of which is 2 and 1, respectively, indicating that the first logical sequence contains two sub-sequences and the second logical sequence contains one sub-sequence. From level = 1, the lengths of two sub-sequences contained by the first logical sequence is 2 and 2, and the length of sub-sequence contained by the second logical sequence is 3.

Therefore, the LoDTensor is represented in the form of recursive sequence lengths as y.recursive_sequence_lengths=[[2,1], [2,2,3]]; and equally, in the form of offset, it is represented as y.lod=[[0,2,3], [0,2,4,7]].

y.data = [[1, 2], [3, 4],
          [5, 6], [7, 8],
          [9, 10], [11, 12], [13, 14]]

y.shape = [2+2+3, 2]

y.recursive_sequence_lengths = [[2, 1], [2, 2, 3]]

y.lod = [[0, 2, 3], [0, 2, 4, 7]]

Examples

import paddle.fluid as fluid

t = fluid.LoDTensor()
has_valid_recursive_sequence_lengths(self: paddle.fluid.core_avx.LoDTensor) → bool

Check whether the LoD of the LoDTensor is valid.

Returns

Whether the LoD is valid.

Return type

bool

Examples

import paddle.fluid as fluid
import numpy as np

t = fluid.LoDTensor()
t.set(np.ndarray([5, 30]), fluid.CPUPlace())
t.set_recursive_sequence_lengths([[2, 3]])
print(t.has_valid_recursive_sequence_lengths()) # True
lod(self: paddle.fluid.core_avx.LoDTensor) → List[List[int]]

Return the LoD of the LoDTensor.

Returns

The lod of the LoDTensor.

Return type

list[list[int]]

Examples

import paddle.fluid as fluid
import numpy as np

t = fluid.LoDTensor()
t.set(np.ndarray([5, 30]), fluid.CPUPlace())
t.set_lod([[0, 2, 5]])
print(t.lod()) # [[0, 2, 5]]
recursive_sequence_lengths(self: paddle.fluid.core_avx.LoDTensor) → List[List[int]]

Return the recursive sequence lengths corresponding to of the LodD of the LoDTensor.

Returns

The recursive sequence lengths.

Return type

list[list[int]]

Examples

import paddle.fluid as fluid
import numpy as np

t = fluid.LoDTensor()
t.set(np.ndarray([5, 30]), fluid.CPUPlace())
t.set_recursive_sequence_lengths([[2, 3]])
print(t.recursive_sequence_lengths()) # [[2, 3]]
set(*args, **kwargs)

Overloaded function.

  1. set(self: paddle.fluid.core_avx.Tensor, array: object, place: paddle::platform::CPUPlace, zero_copy: bool=False) -> None

  2. set(self: paddle.fluid.core_avx.Tensor, array: object, place: paddle::platform::CUDAPlace, zero_copy: bool=False) -> None

  3. set(self: paddle.fluid.core_avx.Tensor, array: object, place: paddle::platform::CUDAPinnedPlace, zero_copy: bool=False) -> None

    Set the data of LoDTensor on place with given numpy array.

    Args:

    lod (numpy.ndarray): The data to set. place (CPUPlace|CUDAPlace|CUDAPinnedPlace): The place where the LoDTensor is to be set. zero_copy (bool, optional): Whether to share memory with the input numpy array. This parameter only works with CPUPlace. Default: False.

    Returns:

    None.

    Examples:
    import paddle.fluid as fluid
    import numpy as np
    
    t = fluid.LoDTensor()
    t.set(np.ndarray([5, 30]), fluid.CPUPlace())
    
set_lod(self: paddle.fluid.core_avx.LoDTensor, lod: List[List[int]]) → None

Set LoD of the LoDTensor.

Parameters

lod (list[list[int]]) – The lod to set.

Returns

None.

Examples

import paddle.fluid as fluid
import numpy as np

t = fluid.LoDTensor()
t.set(np.ndarray([5, 30]), fluid.CPUPlace())
t.set_lod([[0, 2, 5]])
print(t.lod()) # [[0, 2, 5]]
set_recursive_sequence_lengths(self: paddle.fluid.core_avx.LoDTensor, recursive_sequence_lengths: List[List[int]]) → None

Set LoD of the LoDTensor according to recursive sequence lengths.

For example, if recursive_sequence_lengths=[[2, 3]], which means there are two sequences with length 2 and 3 respectively, the corresponding lod would be [[0, 2, 2+3]], i.e., [[0, 2, 5]].

Parameters

recursive_sequence_lengths (list[list[int]]) – The recursive sequence lengths.

Returns

None.

Examples

import paddle.fluid as fluid
import numpy as np

t = fluid.LoDTensor()
t.set(np.ndarray([5, 30]), fluid.CPUPlace())
t.set_recursive_sequence_lengths([[2, 3]])
print(t.recursive_sequence_length())  # [[2, 3]]
print(t.lod())  # [[0, 2, 5]]
shape(self: paddle.fluid.core_avx.Tensor) → List[int]

Return the shape of LoDTensor.

Returns

The shape of LoDTensor.

Return type

list[int]

Examples

import paddle.fluid as fluid
import numpy as np

t = fluid.LoDTensor()
t.set(np.ndarray([5, 30]), fluid.CPUPlace())
print(t.shape())  # [5, 30]