Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support minus-int idx to LayerList #31750

Merged
merged 11 commits into from
Mar 31, 2021

Conversation

lyuwenyu
Copy link
Contributor

@lyuwenyu lyuwenyu commented Mar 19, 2021

PR types

New features

PR changes

APIs

Describe

  • support minux-idx for LayerList
  • __getitem__
  • __setitem__
  • __del__

Before

  • getitem problem
  • setitem problem

will get surprising result here

In [80]: mm = nn.LayerList([nn.Conv2D(2, 2, 2, 2), nn.Linear(10, 10)])

In [81]: mm[-1]
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-81-0325fd583382> in <module>
----> 1 mm[-1]

~/opt/anaconda3/lib/python3.8/site-packages/paddle/fluid/dygraph/container.py in __getitem__(self, idx)
    218             return self.__class__(list(self._sub_layers.values())[idx])
    219         else:
--> 220             return self._sub_layers[str(idx)]
    221 
    222     def __setitem__(self, idx, sublayer):

KeyError: '-1'

In [82]: mm[-1] = nn.ReLU()

In [83]: mm
Out[83]: 
LayerList(
  (0): Conv2D(2, 2, kernel_size=[2, 2], stride=[2, 2], data_format=NCHW)
  (1): Linear(in_features=10, out_features=10, dtype=float32)
  (-1): ReLU()
)

After

In [74]: mm = LayerList([nn.Conv2D(2, 2, 2, 2), nn.Linear(10, 10), nn.ReLU()])

In [75]: mm[-1]
Out[75]: ReLU()

In [76]: mm[-1] = nn.Sigmoid()

In [77]: mm
Out[77]: 
LayerList(
  (0): Conv2D(2, 2, kernel_size=[2, 2], stride=[2, 2], data_format=NCHW)
  (1): Linear(in_features=10, out_features=10, dtype=float32)
  (2): Sigmoid()
)

In [78]: del mm[-1]

In [79]: mm
Out[79]: 
LayerList(
  (0): Conv2D(2, 2, kernel_size=[2, 2], stride=[2, 2], data_format=NCHW)
  (1): Linear(in_features=10, out_features=10, dtype=float32)
)

@paddle-bot-old
Copy link

Thanks for your contribution!
Please wait for the result of CI firstly. See Paddle CI Manual for details.

@lyuwenyu lyuwenyu changed the title add support minus-int idx to LayerList support minus-int idx to LayerList Mar 19, 2021
qingqing01
qingqing01 previously approved these changes Mar 19, 2021
Copy link
Contributor

@qingqing01 qingqing01 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

qingqing01
qingqing01 previously approved these changes Mar 23, 2021
Copy link
Contributor

@qingqing01 qingqing01 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. @phlrain Please help to look

heavengate
heavengate previously approved these changes Mar 23, 2021
Copy link

@saxon-zh saxon-zh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Contributor

@jzhang533 jzhang533 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@qingqing01 qingqing01 merged commit 5394194 into PaddlePaddle:develop Mar 31, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants