Got warning: Couldn't retrieve source code for container

username: zhangboknight
cooked: <p>Hello, I use the following code to define my network. But I got the warning:<br>
<code>"Couldn't retrieve source code for container of "type " + obj.__name__ + ". It won't be checked for correctness upon loading."</code></p>
<p>What’s the meaning of this warning and how to solve it? Thanks!</p>
<pre><code>class BasicBlock(nn.Module):
    def __init__(self):
        super(BasicBlock, self).__init__()
        self.conv = torch.nn.Conv2d(in_channels=64, out_channels=64,
                      kernel_size=3, stride=1, padding=1)
        self.relu = torch.nn.ReLU()
        
    def forward(self, x):
        out = self.conv(x)
        out = self.relu(out)
        return out
        
class MNN(nn.Module):
    def __init__(self, block, blocks):
        super(MNN, self).__init__()
        self.layer1 = torch.nn.Conv2d(in_channels=2, out_channels=64,
                      kernel_size=3, stride=1, padding=1)
        self.layer2 = self.make_layer(block, blocks)
        self.layer3 = torch.nn.Conv2d(in_channels=64, out_channels=1,
                      kernel_size=3, stride=1, padding=1)
    
    def make_layer(self, block, blocks):
        layers = []
        for i in range(0, blocks):
            layers.append(block())
        return nn.Sequential(*layers)
        
    def forward(self, x, y):
        out = self.layer1(x)
        out = self.layer2(out)
        out = self.layer3(out)
        return out+y</code></pre>

username: smth
cooked: <p>This can happen for a variety of reasons, and you can ignore this warning.</p>

username: c594dfc72300bdb4a44c
cooked: <aside class="quote no-group quote-modified" data-username="zhangboknight" data-post="1" data-topic="7689">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/zhangboknight/48/15722_2.png" class="avatar"> zhangboknight:</div>
<blockquote>
<p>'t retrieve source code for container of "type " + obj.<strong>name</strong> + “. It won’t be checked for correctness upon loading.”</p>
</blockquote>
</aside>
<p>I have the same problem. Could you tell me how to avoid this problem?<br>
Thanks.</p>

username: zhangboknight
cooked: <p>You can try to update to the latest version and see whether there is problem</p>

username: geossy
cooked: <p>Well, I appreciate your work, but it could be much better either to get a more exact answer, or to remove the warning from the code if user may even totally ignore it.</p>

username: smth
cooked: <p>I probably typed that answer out from my phone, which is why it was concise.</p>
<p>Retrieving source code of a python class might happen for a variety of reasons, including:</p>
<ul>
<li>If you defined the class in the interpreter directly (like in ipython terminal / notebook, or python shell)</li>
<li>If you only have access to <code>.pyc</code> files but not the original <code>.py</code> files</li>
<li>If the class itself was auto-generated</li>
</ul>
<p>The warning exists to let the user know that we cannot do some sanity checks upon loading the class back, which are around checking if the source code of the old definition and new definition are different.</p>

username: stas
cooked: <p>Thank you for clarifying, <a class="mention" href="/u/smth">@smth</a>!</p>
<p>I’m with <a class="mention" href="/u/geossy">@geossy</a>, preferring warnings to be resolved and to have none if possible.</p>
<p>Is there a workaround/hack to this situation if the class is defined in the notebook? (other than<br>
moving the class outside of the notebook or supressing warnings)</p>
<p>Thank you!</p>
<p>p.s. also the warning has a weird paste of the chunk of its source after the warning is rendered in jupyter nb (last line below):</p>
<pre><code class="lang-auto">UserWarning: Couldn't retrieve source code for container of type MyClass. It won't be checked for correctness upon loading.
  "type " + obj.__name__ + ". It won't be checked 
</code></pre>

username: smth
cooked: <p>You can suppress the warnings by capturing them using a filter: <a href="https://docs.python.org/3/library/warnings.html#overriding-the-default-filter" rel="nofollow noopener">https://docs.python.org/3/library/warnings.html#overriding-the-default-filter</a></p>
<p>There isn’t another mechanism I can think of.</p>

username: stas
cooked: <p>thank you, <a class="mention" href="/u/smth">@smth</a> - but I meant solving the problem, not hiding it. apologies if I weren’t clear.</p>
<p>The idea is that we want to edit a module in the notebook and have pytorch find its source - which normally is not possible since nb is a json file.</p>
<p>Here is a hack I came up with for notebooks:</p>
<pre><code class="lang-auto">#cell0
# some code
[...]

#celln:
from torch import nn
class FeatureLoss(nn.Module): [... your code here ...]

#celln+1:
x=_i # this copies the contents of the prev cell verbatim
file = "./feature_loss.py"
with open(file, 'w') as f: f.write(x)
from feature_loss import FeatureLoss
</code></pre>
<p>and voila, you can have the cake (edit in the notebook) and eat it too (having the source pytorch can find).</p>
<p>Except you can’t rely on any of the local variables of the notebook unless you pass those as arguments to that auto-saved class.</p>

username: smth
cooked: <p>we are opening to disabling the warning in notebook settings, if you think that’s useful.</p>

username: stas
cooked: <p>I think warnings should be there regardless of the environment and they can be turned off by the user if need be, as you suggested originally, so all is good.</p>
<p>I was looking for a way to make pytorch find the source code that resides in the nb, which I succeeded in a hackish way (my previous comment).</p>
<p>Thank you, <a class="mention" href="/u/smth">@smth</a></p>

username: KausthubK
cooked: <p>So just to be clear - this is a problem specifically with using a Jupyter notebook file as opposed to a plain old .py script?</p>

username: stas
cooked: <p>Please, see Soumith’s answer above:</p><aside class="quote quote-modified" data-post="6" data-topic="7689">
  <div class="title">
    <div class="quote-controls"></div>
    <img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/smth/48/13_2.png" class="avatar">
    <a href="https://discuss.pytorch.org/t/got-warning-couldnt-retrieve-source-code-for-container/7689/6">Got warning: Couldn't retrieve source code for container</a> 
  </div>
  <blockquote>
    I probably typed that answer out from my phone, which is why it was concise. 
Retrieving source code of a python class might happen for a variety of reasons, including: 

If you defined the class in the interpreter directly (like in ipython terminal / notebook, or python shell)
If you only have access to .pyc files but not the original .py files
If the class itself was auto-generated

The warning exists to let the user know that we cannot do some sanity checks upon loading the class back, which …
  </blockquote>
</aside>
<p>
i.e. Jupyter env is just one of such possible situations.</p>

username: Brando_Miranda
cooked: <aside class="quote no-group" data-username="smth" data-post="8" data-topic="7689" data-full="true">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/smth/48/13_2.png" class="avatar"> smth:</div>
<blockquote>
<p>You can suppress the warnings by capturing them using a filter: <a href="https://docs.python.org/3/library/warnings.html#overriding-the-default-filter" rel="noopener nofollow ugc">https://docs.python.org/3/library/warnings.html#overriding-the-default-filter </a></p>
<p>There isn’t another mechanism I can think of.</p>
</blockquote>
</aside>
<p>for ppl looking for code here it is:</p>
<pre><code class="lang-auto">    import warnings
    warnings.simplefilter("ignore")
</code></pre>