# LayerList¶

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

LayerList 用于保存子层列表，它包含的子层将被正确地注册和添加。列表中的子层可以像常规 python 列表一样被索引。

## 参数¶

• sublayers (iterable，可选) - 要保存的子层。

## 代码示例¶

```import paddle
import numpy as np

class MyLayer(paddle.nn.Layer):
def __init__(self):
super(MyLayer, self).__init__()
self.linears = paddle.nn.LayerList(
[paddle.nn.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 (Layer) - 要添加的子层。

```import paddle

linears = paddle.nn.LayerList([paddle.nn.Linear(10, 10) for i in range(10)])
another = paddle.nn.Linear(10, 10)
linears.append(another)
print(len(linears))  # 11
```

### insert()¶

• **index ** (int) - 要插入的位置。

• sublayers (Layer) - 要插入的子层。

```import paddle

linears = paddle.nn.LayerList([paddle.nn.Linear(10, 10) for i in range(10)])
another = paddle.nn.Linear(10, 10)
linears.insert(3, another)
print(linears[3] is another)  # True
```

### extend()¶

• sublayers (iterable of Layer) - 要添加的所有子层。

```import paddle

linears = paddle.nn.LayerList([paddle.nn.Linear(10, 10) for i in range(10)])
another_list = paddle.nn.LayerList([paddle.nn.Linear(10, 10) for i in range(5)])
linears.extend(another_list)
print(len(linears))  # 15
print(another_list[0] is linears[10])  # True
```