title: PyTorch model saving error: "Can't pickle local object"
body: <p>When I try to save the PyTorch model with this piece of code:</p>
  <pre><code>checkpoint = {'model': Net(), 'state_dict': model.state_dict(),'optimizer' :optimizer.state_dict()}
  torch.save(checkpoint, 'Checkpoint.pth')
  </code></pre>
  <p>I get the following error:</p>
  <pre><code>    E:\PROGRAM FILES\Anaconda\envs\staj_projesi\lib\site-packages\torch\serialization.py:251: UserWarning: Couldn't retrieve source code for container of type Net. It won't be checked for correctness upon loading.
  ...
  
        &quot;type &quot; + obj.__name__ + &quot;. It won't be checked &quot;
      Can't pickle local object 'trainModel.&lt;locals&gt;.Net'
  </code></pre>
  <p>When I try to save the PyTorch model with this piece of code:</p>
  <pre><code>checkpoint = {'state_dict': model.state_dict(),'optimizer' :optimizer.state_dict()}
  torch.save(checkpoint, 'Checkpoint.pth')
  </code></pre>
  <p>I don't don't get any errors, but I want to save the ANN class. How can I solve this problem? Also, I could save the model with the first structure in the other projects before</p>