Got warning: Couldn't retrieve source code for container

Hello, I use the following code to define my network. But I got the warning:
"Couldn't retrieve source code for container of "type " + obj.__name__ + ". It won't be checked for correctness upon loading."

What’s the meaning of this warning and how to solve it? Thanks!

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):
        return nn.Sequential(*layers)
    def forward(self, x, y):
        out = self.layer1(x)
        out = self.layer2(out)
        out = self.layer3(out)
        return out+y
1 Like

This can happen for a variety of reasons, and you can ignore this warning.

I have the same problem. Could you tell me how to avoid this problem?

You can try to update to the latest version and see whether there is problem

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.


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 are around checking if the source code of the old definition and new definition are different.


Thank you for clarifying, @smth!

I’m with @geossy, preferring warnings to be resolved and to have none if possible.

Is there a workaround/hack to this situation if the class is defined in the notebook? (other than
moving the class outside of the notebook or supressing warnings)

Thank you!

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):

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 

You can suppress the warnings by capturing them using a filter:

There isn’t another mechanism I can think of.

1 Like

thank you, @smth - but I meant solving the problem, not hiding it. apologies if I weren’t clear.

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.

Here is a hack I came up with for notebooks:

# some code

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

x=_i # this copies the contents of the prev cell verbatim
file = "./"
with open(file, 'w') as f: f.write(x)
from feature_loss import FeatureLoss

and voila, you can have the cake (edit in the notebook) and eat it too (having the source pytorch can find).

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.

1 Like

we are opening to disabling the warning in notebook settings, if you think that’s useful.

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.

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).

Thank you, @smth

So just to be clear - this is a problem specifically with using a Jupyter notebook file as opposed to a plain old .py script?

Please, see Soumith’s answer above:

i.e. Jupyter env is just one of such possible situations.

for ppl looking for code here it is:

    import warnings