-
Notifications
You must be signed in to change notification settings - Fork 28.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Problem loading a finetuned model. #135
Comments
Ok I managed to find the problem. It comes from: When trying to load
First, I think that we should add a check of To fix this, we should probably fetch the number of labels of the saved model and use it to instanciate the model being created before loading the saved weights. Unfortunately I don't really know how to do that, any idea? Another possible "fix" would be to force the user to give a @classmethod
def from_pretrained(cls, *args, **kwargs):
if 'num_labels' not in kwargs:
raise ValueError('num_labels should be given when loading a pre-trained classification model')
return super().from_pretrained(*args, **kwargs) And even with this code, we are not able to check that the |
Just use the num_labels when you load your model model_state_dict = torch.load(model_fn)
loaded_model = BertForSequenceClassification.from_pretrained(bert_model, state_dict = model_state_dict, num_labels = 16)
print(loaded_model.num_labels)``` |
As mentioned in my previous posts, I think that the library should either fetch the number of labels from the save file or force the user to provide a While what you are proposing fixes my problem I would like to prevent this problem for other users in the future by patching the library code. |
I see thanks @rodgzilla. Indeed not using the Regarding fetching the number of labels, I understand your point but it will probably add too much custom logic in the library for the moment so let's go for your simple solution of setting the number of labels as mandatory for now (should have done that since the beginning). |
Hi everyone! |
I'm also facing a similar problem using the same model as @ugm2 - biobert-base-cased-v1.1-mnli In my example I know the exact
|
With the latest transformers versions, you can use the recently introduced (#12664) |
Issue:
I am frustrated by this issue I have a the problem is when i am saving my fine tune model i think it is somehow not getting saved correctly. My task is to save the fine tuned model and then use the updated model to fit it on a training dataset.
Full Code:
|
Hi!
There is a problem with the way model are saved and loaded. The following code should crash and doesn't:
This code prints:
The code should raise an exception when trying to load the weights of the task specific linear layer. I'm guessing that the problem comes from
PreTrainedBertModel.from_pretrained
.I would be happy to submit a PR fixing this problem but I'm not used to work with the PyTorch loading mechanisms. @thomwolf could you give me some guidance?
Cheers!
The text was updated successfully, but these errors were encountered: