I finetune a BERT model using hugging face transformer library and train it in GPU in the cloud. Then I save the model and tokenizer like below:
model.save_pretrained('/saved_model/')
torch.save(best_model.state_dict(), '/saved_model/model')
tokenizer.save_pretrained('/saved_model/')
I download the saved_model directory in my computer. Then I load the model/tokenizer like below in my computer
import torch
from transformers import *
tokenizer = BertTokenizer.from_pretrained('./saved_model/')
config = BertConfig('./saved_model/config.json')
model = BertModel(config)
model.load_state_dict(torch.load('./saved_model/pytorch_model.bin', map_location=torch.device('cpu')))
model.eval()
But it throws below error for the model.load_state_dict line
RuntimeError: Error(s) in loading state_dict for BertModel:
Missing key(s) in state_dict:
It lists a bunch of keys that are apparently missing from the state_dict.
I am new to pytorch and not sure what is going on. Most likely I am not saving the model the right way.
Please suggest.