Hi, everyone~ I have defined my model via huggingface, but I don’t know how to save and load the model, hopefully someone can help me out, thanks!
def __init__(self, num_classes):
self.bert = BertModel.from_pretrained('hfl/chinese-roberta-wwm-ext', return_dict=True).to(device)
self.fc = nn.Linear(768, num_classes, bias=False)
def forward(self, x_input_ids, x_type_ids, attn_mask):
outputs = self.bert(x_input_ids, token_type_ids=x_type_ids, attention_mask=attn_mask)
pred = self.fc(outputs.pooler_output)
model = MyModel(num_classes).to(device)
If you make your model a subclass of
PreTrainedModel, then you can use our methods
from_pretrained. Otherwise it’s regular PyTorch code to save and load (using
What if the pre-trained model is saved by using
torch.save(model.state_dict()). How can I use that model like the
BertTokenizer for creating tokens and also embeddings?
Is there a difference between loading a model via torch.load and using from_pretrained in terms of downstream tasks?
Does either method have an advantage over the other for fine-tuning or inference?
If i do torch.save it will save only the model file but it won’t save the config.json file . How to achieve that using torch.save method