LayerDict

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

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

参数:

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

返回:无

代码示例

  1. import paddle
  2. import numpy as np
  3. from collections import OrderedDict
  4. sublayers = OrderedDict([
  5. ('conv1d', paddle.nn.Conv1D(3, 2, 3)),
  6. ('conv2d', paddle.nn.Conv2D(3, 2, 3)),
  7. ('conv3d', paddle.nn.Conv3D(4, 6, (3, 3, 3))),
  8. ])
  9. layers_dict = paddle.nn.LayerDict(sublayers=sublayers)
  10. l = layers_dict['conv1d']
  11. for k in layers_dict:
  12. l = layers_dict[k]
  13. len(layers_dict)
  14. #3
  15. del layers_dict['conv2d']
  16. len(layers_dict)
  17. #2
  18. conv1d = layers_dict.pop('conv1d')
  19. len(layers_dict)
  20. #1
  21. layers_dict.clear()
  22. len(layers_dict)
  23. #0

clear ( )

清除LayerDict 中所有的子层。

参数:

无。

代码示例

  1. import paddle
  2. from collections import OrderedDict
  3. sublayers = OrderedDict([
  4. ('conv1d', paddle.nn.Conv1D(3, 2, 3)),
  5. ('conv2d', paddle.nn.Conv2D(3, 2, 3)),
  6. ('conv3d', paddle.nn.Conv3D(4, 6, (3, 3, 3))),
  7. ])
  8. layer_dict = paddle.nn.LayerDict(sublayers=sublayers)
  9. len(layer_dict)
  10. #3
  11. layer_dict.clear()
  12. len(layer_dict)
  13. #0

pop ( )

移除LayerDict 中的键 并且返回该键对应的子层。

参数:

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

代码示例

  1. import paddle
  2. from collections import OrderedDict
  3. sublayers = OrderedDict([
  4. ('conv1d', paddle.nn.Conv1D(3, 2, 3)),
  5. ('conv2d', paddle.nn.Conv2D(3, 2, 3)),
  6. ('conv3d', paddle.nn.Conv3D(4, 6, (3, 3, 3))),
  7. ])
  8. layer_dict = paddle.nn.LayerDict(sublayers=sublayers)
  9. len(layer_dict)
  10. #3
  11. layer_dict.pop('conv2d')
  12. len(layer_dict)
  13. #2

keys ( )

返回LayerDict 中键的可迭代对象。

参数:

无。

代码示例

  1. import paddle
  2. from collections import OrderedDict
  3. sublayers = OrderedDict([
  4. ('conv1d', paddle.nn.Conv1D(3, 2, 3)),
  5. ('conv2d', paddle.nn.Conv2D(3, 2, 3)),
  6. ('conv3d', paddle.nn.Conv3D(4, 6, (3, 3, 3))),
  7. ])
  8. layer_dict = paddle.nn.LayerDict(sublayers=sublayers)
  9. for k in layer_dict.keys():
  10. print(k)
  11. #conv1d
  12. #conv2d
  13. #conv3d

items ( )

返回LayerDict 中键/值对的可迭代对象。

参数:

无。

代码示例

  1. import paddle
  2. from collections import OrderedDict
  3. sublayers = OrderedDict([
  4. ('conv1d', paddle.nn.Conv1D(3, 2, 3)),
  5. ('conv2d', paddle.nn.Conv2D(3, 2, 3)),
  6. ('conv3d', paddle.nn.Conv3D(4, 6, (3, 3, 3))),
  7. ])
  8. layer_dict = paddle.nn.LayerDict(sublayers=sublayers)
  9. for k, v in layer_dict.items():
  10. print(k, ":", v)
  11. #conv1d : Conv1D(3, 2, kernel_size=[3], data_format=NCL)
  12. #conv2d : Conv2D(3, 2, kernel_size=[3, 3], data_format=NCHW)
  13. #conv3d : Conv3D(4, 6, kernel_size=[3, 3, 3], data_format=NCDHW)

values ( )

返回LayerDict 中值的可迭代对象。

参数:

无。

代码示例

  1. import paddle
  2. from collections import OrderedDict
  3. sublayers = OrderedDict([
  4. ('conv1d', paddle.nn.Conv1D(3, 2, 3)),
  5. ('conv2d', paddle.nn.Conv2D(3, 2, 3)),
  6. ('conv3d', paddle.nn.Conv3D(4, 6, (3, 3, 3))),
  7. ])
  8. layer_dict = paddle.nn.LayerDict(sublayers=sublayers)
  9. for v in layer_dict.values():
  10. print(v)
  11. #Conv1D(3, 2, kernel_size=[3], data_format=NCL)
  12. #Conv2D(3, 2, kernel_size=[3, 3], data_format=NCHW)
  13. #Conv3D(4, 6, kernel_size=[3, 3, 3], data_format=NCDHW)

update ( )

更新子层中的键/值对到LayerDict中,会覆盖已经存在的键。

参数:

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

代码示例

  1. import paddle
  2. from collections import OrderedDict
  3. sublayers = OrderedDict([
  4. ('conv1d', paddle.nn.Conv1D(3, 2, 3)),
  5. ('conv2d', paddle.nn.Conv2D(3, 2, 3)),
  6. ('conv3d', paddle.nn.Conv3D(4, 6, (3, 3, 3))),
  7. ])
  8. new_sublayers = OrderedDict([
  9. ('relu', paddle.nn.ReLU()),
  10. ('conv2d', paddle.nn.Conv2D(4, 2, 4)),
  11. ])
  12. layer_dict = paddle.nn.LayerDict(sublayers=sublayers)
  13. layer_dict.update(new_sublayers)
  14. for k, v in layer_dict.items():
  15. print(k, ":", v)
  16. #conv1d : Conv1D(3, 2, kernel_size=[3], data_format=NCL)
  17. #conv2d : Conv2D(4, 2, kernel_size=[4, 4], data_format=NCHW)
  18. #conv3d : Conv3D(4, 6, kernel_size=[3, 3, 3], data_format=NCDHW)
  19. #relu : ReLU()