未验证 提交 cb36478a 编写于 作者: S songyouwei 提交者: GitHub

add LayerList insert and extend (#23377)

* add LayerList insert and extend
test=develop

* add index range check
test=develop

* add sample codes
test=develop

* refine sample code
test=develop
上级 b59426b5
...@@ -236,6 +236,65 @@ class LayerList(Layer): ...@@ -236,6 +236,65 @@ class LayerList(Layer):
Parameters: Parameters:
sublayer (Layer): sublayer to append sublayer (Layer): sublayer to append
Examples:
.. 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
""" """
self.add_sublayer(str(len(self)), sublayer) self.add_sublayer(str(len(self)), sublayer)
return self return self
def insert(self, index, sublayer):
"""
Insert a sublayer before a given index in the list.
Parameters:
index (int): index to insert.
sublayer (Layer): sublayer to insert
Examples:
.. 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
"""
assert isinstance(index, int) and \
0 <= index < len(self._sub_layers), \
"index should be an integer in range [0, len(self))"
for i in range(len(self._sub_layers), index, -1):
self._sub_layers[str(i)] = self._sub_layers[str(i - 1)]
self._sub_layers[str(index)] = sublayer
def extend(self, sublayers):
"""
Appends sublayers to the end of the list.
Parameters:
sublayers (iterable of Layer): iterable of sublayers to append
Examples:
.. 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
"""
offset = len(self)
for i, sublayer in enumerate(sublayers):
idx = str(offset + i)
self.add_sublayer(idx, sublayer)
return self
...@@ -30,8 +30,8 @@ class MyLayer(fluid.Layer): ...@@ -30,8 +30,8 @@ class MyLayer(fluid.Layer):
return x return x
class TestImperativeContainerParameterList(unittest.TestCase): class TestImperativeContainer(unittest.TestCase):
def test_paramter_list(self): def test_layer_list(self):
data_np = np.random.uniform(-1, 1, [5, 1]).astype('float32') data_np = np.random.uniform(-1, 1, [5, 1]).astype('float32')
with fluid.dygraph.guard(): with fluid.dygraph.guard():
x = fluid.dygraph.to_variable(data_np) x = fluid.dygraph.to_variable(data_np)
...@@ -61,6 +61,20 @@ class TestImperativeContainerParameterList(unittest.TestCase): ...@@ -61,6 +61,20 @@ class TestImperativeContainerParameterList(unittest.TestCase):
self.assertListEqual(res6.shape, [5, 2**(0 + 1)]) self.assertListEqual(res6.shape, [5, 2**(0 + 1)])
res6.backward() res6.backward()
model3 = MyLayer(layerlist[:-2])
model3.layerlist.append(fluid.dygraph.Linear(3, 1))
model3.layerlist.insert(size - 2,
fluid.dygraph.Linear(2**(size - 2), 3))
res7 = model3(x)
self.assertListEqual(res7.shape, [5, 1])
to_be_extended = [
fluid.dygraph.Linear(3**i, 3**(i + 1)) for i in range(3)
]
model3.layerlist.extend(to_be_extended)
res8 = model3(x)
self.assertListEqual(res8.shape, [5, 3**3])
res8.backward()
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册