如何从文件夹加载变压器管道?
How to load transformers pipeline from folder?
根据 here pipeline 提供了一个接口,可以使用 save_pretrained
方法在本地保存预训练的 pipeline。当我使用它时,我看到一个文件夹创建了一堆 json 和 bin 文件,大概是为了标记器和模型。
但是文档没有指定加载方法。如何使用本地保存的管道初始化管道?
如果您阅读 save_pretrained
的规范,它只是说明它
Save[s] the pipeline’s model and tokenizer.
我还给出了一个稍微相关的答案 here 关于如何加载自定义模型和分词器。本质上,您可以简单地在 pipeline
:
中指定特定的 models/paths
from transformers import pipeline, AutoModel, AutoTokenizer
# Replace with your custom model of choice
model = AutoTokenizer.from_pretrained('/path/to/your/model')
tokenizer = AutoTokenizer.from_pretrained('/path/to/your/tokenizer')
pipe = pipeline(task='summarization', # replace with whatever task you have
model=model,
tokenizer=tokenizer)
显然默认初始化也适用于本地文件夹。所以可以下载这样的模型:
pipe = pipeline("text-classification")
pipe.save_pretrained("my_local_path")
然后像这样加载它
pipe = pipeline("text-classification", model = "my_local_path")
根据 here pipeline 提供了一个接口,可以使用 save_pretrained
方法在本地保存预训练的 pipeline。当我使用它时,我看到一个文件夹创建了一堆 json 和 bin 文件,大概是为了标记器和模型。
但是文档没有指定加载方法。如何使用本地保存的管道初始化管道?
如果您阅读 save_pretrained
的规范,它只是说明它
Save[s] the pipeline’s model and tokenizer.
我还给出了一个稍微相关的答案 here 关于如何加载自定义模型和分词器。本质上,您可以简单地在 pipeline
:
from transformers import pipeline, AutoModel, AutoTokenizer
# Replace with your custom model of choice
model = AutoTokenizer.from_pretrained('/path/to/your/model')
tokenizer = AutoTokenizer.from_pretrained('/path/to/your/tokenizer')
pipe = pipeline(task='summarization', # replace with whatever task you have
model=model,
tokenizer=tokenizer)
显然默认初始化也适用于本地文件夹。所以可以下载这样的模型:
pipe = pipeline("text-classification")
pipe.save_pretrained("my_local_path")
然后像这样加载它
pipe = pipeline("text-classification", model = "my_local_path")