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