【发布时间】:2022-02-11 01:47:56
【问题描述】:
根据here pipeline 提供了一个接口,通过save_pretrained 方法在本地保存预训练的pipeline。当我使用它时,我看到一个文件夹创建了一堆 json 和 bin 文件,大概是为 tokenizer 和模型创建的。
但是文档没有指定加载方法。如何使用本地保存的管道初始化管道?
【问题讨论】:
标签: python huggingface-transformers
根据here pipeline 提供了一个接口,通过save_pretrained 方法在本地保存预训练的pipeline。当我使用它时,我看到一个文件夹创建了一堆 json 和 bin 文件,大概是为 tokenizer 和模型创建的。
但是文档没有指定加载方法。如何使用本地保存的管道初始化管道?
【问题讨论】:
标签: python huggingface-transformers
如果您阅读save_pretrained 的规范,它只是声明它
保存[s]管道的模型和标记器。
我还给出了一个稍微相关的答案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")
【讨论】:
model,它是否也会自动加载正确的分词器?
tokenizer.json,我假设它会自动加载。我想知道它是否会加载默认标记器