ptrblck:

Are you storing and loading the model or optimizer in some way.


Yes, I am storing the model, the optimizer, and all the other parameters as well in an ad hoc class.
class NNet:

     def __init__(self, name):
         self.name = name
         . . . ## other conf variables

         self.model = None
         self.criterion = None
         self.optimizer = None
         self.scheduler = None
         
         # data # i use these lists to store the training/validation avg. loss and accuracy
         self.train = []
         self.valid = []
         self.test = []

    # define training step with class method
    def train_step(self, train_loader, epoch):
        
        self.model.train()
        if self.scheduler is not None:
            self.scheduler.step()
            
        for batch_idx, (data, label) in enumerate(train_loader):
            inputV = Variable(data)
            target = Variable(label)
            
            output = self.model( inputV )
            loss = self.criterion(output, target)
            
            self.optimizer.zero_grad()
            loss.backward()
            self.optimizer.step()
          
    # define validation/test step with class method
    def test_step(self, test_loader):
        self.model.eval()
        test_loss = 0
        correct = 0
        with torch.no_grad():
            for data, label in test_loader:
                inputV = Variable(data)
                target = Variable(label)
            
                output = self.model( inputV )
                test_loss += self.criterion(output, target, size_average=False).item() # sum up batch loss
                pred = output.max(1, keepdim=True)[1] # get the index of the max log-probability
                correct += pred.eq(target.view_as(pred)).sum().item()

        test_loss /= len(test_loader.dataset)         
        correct /= len(test_loader.dataset)

The class is initialized with some empty variables (model, criterion, etc), but then I create the model and save it in the class:
 def generate_entry(name):

    entry = NNet(name)
    entry.model = _user_defined_model_(**entry.model_args)
    entry.criterion = torch.nn.functional.cross_entropy
    ...

edit: found how to properly embed code