From e41fd95c3aec4a34b1bf1d2bdacba54f43dc7c99 Mon Sep 17 00:00:00 2001 From: GGBond8488 <33050871+GGBond8488@users.noreply.github.com> Date: Tue, 29 Nov 2022 17:03:53 +0800 Subject: [PATCH] remove paddle.nn.Sequential to fix dygraph to static error (#48477) --- python/paddle/nn/__init__.py | 2 +- python/paddle/nn/layer/container.py | 73 ----------------------------- 2 files changed, 1 insertion(+), 74 deletions(-) diff --git a/python/paddle/nn/__init__.py b/python/paddle/nn/__init__.py index a4aaa18ea5552..ad966683aede2 100644 --- a/python/paddle/nn/__init__.py +++ b/python/paddle/nn/__init__.py @@ -17,7 +17,7 @@ from ..fluid.dygraph.layers import Layer # noqa: F401 from .layer.container import LayerList # noqa: F401 from .layer.container import ParameterList # noqa: F401 -from .layer.container import Sequential # noqa: F401 +from ..fluid.dygraph.container import Sequential # noqa: F401 from .clip import ClipGradByGlobalNorm # noqa: F401 from .clip import ClipGradByNorm # noqa: F401 diff --git a/python/paddle/nn/layer/container.py b/python/paddle/nn/layer/container.py index 0a8e5ec009d02..6a54a20787c18 100644 --- a/python/paddle/nn/layer/container.py +++ b/python/paddle/nn/layer/container.py @@ -299,79 +299,6 @@ def update(self, sublayers): self.add_sublayer(kv[0], kv[1]) -class Sequential(Layer): - """Sequential container. - Sub layers will be added to this container in the order of argument in the constructor. - The argument passed to the constructor can be iterable Layers or iterable name Layer pairs. - - Parameters: - layers(Layer|list|tuple): Layer or list/tuple of iterable name Layer pair. - - Examples: - .. code-block:: python - - import paddle - - data = paddle.uniform(shape=[30, 10], dtype='float32') - # create Sequential with iterable Layers - model1 = paddle.nn.Sequential( - paddle.nn.Linear(10, 1), paddle.nn.Linear(1, 2) - ) - model1[0] # access the first layer - res1 = model1(data) # sequential execution - - # create Sequential with name Layer pairs - model2 = paddle.nn.Sequential( - ('l1', paddle.nn.Linear(10, 2)), - ('l2', paddle.nn.Linear(2, 3)) - ) - model2['l1'] # access l1 layer - model2.add_sublayer('l3', paddle.nn.Linear(3, 3)) # add sublayer - res2 = model2(data) # sequential execution - - """ - - def __init__(self, *layers): - super().__init__() - if len(layers) > 0 and isinstance(layers[0], (list, tuple)): - for name, layer in layers: - self.add_sublayer(name, layer) - else: - for idx, layer in enumerate(layers): - self.add_sublayer(str(idx), layer) - - def __getitem__(self, name): - if isinstance(name, slice): - return self.__class__(*(list(self._sub_layers.values())[name])) - elif isinstance(name, str): - return self._sub_layers[name] - else: - if name >= len(self._sub_layers): - raise IndexError('index {} is out of range'.format(name)) - elif name < 0 and name >= -len(self._sub_layers): - name += len(self._sub_layers) - elif name < -len(self._sub_layers): - raise IndexError('index {} is out of range'.format(name)) - return list(self._sub_layers.values())[name] - - def __setitem__(self, name, layer): - assert isinstance(layer, Layer) - setattr(self, str(name), layer) - - def __delitem__(self, name): - name = str(name) - assert name in self._sub_layers - del self._sub_layers[name] - - def __len__(self): - return len(self._sub_layers) - - def forward(self, input): - for layer in self._sub_layers.values(): - input = layer(input) - return input - - class ParameterList(Layer): """ParameterList Container.