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.

Loading fine tuned BertForMaskedLM

See original GitHub issue

Hi,

I tried to fine tune BertForMaskedLM and it works. But i’m facing issues when I try to load the fine tuned model. Here is the code I used to load the model :

model_state_dict = torch.load("./finetunedmodel/pytorch_model.bin",  map_location='cpu')
model_fine = BertForMaskedLM.from_pretrained(pretrained_model_name='bert-base-multilingual-cased', state_dict=model_state_dict, cache_dir='./data')

The error I’m facing is : init() got an unexpected keyword argument ‘state_dict’

Does someone already faced this issue ?

Thanks

Edit : I trained my model on gpu and try to use it on cpu. When I use it on gpu it works !

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
mraj96commented, Jan 22, 2019

yeah yes. if you trained model in GPU, can’t be loaded. we will change map_location=“CPU” in modeling.py line 511. https://github.com/huggingface/pytorch-pretrained-BERT/blob/0a9d7c7edb20a3e82cfbb4b72515575543784823/pytorch_pretrained_bert/modeling.py#L511

state_dict = torch.load(weights_path, map_location="CPU" )

now it will load our finetuned model. this is only for inference running on CPU.

0reactions
psureshmagadi17commented, Sep 21, 2020

@tgriseau - I want to fine-tune bert on MaskedLM using domain-specific text. could you please provide an example of how you fine-tuned or provide some details about what kind of inputs need to be passed? will I be using the true sentence as the output for fine-tuning?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Fine-tuning a masked language model - Hugging Face Course
This process of fine-tuning a pretrained language model on in-domain data is usually called domain adaptation. It was popularized in 2018 by ULMFiT,...
Read more >
Loading fine tuned BertForMaskedLM · Issue #215 - GitHub
Hi, I tried to fine tune BertForMaskedLM and it works. But i'm facing issues when I try to load the fine tuned model....
Read more >
BERT Fine-Tuning Tutorial with PyTorch - Chris McCormick
In order for torch to use the GPU, we need to identify and specify the GPU as the device. Later, in our training...
Read more >
pytorch-pretrained-bert - PyPI
This section explain how you can save and re-load a fine-tuned model (BERT, GPT, GPT-2 and Transformer-XL). There are three types of files...
Read more >
How to use fine-tuned model in huggingface for actual ...
Remember that you also need to tokenize the input to your model, just like in the training phase. Merely feeding a sentence to...
Read more >

github_iconTop Related Medium Post

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