# Same checkpoint produces different output

**URL:** </static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fsame-checkpoint-produces-different-output%2F74055%3E%5C
**Category:** 🤗Transformers\
**Created:** [February 20, 2024, 10:17pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fsame-checkpoint-produces-different-output%2F74055 "2024-02-20T22:17:15Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Prerit](https://avatars.discourse-cdn.com/v4/letter/p/f19dbf/32.png) [@Prerit](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2FPrerit)%5C
**Post date:** [February 20, 2024, 10:17pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fsame-checkpoint-produces-different-output%2F74055%2F1 "2024-02-20T22:17:15Z")

</div>

I am trying to load a model trained using the trainer class. Here is the script that I used to load the saved model. However, I get different outputs each time I reload the model. Here is the code

```auto

import torch
from transformers import BertForSequenceClassification, BertTokenizer

class ModelPredictor:
    def __init__ (self, model_path, model_name):
        self.model = BertForSequenceClassification.from_pretrained(model_path, return_dict=False)
        self.tokenizer = BertTokenizer.from_pretrained(model_name, 
                                                       cache_dir='/data/hfacecache')
        self.model.eval() # Enters evaluation mode
    def predict(self, name):
        test_name_prompt = 'Tell me this persons race: ' + name
        inputs = self.tokenizer(test_name_prompt, padding=True, truncation=True, max_length=512, return_tensors="pt")
        # with torch.no_grad():
        with torch.no_grad():
            outputs = self.model(**inputs)
            predictions = torch.nn.functional.softmax(outputs[0], dim=-1)

        predicted_class_index = torch.argmax(predictions[0]).item()
        predicted_class_label = self.model.config.id2label[predicted_class_index]
        return predictions, predicted_class_label

```

Evaluation

```auto
#%%
model_path = "/output/checkpoint-30"
model_name = "bert-base-uncased"
predictor = ModelPredictor(model_path, model_name)

name_testing = "Joe Doe"
predictions, predicted_class_label = predictor.predict(name_testing)
print(predicted_class_label)
print(predictions)

```

```auto
This is the output
Asian
tensor([[0.1894, 0.1887, 0.3228, 0.2991]])

```

```auto
#%%
model_path = "/output/checkpoint-30"
model_name = "bert-base-uncased"
predictor = ModelPredictor(model_path, model_name)
name_testing = "Joe Doe"
predictions, predicted_class_label = predictor.predict(name_testing)
print(predicted_class_label)
print(predictions)

```

```auto
This is the output
White
tensor([[0.2871, 0.2415, 0.2611, 0.2102]])

```

I am not sure why this is happening despite loading the pre-trained model and setting the model in evaluation mode.
