LayerList

class paddle.fluid.dygraph.LayerList(sublayers=None)[source]

LayerList holds sublayers, and sublayers it contains are properly registered. Holded sublayers can be indexed like a regular python list.

Parameters

sublayers (iterable of Layer, optional) – sublayers to hold

Examples

System Message: ERROR/3 (/usr/local/lib/python2.7/dist-packages/paddle/fluid/dygraph/container.py:docstring of paddle.fluid.dygraph.LayerList, line 9)

Error in “code-block” directive: maximum 1 argument(s) allowed, 9 supplied.

.. code-block:: python
    import paddle.fluid as fluid
    import numpy as np

    class MyLayer(fluid.Layer):
        def __init__(self):
            super(MyLayer, self).__init__()
            self.linears = fluid.dygraph.LayerList(
                [fluid.dygraph.Linear(10, 10) for i in range(10)])

        def forward(self, x):
            # LayerList can act as an iterable, or be indexed using ints
            for i, l in enumerate(self.linears):
                x = self.linears[i // 2](x) + l(x)
            return x

append(sublayer)

Appends a sublayer to the end of the list.

Parameters

sublayer (Layer) – sublayer to append

Examples

System Message: ERROR/3 (/usr/local/lib/python2.7/dist-packages/paddle/fluid/dygraph/container.py:docstring of paddle.fluid.dygraph.LayerList.append, line 8)

Error in “code-block” directive: maximum 1 argument(s) allowed, 5 supplied.

.. code-block:: python
    import paddle.fluid as fluid

    with fluid.dygraph.guard():
        linears = fluid.dygraph.LayerList([fluid.dygraph.Linear(10, 10) for i in range(10)])
        another = fluid.dygraph.Linear(10, 10)
        linears.append(another)
        print(len(linears))  # 11
insert(index, sublayer)

Insert a sublayer before a given index in the list.

Parameters
  • index (int) – index to insert.

  • sublayer (Layer) – sublayer to insert

Examples

System Message: ERROR/3 (/usr/local/lib/python2.7/dist-packages/paddle/fluid/dygraph/container.py:docstring of paddle.fluid.dygraph.LayerList.insert, line 10)

Error in “code-block” directive: maximum 1 argument(s) allowed, 5 supplied.

.. code-block:: python
    import paddle.fluid as fluid

    with fluid.dygraph.guard():
        linears = fluid.dygraph.LayerList([fluid.dygraph.Linear(10, 10) for i in range(10)])
        another = fluid.dygraph.Linear(10, 10)
        linears.insert(3, another)
        print(linears[3] is another)  # True
extend(sublayers)

Appends sublayers to the end of the list.

Parameters

sublayers (iterable of Layer) – iterable of sublayers to append

Examples

System Message: ERROR/3 (/usr/local/lib/python2.7/dist-packages/paddle/fluid/dygraph/container.py:docstring of paddle.fluid.dygraph.LayerList.extend, line 8)

Error in “code-block” directive: maximum 1 argument(s) allowed, 5 supplied.

.. code-block:: python
    import paddle.fluid as fluid

    with fluid.dygraph.guard():
        linears = fluid.dygraph.LayerList([fluid.dygraph.Linear(10, 10) for i in range(10)])
        another_list = fluid.dygraph.LayerList([fluid.dygraph.Linear(10, 10) for i in range(5)])
        linears.extend(another_list)
        print(len(linears))  # 15
        print(another_list[0] is linears[10])  # True