# LayerDict¶

class paddle.nn. LayerDict ( sublayers=None ) [源代码]

LayerDict 用于保存子层到有序字典中，它包含的子层将被正确地注册和添加。列表中的子层可以像常规 python 有序字典一样被访问。

## 参数¶

• sublayers (LayerDict|OrderedDict|list[(key, Layer)]，可选) - 键值对的可迭代对象，值的类型为 paddle.nn.Layer

## 代码示例¶

```>>> import paddle
>>> import numpy as np
>>> from collections import OrderedDict

>>> sublayers = OrderedDict([
...     ('conv3d', paddle.nn.Conv3D(4, 6, (3, 3, 3))),
>>> ])

>>> l = layers_dict['conv1d']

>>> for k in layers_dict:
...     l = layers_dict[k]
...
>>> print(len(layers_dict))
3

>>> del layers_dict['conv2d']
>>> print(len(layers_dict))
2

>>> conv1d = layers_dict.pop('conv1d')
>>> print(len(layers_dict))
1

>>> layers_dict.clear()
>>> print(len(layers_dict))
0
```

## 方法¶

### pop()¶

• key (str) - 要移除的 key。

```>>> import paddle
>>> from collections import OrderedDict

>>> sublayers = OrderedDict([
...     ('conv3d', paddle.nn.Conv3D(4, 6, (3, 3, 3))),
>>> ])

>>> len(layer_dict)
3

>>> layer_dict.pop('conv2d')
>>> len(layer_dict)
2
```

### keys()¶

```>>> import paddle
>>> from collections import OrderedDict

>>> sublayers = OrderedDict([
...     ('conv3d', paddle.nn.Conv3D(4, 6, (3, 3, 3))),
>>> ])

>>> for k in layer_dict.keys():
...     print(k)
conv1d
conv2d
conv3d
```

### items()¶

```>>> import paddle
>>> from collections import OrderedDict

>>> sublayers = OrderedDict([
...     ('conv3d', paddle.nn.Conv3D(4, 6, (3, 3, 3))),
>>> ])

>>> for k, v in layer_dict.items():
...     print(f"{k}:", v)
conv1d : Conv1D(3, 2, kernel_size=[3], data_format=NCL)
conv2d : Conv2D(3, 2, kernel_size=[3, 3], data_format=NCHW)
conv3d : Conv3D(4, 6, kernel_size=[3, 3, 3], data_format=NCDHW)
```

### values()¶

```>>> import paddle
>>> from collections import OrderedDict

>>> sublayers = OrderedDict([
...     ('conv3d', paddle.nn.Conv3D(4, 6, (3, 3, 3))),
>>> ])

>>> for v in layer_dict.values():
...     print(v)
Conv1D(3, 2, kernel_size=[3], data_format=NCL)
Conv2D(3, 2, kernel_size=[3, 3], data_format=NCHW)
Conv3D(4, 6, kernel_size=[3, 3, 3], data_format=NCDHW)
```

### update()¶

• sublayers (LayerDict|OrderedDict|list[(key, Layer)]) - 键值对的可迭代对象，值的类型为 paddle.nn.Layer

```>>> import paddle
>>> from collections import OrderedDict

>>> sublayers = OrderedDict([
...     ('conv3d', paddle.nn.Conv3D(4, 6, (3, 3, 3))),
>>> ])

>>> new_sublayers = OrderedDict([