question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Bert (sentence classification) output is non-deterministic(have checked previous issue, SET model.eval() )

See original GitHub issue

Environment info

  • transformers version:
  • Platform: Ubuntu 18.04
  • Python version: 3.7.6
  • PyTorch version (GPU?): 1.5.1
  • Tensorflow version (GPU?): /
  • Using GPU in script?: Yes for trainning, Both GPU and CPU for testing scripts
  • Using distributed or parallel set-up in script?: Yes for trainning

Who can help

@LysandreJik

Information

Model I am using (Bert, XLNet …): Bert

The problem arises when using:

  • the official example scripts: (give details below)
  • [ v] my own modified scripts: (give details below)

The tasks I am working on is:

  • an official GLUE/SQUaD task: (give the name)
  • [v ] my own task or dataset: (give details below)

I’m using chinese bert to match the similar tags and reduce the size of the database. So I use some mannually merged tags as the dataset, trainning a bert with inputting two tags and outputting the probability of they are similar. It did well after training in the calling of test() function I wrote(of course with model.eval()). But when I save the model to a .pth file and load it in another script, the output is non deterministic.

To reproduce

The whole test scripts is too long, but I have a short test snippet, It should cover the core of this issue.

from transformers import AutoTokenizer, AutoModelForSequenceClassification

tokenizer = AutoTokenizer.from_pretrained("hfl/chinese-bert-wwm-ext")
model = AutoModelForSequenceClassification.from_pretrained("hfl/chinese-bert-wwm-ext")
model.state_dict(torch.load('./weights/best_bert.pth', map_location='cpu'))
# model.cuda()
for i in range(100): # use to control the time to call model.eval()
    foo = 1

# model = model.eval()
model.eval()
with torch.no_grad():
    srcText = '春天' # 'spring'
    tgtText = '春季' # 'spring time'
    predict = model(
                **tokenizer(text=srcText, text_pair=tgtText,
                  truncation=True, return_tensors='pt', max_length=256)
            )
    # NON DETERMINISTIC
    print(torch.softmax(predict.logits, dim=1))

Steps to reproduce the behavior:

  1. run the script above
  2. change the iterative times for foo = 1, or just do nothing
  3. run again
  4. get different outputs logits and probabilities

Expected behavior

Get identical outputs in step 1 and 3

Additional information

I have read issue #4769 and some other similar issues, but I checked again and confirmed I called the function eval()

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
MartinoMensiocommented, Mar 31, 2022

I just managed finally to have deterministic results. If you are still struggling, see https://discuss.huggingface.co/t/initializing-the-weights-of-the-final-layer-of-e-g-bertfortokenclassification-with-a-manual-seed/1377/3

def set_seed(seed: Optional[int] = None):
    """Set all seeds to make results reproducible (deterministic mode).
       When seed is None, disables deterministic mode.
    :param seed: an integer to your choosing
    """
    if seed is not None:
        torch.manual_seed(seed)
        torch.cuda.manual_seed_all(seed)
        torch.backends.cudnn.deterministic = True
        torch.backends.cudnn.benchmark = False
        np.random.seed(seed)
        random.seed(seed)
        os.environ['PYTHONHASHSEED'] = str(seed)

0reactions
austindaycommented, Dec 9, 2022

You should get the following warning when you instantiate your AutoModelForSequenceClassification model:

Some weights of BertForSequenceClassification were not initialized from the model checkpoint at hfl/chinese-bert-wwm-ext and are newly initialized: ['classifier.bias', 'classifier.weight']

This tells you that the sequence classifier is not in the checkpoint you’re loading: it will be initialized randomly everytime you re-initialize it.

Thank you, I was struggling with trying to figure out why this was happening. I assumed that “random initialization” just meant it was randomly initialized once when the model was instantiated, not every time it’s called. Do you know why it has that behavior? Why wouldn’t it just be initialized randomly once? What tells it to stop being random? (A round of training? A flag?)

Read more comments on GitHub >

github_iconTop Results From Across the Web

neural network - BERT has a non deterministic behaviour
I am using the BERT implementation in https://github.com/google-research/bert for feature extracting and I have noticed a weird behaviour which ...
Read more >
BERT output not deterministic - deep learning - Stack Overflow
I expect the output values are deterministic when I put a same input, but my bert model the values are changing. Sounds awkwardly,...
Read more >
Trainer - Hugging Face
The Trainer class is optimized for Transformers models and can have surprising behaviors when you use it on other models. When using it...
Read more >
Chapter 4. Multilingual Named Entity Recognition - O'Reilly
Here we see that instead of the [CLS] and [SEP] tokens that BERT uses for sentence classification tasks, XLM-R uses <s> and <\s>...
Read more >
Explainable AI Methods - A Brief Overview | SpringerLink
Explainable Artificial Intelligence (xAI) is an established field with a vibrant community that has developed a variety of very successful ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found