Some weights of {} were not initialized from the model checkpoint
See original GitHub issueI keep failing to load model checkpoint.
I built a model inheriting PreTrainedModel and have roberta inside initialization.
Training this model with trainer works fine, but when I try to load the checkpoint using from_pretrained, it keeps failing to load the checkpoint. Can someone help me out? Thanks
Structure of my model
class MaskClassifier(PreTrainedModel):
def __init__(self, config, path):
super().__init__(config=config)
self.roberta = RobertaModel.from_pretrained(path)
self.max_mask = 10
self.hidden_size = RobertaConfig().hidden_size
self.linear1 = torch.nn.Linear(2 * self.hidden_size, self.hidden_size)
self.linear2 = torch.nn.Linear(self.hidden_size, self.max_mask + 1)
self.softmax = torch.nn.Softmax(dim=1)
def forward(self, input_ids, attention_mask, token_type_ids, labels=None):
...
# Feed input to RoBERTa
Initialize before training
config = RobertaConfig()
config.max_position_embeddings = 514
config.type_vocab_size = 1
config.vocab_size = 50265
model = MaskClassifier(config=config, path='roberta-base')
Saving after training
trainer.save_model('./slogan_pretrained')
Loading the checkpoint
config = RobertaConfig()
config.max_position_embeddings = 514
config.type_vocab_size = 1
config.vocab_size = 50265
model = MaskClassifier.from_pretrained(path, config=config, path='roberta-base')
I found similair issue(https://github.com/huggingface/transformers/issues/2886), but I don’t know exactly how I should override the function from_pretrained and even I tried overriding this functions, it still can’t load the checkpoint.
Error Message
Some weights of MaskClassifier were not initialized from the model checkpoint at /home/yeoun/slogans/slogan_pretrained and are newly initialized: [‘.roberta.embeddings.position_ids’, ‘.roberta.embeddings.word_embeddings.weight’, ‘.roberta.embeddings.position_embeddings.weight’, ‘.roberta.embeddings.token_type_embeddings.weight’, ‘.roberta.embeddings.LayerNorm.weight’, ‘.roberta.embeddings.LayerNorm.bias’, ‘.roberta.encoder.layer.0.attention.self.query.weight’, ‘.roberta.encoder.layer.0.attention.self.query.bias’, ‘.roberta.encoder.layer.0.attention.self.key.weight’, ‘.roberta.encoder.layer.0.attention.self.key.bias’, ‘.roberta.encoder.layer.0.attention.self.value.weight’, …
Issue Analytics
- State:
- Created 3 years ago
- Comments:6 (6 by maintainers)
Top Related StackOverflow Question
Hi! Thanks for opening an issue. I see two issues with your setup here:
from_pretrainedto load theRobertaModelinside your pre-trained model? You should just initialize aRobertaModelfrom the configuration imo.PreTrainedModel, I would instead useRobertaPreTrainedModel.See the below script for an example of what I would recommend. I’m saving & reloading the model to make sure that all the weights get saved/loaded:
Let’s see the logs now, for the first load using the
roberta-basecheckpoint:The warning tells you: you’re not using the
lm_headweights, and the following layers are initialized:linear1andlinear2. Since you’re not using the LM head, and the two layers are the ones you just added, then there’s nothing to worry about.Let’s try saving the model and reloading it again:
The logs show:
Success 🎉
Glad I could help!