Error while saving my network

I have made a CNN:

class convNet(nn.Module):
    #constructor
    def __init__(self):
        super(convNet, self).__init__()
       
        self.conv1 = nn.Conv2d(3, 96, kernel_size=7,stride=1,padding=3)
        self.conv2 = nn.Conv2d(96, 256, kernel_size=5,stride=1,padding=2)
        self.conv3 = nn.Conv2d(256,384,kernel_size=3,stride=1,padding=1)
        self.conv4 = nn.Conv2d(384,512,kernel_size=3,stride=1,padding=1)
        self.conv5 = nn.Conv2d(512,1024,kernel_size=3,stride=1,padding=1)
        self.fc1   = nn.Linear(4*4*1024, 300)
        self.fc2   = nn.Linear(300, 100)
        self.fc3   = nn.Linear(100, 10)
        

    def forward(self, x):
        conv1_relu = nnFunctions.relu(self.conv1(x))
        conv2_relu = nnFunctions.relu(self.conv2(conv1_relu))
        conv3_relu =nnFunctions.max_pool2d(nnFunctions.relu(self.conv3(conv2_relu)),2)
        conv4_relu =nnFunctions.max_pool2d(nnFunctions.relu(self.conv4(conv3_relu)),2)
        conv5_relu =nnFunctions.max_pool2d(nnFunctions.relu(self.conv5(conv4_relu)),2)
    
        x = conv5_relu.view(-1, 4*4*1024)
        x = nnFunctions.relu(self.fc1(x))
        x = nnFunctions.relu(self.fc2(x))
        x = self.fc3(x)
        return x
net=convNet()
net.cuda()

But when I write torch.save('net.txt',net)

It gives me the following errror:


AttributeError                            Traceback (most recent call last)
<ipython-input-13-a8f33f24049b> in <module>()
----> 1 torch.save('net.t7',net)

/home/sarthak/anaconda2/lib/python2.7/site-packages/torch/serialization.pyc in save(obj, f, pickle_module, pickle_protocol)
    121         f = open(f, "wb")
    122     try:
--> 123         return _save(obj, f, pickle_module, pickle_protocol)
    124     finally:
    125         if new_fd:

/home/sarthak/anaconda2/lib/python2.7/site-packages/torch/serialization.pyc in _save(obj, f, pickle_module, pickle_protocol)
    212         pickle_module.dump(sys_info, f, protocol=pickle_protocol)
    213 
--> 214     with closing(tarfile.open(fileobj=f, mode='w:', format=tarfile.PAX_FORMAT)) as tar:
    215         _add_to_tar(save_sys_info, tar, 'sys_info')
    216         _add_to_tar(pickle_objects, tar, 'pickle')

/home/sarthak/anaconda2/lib/python2.7/tarfile.pyc in open(cls, name, mode, fileobj, bufsize, **kwargs)
   1691             else:
   1692                 raise CompressionError("unknown compression type %r" % comptype)
-> 1693             return func(name, filemode, fileobj, **kwargs)
   1694 
   1695         elif "|" in mode:

/home/sarthak/anaconda2/lib/python2.7/tarfile.pyc in taropen(cls, name, mode, fileobj, **kwargs)
   1721         if mode not in ("r", "a", "w"):
   1722             raise ValueError("mode must be 'r', 'a' or 'w'")
-> 1723         return cls(name, mode, fileobj, **kwargs)
   1724 
   1725     @classmethod

/home/sarthak/anaconda2/lib/python2.7/tarfile.pyc in __init__(self, name, mode, fileobj, format, tarinfo, dereference, ignore_zeros, encoding, errors, pax_headers, debug, errorlevel)
   1577         self.members = []       # list of members as TarInfo objects
   1578         self._loaded = False    # flag if all members have been read
-> 1579         self.offset = self.fileobj.tell()
   1580                                 # current position in the archive file
   1581         self.inodes = {}        # dictionary caching the inodes of

/home/sarthak/anaconda2/lib/python2.7/site-packages/torch/nn/modules/module.pyc in __getattr__(self, name)
    241             if name in modules:
    242                 return modules[name]
--> 243         return object.__getattribute__(self, name)
    244 
    245     def __setattr__(self, name, value):

AttributeError: 'convNet' object has no attribute 'tell'

you have an obvious mistake.
It’s not:

torch.save('net.txt',net)

It is:

torch.save(net, 'net.txt')
3 Likes

Thanks for your help

While loading my network:
net=torch.load('net.txt')
I get the following error:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
in ()
----> 1 net=torch.load(‘net.txt’)

/home/sarthak/anaconda2/lib/python2.7/site-packages/torch/serialization.pyc in load(f, map_location, pickle_module)
    246         f = open(f, 'rb')
    247     try:
--> 248         return _load(f, map_location, pickle_module)
    249     finally:
    250         if new_fd:

/home/sarthak/anaconda2/lib/python2.7/site-packages/torch/serialization.pyc in _load(f, map_location, pickle_module)
    344         unpickler = pickle_module.Unpickler(pickle_file)
    345         unpickler.persistent_load = persistent_load
--> 346         result = unpickler.load()
    347         return result

AttributeError: 'module' object has no attribute 'convNet'

It’s hard to debug this without seeing the code, but my guess would be that pickle can’t find the definition of the convNet class. You’re probably trying to import it from a different directory, or you have moved the file.

I have saved my net on one jupyter-notebook and I am trying to load the saved network in another jupyter-notebook . Is there a way to load the net in a different notebook.

You need to copy the code of your network to that other notebook.

Or better put it in a file in the same directory as your notebooks and import it from both.