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.

UnboundLocalError: local variable 'tokenizer' referenced before assignment

See original GitHub issue

I am runnnig the example code on the homepage. However,I met this problem.

import torch
from transformers import *

MODELS = [(BertModel,       BertTokenizer,       'bert-base-uncased'),
          (OpenAIGPTModel,  OpenAIGPTTokenizer,  'openai-gpt'),
          (GPT2Model,       GPT2Tokenizer,       'gpt2'),
          (CTRLModel,       CTRLTokenizer,       'ctrl'),
          (TransfoXLModel,  TransfoXLTokenizer,  'transfo-xl-wt103'),
          (XLNetModel,      XLNetTokenizer,      'xlnet-base-cased'),
          (XLMModel,        XLMTokenizer,        'xlm-mlm-enfr-1024'),
          (DistilBertModel, DistilBertTokenizer, 'distilbert-base-cased'),
          (RobertaModel,    RobertaTokenizer,    'roberta-base'),
          (XLMRobertaModel, XLMRobertaTokenizer, 'xlm-roberta-base'),
         ]
for model_class, tokenizer_class, pretrained_weights in MODELS:
    tokenizer = tokenizer_class.from_pretrained(pretrained_weights)
    model = model_class.from_pretrained(pretrained_weights)
    input_ids = torch.tensor([tokenizer.encode("Here is some text to encode", add_special_tokens=True)])  
    with torch.no_grad():
        last_hidden_states = model(input_ids)[0]
`UnboundLocalError: local variable 'tokenizer' referenced before assignmen

This happened when the model_class goes to the XLMModel.I do not quite understand why this happen,because this problem only occurs when the model is XLMModel.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:8 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
nbroad1881commented, Mar 19, 2020

I can also attest to this error.

I am using a Kaggle notebook, and I get this error after running this in my first cell. Most of it is default code, bottom two lines are the key ones.

# This Python 3 environment comes with many helpful analytics libraries installed
# It is defined by the kaggle/python docker image: https://github.com/kaggle/docker-python
# For example, here's several helpful packages to load in 

import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

# Input data files are available in the "../input/" directory.
# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory

import os
for dirname, _, filenames in os.walk('/kaggle/input'):
    for filename in filenames:
        print(os.path.join(dirname, filename))

# Any results you write to the current directory are saved as output.
print(os.getcwd(), os.listdir())

from transformers import RobertaTokenizer
tknzr = RobertaTokenizer.from_pretrained('roberta-large')

Error thrown

UnboundLocalError                         Traceback (most recent call last)
<ipython-input-1-7957db35f110> in <module>
     19 from transformers import RobertaTokenizer
     20 
---> 21 tknzr = RobertaTokenizer.from_pretrained('roberta-large')

/opt/conda/lib/python3.6/site-packages/transformers/tokenization_utils.py in from_pretrained(cls, *inputs, **kwargs)
    300 
    301         """
--> 302         return cls._from_pretrained(*inputs, **kwargs)
    303 
    304 

/opt/conda/lib/python3.6/site-packages/transformers/tokenization_utils.py in _from_pretrained(cls, pretrained_model_name_or_path, *init_inputs, **kwargs)
    442 
    443         # Save inputs and kwargs for saving and re-loading with ``save_pretrained``
--> 444         tokenizer.init_inputs = init_inputs
    445         tokenizer.init_kwargs = init_kwargs
    446 

UnboundLocalError: local variable 'tokenizer' referenced before assignment

Kaggle runs transformers version 2.3.0 by default. After updating to 2.5.1 it worked just fine. To update on Kaggle, turn the internet option on in the settings in the right side. Then do !pip install -U transformers

0reactions
stale[bot]commented, May 18, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

Read more comments on GitHub >

github_iconTop Results From Across the Web

local variable referenced before assignment in python closure
It's like having a hidden local filename in the first line of your function (note: local is not an actual Python keyword, but...
Read more >
Keras-io/Lobby - Gitter
i get this error: UnboundLocalError: local variable 'batch_outputs' referenced before assignment. this error is INSIDE the training.py script from keras.
Read more >
How to Fix Local Variable Referenced Before Assignment ...
UnboundLocalError : local variable 'value' referenced before assignment. The issue is that in this line: value = value + 1.
Read more >
local variable 'server' referenced before assignment
When I use the variable server on my print statement I get this error. File "/Users/c0deninja/projects/gsecurity/modules/fetch_requests.py", line 40, in ...
Read more >
Latest nlp topics - PyTorch Forums
Topic Replies Views Activity LSTM hidden_states issue 2 199 July 27, 2020 Same output for entire batch 0 568 July 26, 2020 Tricks to improve model...
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