# LoDTensor¶

class `paddle.fluid.``LoDTensor`

LoDTensor是一个具有LoD（Level of Details）信息的张量（Tensor），可用于表示变长序列，详见 LoDTensor

LoDTensor可以通过 `np.array(lod_tensor)` 方法转换为numpy.ndarray。

```x.data = [[1, 2], [3, 4],
[5, 6], [7, 8], [9, 10]]

x.shape = [5, 2]

x.recursive_sequence_length = [[2, 3]]

x.lod  =  [[0, 2, 5]]
```

LoD可以有多个level（例如，一个段落可以有多个句子，一个句子可以有多个单词）。假设y为LoDTensor ，lod_level为2。从level=0来看有2个逻辑序列，序列长度分别为2和1，表示第一个逻辑序列包含2个子序列，第二个逻辑序列包含1个子序列。从level=1来看，第一个逻辑序列包含的2个子序列长度分别为2和2，第二个逻辑序列包含的1个子序列长度为3。

```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_length = [[2, 1], [2, 2, 3]]

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

## 代码示例¶

```import paddle.fluid as fluid

t = fluid.LoDTensor()
```

## 方法¶

bool。

```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
```

```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]]
```

```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)¶

• array (numpy.ndarray) - 要设置的numpy array，支持的数据类型为bool, float32, float64, int8, int32, int64, uint8, uint16。
• place (CPUPlace|CUDAPlace|CUDAPinnedPlace) - 要设置的LoDTensor所在的设备。

```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¶

• lod （List [List [int]]） - 要设置的LoD。

```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¶

• recursive_sequence_lengths (List [List [int]]) - 递归序列长度。

```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]]
```

```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]
```