Can use different transformer model for tokenizer and model?

656 Views Asked by At

Can I use roberta for tokenizer while bert for model?

from transformers import RobertaTokenizerFast

tokenizer = RobertaTokenizerFast.from_pretrained("./bert_tokenizer", max_len=512)

from transformers import BertForMaskedLM
config = BertConfig()
bert= BertForMaskedLM(config)
1

There are 1 best solutions below

1
On

You cannot use RoBERTa tokenizer for BERT model. The reason is that the vocabulary for BERT and RoBERTa are different. Thus a word-piece token which is present in RoBERTa's vocabulary may not be present in BERT's vocabulary.