Pytorch checkpoint giving poor results when loading after few days again

anushka_Singh: <p>my pytorch checkpoints are giving bad results when i am saving and loading them again like this…</p>
<p>torch.save(model, ‘models/model_0.pth’)<br>
model = torch.load(‘models/model_0.pth’)</p>
<p>import torch.nn.functional as F</p>
<h1><a name="creating-the-customized-model-by-adding-a-drop-out-and-a-dense-layer-on-top-of-distil-bert-to-get-the-final-output-for-the-model-1" class="anchor" href="#creating-the-customized-model-by-adding-a-drop-out-and-a-dense-layer-on-top-of-distil-bert-to-get-the-final-output-for-the-model-1"></a>Creating the customized model, by adding a drop out and a dense layer on top of distil bert to get the final output for the model.</h1>
<p>class DistilBERTClass(torch.nn.Module):<br>
def <strong>init</strong>(self):<br>
super(DistilBERTClass, self).<strong>init</strong>()<br>
self.l1 = DistilBertModel.from_pretrained(“distilbert-base-uncased”)</p>
<pre><code>    # unFreeze all layers except the last layer
    for name, param in self.l1.named_parameters():
                 param.requires_grad = True
         

    self.pre_classifier = torch.nn.Linear(768, 768)
    self.dropout = torch.nn.Dropout(0.1)
    self.classifier = torch.nn.Linear(768, 26)

def forward(self, input_ids, attention_mask, token_type_ids):
    output_1 = self.l1(input_ids=input_ids, attention_mask=attention_mask)
    hidden_state = output_1[0]
   # pooler = hidden_state[:, 0]
    pooler = torch.mean(hidden_state, dim=1)
    pooler = self.pre_classifier(pooler)  
    pooler = torch.nn.Tanh()(pooler)
    pooler = self.dropout(pooler)
    output = self.classifier(pooler) 
    output = F.sigmoid(output)
    return output
</code></pre>
<p>model = DistilBERTClass()<br>
model.to(device)</p>
<p>def save_checkpoint(model, optimizer, epoch, iteration, loss, filepath=‘checkpoint.pth’):<br>
checkpoint = {<br>
‘epoch’: epoch,<br>
‘iteration’: iteration,<br>
‘model_state_dict’: model.state_dict(),<br>
‘optimizer_state_dict’: optimizer.state_dict(),<br>
‘loss’: loss<br>
}<br>
torch.save(checkpoint, filepath)</p>
<p>def train(epoch):<br>
model.train()<br>
for _, data in tqdm(enumerate(training_loader, 0)):<br>
ids = data[‘ids’].to(device, dtype=torch.long)<br>
mask = data[‘mask’].to(device, dtype=torch.long)<br>
token_type_ids = data[‘token_type_ids’].to(device, dtype=torch.long)<br>
targets = data[‘targets’].to(device, dtype=torch.float)</p>
<pre><code>    outputs = model(ids, mask, token_type_ids)

    optimizer.zero_grad()
    loss = loss_fn(outputs, targets)
    if _ % 100 == 0:
        print(f'Epoch: {epoch}, Loss:  {loss.item()}')

    loss.backward()
    optimizer.step()

    # Save checkpoint after every n iterations (e.g., every 100 iterations)
    if _ % 230 == 0:
        save_checkpoint(model, optimizer, epoch, _, loss.item())
</code></pre>
<p>for epoch in range(EPOCHS):<br>
train(epoch)</p>
<p>def validation(testing_loader):<br>
model.eval()<br>
fin_targets=<span class="chcklst-box fa fa-square-o fa-fw"></span><br>
fin_outputs=<span class="chcklst-box fa fa-square-o fa-fw"></span><br>
with torch.no_grad():<br>
for _, data in tqdm(enumerate(testing_loader, 0)):<br>
ids = data[‘ids’].to(device, dtype = torch.long)<br>
mask = data[‘mask’].to(device, dtype = torch.long)<br>
token_type_ids = data[‘token_type_ids’].to(device, dtype = torch.long)</p>
<p>outputs, targets = validation(testing_loader)<br>
final_outputs = np.array(outputs) &gt;=0.5</p>
<p>val_hamming_loss = metrics.hamming_loss(targets, final_outputs)<br>
##<span class="hashtag-raw">#givinig</span> poor results</p>
<p>val_flat_accuracy_score = flat_accuracy(np.array(targets), np.array(final_outputs))<br>
##<span class="hashtag-raw">#givinig</span> poor results suddnely</p>
<pre><code>        targets = data['targets'].to(device, dtype = torch.float)
        outputs = model(ids, mask, token_type_ids)
        fin_targets.extend(targets.cpu().detach().numpy().tolist())
        fin_outputs.extend(outputs.cpu().detach().numpy().tolist()) 
return fin_outputs, fin_targets
</code></pre>
<p>#<span class="hashtag-raw">#please</span> suggest me the right way to load the model</p>