Unexpected keyword argument 'return_dict' in BertForSequenceClassification

My transformers version is 3.4.0. When my code is here:

from transformers import BertTokenizer, BertForSequenceClassification
import torch
bert_config = BertConfig.from_json_file('torch_bert_chinese/config.json')
model = BertForSequenceClassification.from_pretrained('torch_bert_chinese/', from_tf=False, config=bert_config, local_files_only=True, return_dict=True)

The exception is here:

TypeError                                 Traceback (most recent call last)
<ipython-input-33-08c5f3693153> in <module>
      5 bert_config = BertConfig.from_json_file('torch_bert_chinese/config.json')
      6 model = BertForSequenceClassification.from_pretrained('torch_bert_chinese/', from_tf=False, config=bert_config, local_files_only=True,
----> 7                                                      return_dict=True)
      8 

/usr/local/lib/python3.6/dist-packages/transformers/modeling_utils.py in from_pretrained(cls, pretrained_model_name_or_path, *model_args, **kwargs)
    945 
    946         # Instantiate model.
--> 947         model = cls(config, *model_args, **model_kwargs)
    948 
    949         if state_dict is None and not from_tf:

TypeError: __init__() got an unexpected keyword argument 'return_dict'

If you are instantiating your model with a config, you need to pass this return_dict=True to the config create (so the line above in your case).

1 Like