2

I define a sub-class of PyTorch's Module in PyCharm and create an instance a:

from torch.nn import Module

class AModule(Module):
    def __init__(self):
        self.something = 10

    def __repr__(self):
        return "AModule"

a = AModule()

If I run the debugger and examine a, I can't see its attributes:

enter image description here

I checked and Module is written in Python (as opposed to being implemented in C), so why is that?

4
  • 1
    Part of this seems to be a problem with pytorch itself, somehow - I tried running the code through pdb and examining dir(a), which threw an error *** torch.nn.modules.module.ModuleAttributeError: 'AModule' object has no attribute '_parameters'. Various other questions I found by googling that error message seem to think that the way you're invoking Module could be to blame - what does your import statement look like, and can you edit that into your question? Commented Sep 1, 2020 at 3:47
  • 1
    That said, this seems like it might also expose a bug with PyCharm and the engine it's using for debug (it should probably handle a situation like this more gracefully than it seems to be doing). If no one posts a conclusive answer in a reasonable timeframe you might consider submitting a bug report. Commented Sep 1, 2020 at 3:48
  • Thanks, you're right that I am using Module incorrectly. I should have invoked Module's init in AModule's init. Feel free to submit an answer to get that credit; otherwise I will do it tomorrow. And yes, I agree PyCharm could handle it more gracefully. Commented Sep 1, 2020 at 4:04
  • Created youtrack.jetbrains.com/issue/PY-44277 Commented Sep 1, 2020 at 5:11

1 Answer 1

1

This is caused by not having properly initialized Module with a super call in the first like of __init__:

super(AModule, self).__init__()

However, PyCharm could have shown more useful information, so I created this issue.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.