saving and loading pytorch neural nets
So I created a neural net and I would like to save it and load it whenever I want. Specifically, I want to take pictures and do real time processing. I am using the neural net created here

I read that the standard way is to create the net then use torch.save(net,'mynet') to save it and then load it with torch.load('mynet').

However if I open a new python3 terminal and use:

&gt;&gt;import torch
&gt;&gt;torch.load('mynet')


It gives me the error:

  File "&lt;stdin&gt;", line 1, in &lt;module&gt;
  File "/home/tim/anaconda3/lib/python3.7/site-packages/torch/serialization.py", line 367, in load
    return _load(f, map_location, pickle_module)
  File "/home/tim/anaconda3/lib/python3.7/site-packages/torch/serialization.py", line 538, in _load
    result = unpickler.load()
AttributeError: Can't get attribute 'Net' on &lt;module '__main__' (built-in)&gt;


I think this is from not having the Net class defined. Adding 

import torch.nn as nn
import torch.nn.functional as F
class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        self.conv1 = nn.Conv2d(3, 15, 3)
        self.pool = nn.MaxPool2d(2, 2)
        self.conv2 = nn.Conv2d(15, 15, 5)
        self.conv3 = nn.Conv2d(15, 10, 3)
        self.fc1 = nn.Linear(10*4*4, 100)
        self.fc2 = nn.Linear(100, 24)
        self.fc3 = nn.Linear(24, 4)

    def forward(self, x):
        x = self.pool(F.relu(self.conv1(x)))
        x = self.pool(F.relu(self.conv2(x)))
        x = self.pool(F.relu(self.conv3(x)))
        x = x.view(-1, 10*4*4)
        x = F.relu(self.fc1(x))
        x = F.relu(self.fc2(x))
        x = self.fc3(x)
        return x


is what you need to do , but why do we need to define the neural net class? What If I load a neural net with a different architecture to the one i specify in the class will the architecture defined in the class get overwritten? surely the object im loading have all the architecture and class information incapsulated in it?

Update:
Actually it doesn't even work when i define the Net class.

Please refer to the docs on serialization semantics, which first describes the suggested approach and then the one  you used as "serialized data is bound to the specific classes and the exact directory structure used, so it can break in various ways when used in other projects, or after some serious refactors."

In other words, you need to save/load net.state_dict(), not the net itself.
