1

I have a rather weird problem in python: I am trying to run this script (it is auto generated using MMddn, which converts one neural network model to another - but this background is irrelevant to this question - just an FYI):

https://zerobin.net/?a8436f2ae6791499#dhZsFWXc91YpvlHajIqLY74MdeP8pE98E3IELiAD3bw=

but when I execute (using another script which calls this script) it I get:

  File "/home/foo/ve_name/env_name/lib/python3.7/site-packages/torch/nn/modules/module.py", line 550, in __call__
    result = self.forward(*input, **kwargs)
  File "/home/foo/ve_name/env_name/etc/model-r100-ii/pytorch.py", line 288, in forward
    self.minusscalar0_second = torch.autograd.Variable(torch.from_numpy(__weights_dict['minusscalar0_second']['value']), requires_grad=False)
NameError: name '_KitModel__weights_dict' is not defined

I am perplexed why this is happening. I can clearly see the __weights_dict being defined globally, so I am clueless why this error appears :(

Any directions to solve this problem would be incredibly useful (wasted 8 hours on this already!)

10
  • 3
    Note that double-underscore variables inside classes are name mangled to be "private", even when they are not class/instance variables. Using __weights_dict inside KitModel is not the global __weights_dict, but _KitModel__weights_dict instead. Commented Jul 9, 2020 at 8:26
  • If this is autogenerated code, the question is then though whether you need to fix the output or the generator. Why does the code generator create invalid code in the first place? Commented Jul 9, 2020 at 8:26
  • @MisterMiyagi: firstly, thank you for your suggestions. From what I understand, and from the autogenerated code, __weights_dict is initially defined outside the class and then it is declared as global within the class. So, _KitModel__weights_dict is now a global variable. But then why does it complain that it is not found? :( I am confused now! do you have any suggestions on what I can try? Commented Jul 9, 2020 at 8:29
  • @MisterMiyagi: the code is actually auto generated with mmdnn(github.com/microsoft/MMdnn). Perhaps it is a bug, but I am unsure.. Commented Jul 9, 2020 at 8:30
  • 1
    @MisterMiyagi: that solved this problem (but it looks like I have another problem now - but thats not related to this!). Thank you. Could you please post this as a solution and I will accept it? Thanks again. never knew about name mangling. Commented Jul 9, 2020 at 8:44

1 Answer 1

1

Python has name mangling inside class scopes, which means that names starting with two underscores are renamed. Inside the class scope, __weights_dict actually refers to _KitModel__weights_dict, i.e. not the name of the global variable.

As a fix, rename all occurrences of __weights_dict to _weights_dict.

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.