python - 下载变压器模型以供离线使用

标签 python nlp pytorch huggingface-transformers

我有一个训练有素的变压器 NER 模型,我想在未连接到互联网的机器上使用它。加载此类模型时,当前它会将缓存文件下载到 .cache 文件夹。

要离线加载和运行模型,您需要将 .cache 文件夹中的文件复制到离线机器。但是,这些文件具有很长的非描述性名称,如果您要使用多个模型,则很难识别正确的文件。对此有何想法?

Example of model files

最佳答案

处理此问题的一种相对简单的方法是简单地“重命名”预训练模型,如 this 中所述。线。
从本质上讲,对于您尝试使用的任何模型,您所要做的就是这样:

from transformers import BertModel

model = BertModel.from_pretrained("bert-base-uncased")
model.save_pretrained("./my_named_bert")
该线程还详细介绍了本地模型文件夹的命名方式,请参阅 LysandreJik 的帖子:

Hi, they are named as such because that's a clean way to make sure the model on the S3 is the same as the model in the cache. The name is created from the etag of the file hosted on the S3. [...]

关于python - 下载变压器模型以供离线使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62261602/

相关文章:

python - 为什么集合的扩充分配表现不同?

python - 如何在图像上训练 scikit-neuralnetwork

algorithm - 从给定文本生成关键字的最佳方法是什么?

python - 计算两个文档之间的对称 Kullback-Leibler 散度

python - 在 Pytorch 自定义模块中添加模块

python - PyTorch:从多个数据集进行批处理

python - 如何从 col2 中获取与 col1 相同类别的 7 天内的最早日期?

python - 根据列添加缺失的行

java - 斯坦福核心 NLP - 语法关系缺失

pytorch - 如何在 PyTorch 中使用嵌入层作为线性层?