简体   繁体   English

如何从检查点文件加载经过微调的 pytorch huggingface bert model?

[英]How to load a fine tuned pytorch huggingface bert model from a checkpoint file?

I had fine tuned a bert model in pytorch and saved its checkpoints via torch.save(model.state_dict(), 'model.pt')我在 pytorch 中微调了一个 bert model 并通过torch.save(model.state_dict(), 'model.pt')保存了它的检查点

Now When I want to reload the model, I have to explain whole network again and reload the weights and then push to the device.现在当我想重新加载model时,我必须再次解释整个网络并重新加载权重然后推送到设备。

Can anyone tell me how can I save the bert model directly and load directly to use in production/deployment?谁能告诉我如何直接保存bert model 并直接加载以用于生产/部署?

Following is the training code and you can try running there in colab itself, After training completion.以下是训练代码,您可以在训练完成后尝试在 colab 中运行。 you will notice in file system we have a checkpoint file.您会注意到在文件系统中我们有一个检查点文件。 But I want to save the model itself.但我想保存 model 本身。

LINK TO COLAB NOTEBOOK FOR SAMPLE TRAINING 链接到用于示例培训的 COLAB 笔记本

Following is the current inferencing code I written.以下是我编写的当前推理代码。

import torch
import torch.nn as nn
from transformers import AutoModel, BertTokenizerFast
import numpy as np
import json

tokenizer = BertTokenizerFast.from_pretrained('bert-base-uncased')
device = torch.device("cpu")


class BERT_Arch(nn.Module):

    def __init__(self, bert):
        super(BERT_Arch, self).__init__()

        self.bert = bert

        # dropout layer
        self.dropout = nn.Dropout(0.1)

        # relu activation function
        self.relu = nn.ReLU()

        # dense layer 1
        self.fc1 = nn.Linear(768, 512)

        # dense layer 2 (Output layer)
        self.fc2 = nn.Linear(512, 2)

        # softmax activation function
        self.softmax = nn.LogSoftmax(dim=1)

    # define the forward pass
    def forward(self, sent_id, mask):
        # pass the inputs to the model
        _, cls_hs = self.bert(sent_id, attention_mask=mask, return_dict=False)

        x = self.fc1(cls_hs)

        x = self.relu(x)

        x = self.dropout(x)

        # output layer
        x = self.fc2(x)

        # apply softmax activation
        x = self.softmax(x)

        return x


bert = AutoModel.from_pretrained('bert-base-uncased')
model = BERT_Arch(bert)
path = './models/saved_weights_new_data.pt'
model.load_state_dict(torch.load(path, map_location=device))
model.to(device)

def inference(comment):
    tokens_test = tokenizer.batch_encode_plus(
        list([comment]),
        max_length=75,
        pad_to_max_length=True,
        truncation=True,
        return_token_type_ids=False
    )
    test_seq = torch.tensor(tokens_test['input_ids'])
    test_mask = torch.tensor(tokens_test['attention_mask'])
    predictions = model(test_seq.to(device), test_mask.to(device))
    predictions = predictions.detach().cpu().numpy()
    predictions = np.argmax(predictions, axis=1)
    return predictions

I simply want to save a model from this notebook in a way such that I can use it for inferencing anywhere.我只是想从这个笔记本中保存一个 model,以便我可以在任何地方使用它进行推理。

Just save your model using model.save_pretrained, here is an example:只需使用 model.save_pretrained 保存您的 model,这是一个示例:

model.save_pretrained("<path_to_dummy_folder>")

You can download the model from colab, save it on your gdrive or at any other location of your choice.您可以从 colab 下载 model,将其保存在您的 gdrive 或您选择的任何其他位置。 While doing inference, you can just give path to this model (you may have to upload it) and start with inference.在进行推理时,您只需给出此 model 的路径(您可能必须上传它)并从推理开始。

To load the model加载 model

model = AutoModel.from_pretrained("<path_to_saved_pretrained_model>")

#Note: Instead of AutoModel class, you may use the task specific class as well.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 BertPreTrainedModel.from_pretrained() 加载微调的 AllenNLP BERT-SRL model? - How do I load a fine-tuned AllenNLP BERT-SRL model using BertPreTrainedModel.from_pretrained()? 如何在pytorch模型中加载检查点文件? - How to load a checkpoint file in a pytorch model? 如何使用微调的 BERT model 进行句子编码? - How to use fine-tuned BERT model for sentence encoding? 如何在另一个数据集上微调我训练的 model (bert) - How Fine-tuned my trained model (bert) on another dataset 我们如何将字符串列表传递给经过微调的 bert model? - How can we pass a list of strings to a fine tuned bert model? 保存“微调”的 bert 模型 - Saving a 'fine-tuned' bert model 如何在重新加载后在拥抱脸中使用微调的 model 进行实际预测? - How to use fine-tuned model in huggingface for actual prediction after re-loading? 我可以使用来自 TF HUB 的非微调 BERT 模型来为它提供 TF 服务吗? - Can I use non-fine-tuned BERT model from TF HUB to serve it with TF serving? 如何使用 Huggingface BERT 模型来馈送二元分类器 CNN? - How to use a Huggingface BERT model from to feed a binary classifier CNN? 无法从 Pytorch-Lightning 中的检查点加载模型 - Unable to load model from checkpoint in Pytorch-Lightning
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM