title: How to load a checkpoint file in a pytorch model?
body: <p>In my pytorch model, I'm initializing my model and optimizer like this. </p>
  
  <pre><code>model = MyModelClass(config, shape, x_tr_mean, x_tr,std)
  optimizer = optim.SGD(model.parameters(), lr=config.learning_rate)
  </code></pre>
  
  <p>And here is the path to my checkpoint file. </p>
  
  <p>checkpoint_file = os.path.join(config.save_dir, "checkpoint.pth")</p>
  
  <p>To load this checkpoint file, I check and see if the checkpoint file exists and then I load it as well as the model and optimizer. </p>
  
  <pre><code>if os.path.exists(checkpoint_file):
      if config.resume:
          torch.load(checkpoint_file)
          model.load_state_dict(torch.load(checkpoint_file))
          optimizer.load_state_dict(torch.load(checkpoint_file))
  </code></pre>
  
  <p>Also, here's how I'm saving my model and optimizer. </p>
  
  <pre><code> torch.save({'model': model.state_dict(), 'optimizer': optimizer.state_dict(), 'iter_idx': iter_idx, 'best_va_acc': best_va_acc}, checkpoint_file)
  </code></pre>
  
  <p>For some reason I keep getting a strange error whenever I run this code. </p>
  
  <pre><code>model.load_state_dict(torch.load(checkpoint_file))
  File "/home/Josh/.local/lib/python3.6/site-packages/torch/nn/modules/module.py", line 769, in load_state_dict
  self.__class__.__name__, "\n\t".join(error_msgs)))
  RuntimeError: Error(s) in loading state_dict for MyModelClass:
          Missing key(s) in state_dict: "mean", "std", "attribute.weight", "attribute.bias".
          Unexpected key(s) in state_dict: "model", "optimizer", "iter_idx", "best_va_acc"
  </code></pre>
  
  <p>Does anyone know why I'm getting this error? </p>