如何从文件夹加载变压器管道?

Vic*_*r S 11 python huggingface-transformers

根据here pipeline提供了一个接口,用一种方法在本地保存预训练的管道save_pretrained。当我使用它时,我看到一个使用一堆 json 和 bin 文件创建的文件夹,大概是用于标记器和模型的。

但文档没有指定加载方法。如何pipeline使用本地保存的管道来初始化?

Vic*_*r S 15

显然,默认初始化也适用于本地文件夹。因此可以下载这样的模型:

pipe = pipeline("text-classification")
pipe.save_pretrained("my_local_path")
Run Code Online (Sandbox Code Playgroud)

然后加载它就像

pipe = pipeline("text-classification", model = "my_local_path")
Run Code Online (Sandbox Code Playgroud)


den*_*ger 9

如果您阅读 的规范save_pretrained,它只是说明它

\n
\n

保存 pipeline\xe2\x80\x99s模型和 tokenizer

\n
\n

我还在这里给出了关于如何加载自定义模型和分词器的稍微相关的答案。本质上,您可以简单地在以下位置指定特定的模型/路径pipeline

\n
from transformers import pipeline, AutoModel, AutoTokenizer\n\n# Replace with your custom model of choice\nmodel = AutoModel.from_pretrained(\'/path/to/your/model\')\ntokenizer = AutoTokenizer.from_pretrained(\'/path/to/your/tokenizer\')\n\npipe = pipeline(task=\'summarization\',  # replace with whatever task you have\n                model=model,\n                tokenizer=tokenizer)\n\n
Run Code Online (Sandbox Code Playgroud)\n