Mit*_*ops 10 huggingface-transformers
从from_pretrained的文档中,我知道我不必每次都下载预训练的向量,我可以使用以下语法保存它们并从磁盘加载:
- a path to a `directory` containing vocabulary files required by the tokenizer, for instance saved using the :func:`~transformers.PreTrainedTokenizer.save_pretrained` method, e.g.: ``./my_model_directory/``.
- (not applicable to all derived classes, deprecated) a path or url to a single saved vocabulary file if and only if the tokenizer only requires a single vocabulary file (e.g. Bert, XLNet), e.g.: ``./my_model_directory/vocab.txt``.
Run Code Online (Sandbox Code Playgroud)
所以,我去了模型中心:
我找到了我想要的模型:
我从他们提供给这个存储库的链接下载了它:
使用掩码语言建模 (MLM) 目标的英语语言预训练模型。它是在本文中介绍的,并首次在此存储库中发布。此模型区分大小写:它区分英语和英语。
存储在:
/my/local/models/cased_L-12_H-768_A-12/
Run Code Online (Sandbox Code Playgroud)
其中包含:
./
../
bert_config.json
bert_model.ckpt.data-00000-of-00001
bert_model.ckpt.index
bert_model.ckpt.meta
vocab.txt
Run Code Online (Sandbox Code Playgroud)
所以,现在我有以下几点:
PATH = '/my/local/models/cased_L-12_H-768_A-12/'
tokenizer = BertTokenizer.from_pretrained(PATH, local_files_only=True)
Run Code Online (Sandbox Code Playgroud)
我得到这个错误:
> raise EnvironmentError(msg)
E OSError: Can't load config for '/my/local/models/cased_L-12_H-768_A-12/'. Make sure that:
E
E - '/my/local/models/cased_L-12_H-768_A-12/' is a correct model identifier listed on 'https://huggingface.co/models'
E
E - or '/my/local/models/cased_L-12_H-768_A-12/' is the correct path to a directory containing a config.json file
Run Code Online (Sandbox Code Playgroud)
同样,当我直接链接到 config.json 时:
PATH = '/my/local/models/cased_L-12_H-768_A-12/bert_config.json'
tokenizer = BertTokenizer.from_pretrained(PATH, local_files_only=True)
if state_dict is None and not from_tf:
try:
state_dict = torch.load(resolved_archive_file, map_location="cpu")
except Exception:
raise OSError(
> "Unable to load weights from pytorch checkpoint file. "
"If you tried to load a PyTorch model from a TF 2.0 checkpoint, please set from_tf=True. "
)
E OSError: Unable to load weights from pytorch checkpoint file. If you tried to load a PyTorch model from a TF 2.0 checkpoint, please set from_tf=True.
Run Code Online (Sandbox Code Playgroud)
我应该怎么做才能让 Huggingface 使用我的本地预训练模型?
YOURPATH = '/somewhere/on/disk/'
name = 'transfo-xl-wt103'
tokenizer = TransfoXLTokenizerFast(name)
model = TransfoXLModel.from_pretrained(name)
tokenizer.save_pretrained(YOURPATH)
model.save_pretrained(YOURPATH)
>>> Please note you will not be able to load the save vocabulary in Rust-based TransfoXLTokenizerFast as they don't share the same structure.
('/somewhere/on/disk/vocab.bin', '/somewhere/on/disk/special_tokens_map.json', '/somewhere/on/disk/added_tokens.json')
Run Code Online (Sandbox Code Playgroud)
所以一切都得救了,但是……
YOURPATH = '/somewhere/on/disk/'
TransfoXLTokenizerFast.from_pretrained('transfo-xl-wt103', cache_dir=YOURPATH, local_files_only=True)
"Cannot find the requested files in the cached path and outgoing traffic has been"
ValueError: Cannot find the requested files in the cached path and outgoing traffic has been disabled. To enable model look-ups and downloads online, set 'local_files_only' to False.
Run Code Online (Sandbox Code Playgroud)
Sam*_*hid 27
该文件相对于您的模型文件夹位于哪里?我相信它必须是相对路径而不是绝对路径。因此,如果您编写代码的文件位于'my/local/',那么您的代码应该如下所示:
PATH = 'models/cased_L-12_H-768_A-12/'
tokenizer = BertTokenizer.from_pretrained(PATH, local_files_only=True)
Run Code Online (Sandbox Code Playgroud)
您只需指定所有文件所在的文件夹,而不是直接指定文件。我认为这绝对是一个问题PATH。尝试改变“斜杠”的样式:“/”与“\”,这些在不同的操作系统中是不同的。也尝试使用“.”,像这样./models/cased_L-12_H-768_A-12/等等。
我也有同样的需求,刚刚在我的 Linux 机器上使用 Tensorflow 进行了工作,所以我想分享一下。
我的requirements.txt代码环境文件:
tensorflow==2.2.0
Keras==2.4.3
scikit-learn==0.23.1
scipy==1.4.1
numpy==1.18.1
opencv-python==4.5.1.48
seaborn==0.11.1
tensorflow-hub==0.12.0
nltk==3.6.2
tqdm==4.60.0
transformers==4.6.0
ipywidgets==7.6.3
Run Code Online (Sandbox Code Playgroud)
我正在使用Python 3.6。
我访问了这个网站,它显示了我想要的特定 Huggingface 模型的目录树。我碰巧想要无壳模型,但这些步骤对于您的有壳版本应该类似。另请注意,我的链接是该模型的一个非常具体的提交,只是为了可重复性 - 当有人阅读本文时,很可能会有更新的版本。
我手动下载(或者必须复制/粘贴到记事本++中,因为下载按钮在某些情况下将我带到txt / json的原始版本......奇怪......)以下文件:
config.jsontf_model.h5tokenizer_config.jsontokenizer.jsonvocab.txt注意:再一次,我使用的是 Tensorflow,所以我没有下载 Pytorch 权重。如果您使用 Pytorch,您可能想要下载这些权重而不是文件tf_model.h5。
然后我将这些文件放在我的 Linux 机器上的这个目录中:
/opt/word_embeddings/bert-base-uncased/
Run Code Online (Sandbox Code Playgroud)
确保至少对所有这些文件具有快速读取权限可能是个好主意ls -la(我对每个文件的权限是-rw-r--r--)。我还对父目录(上面列出的目录)具有执行权限,因此人们可以cd访问该目录。
从那里,我可以像这样加载模型:
分词器:
tensorflow==2.2.0
Keras==2.4.3
scikit-learn==0.23.1
scipy==1.4.1
numpy==1.18.1
opencv-python==4.5.1.48
seaborn==0.11.1
tensorflow-hub==0.12.0
nltk==3.6.2
tqdm==4.60.0
transformers==4.6.0
ipywidgets==7.6.3
Run Code Online (Sandbox Code Playgroud)
层/模型权重:
/opt/word_embeddings/bert-base-uncased/
Run Code Online (Sandbox Code Playgroud)
在 Windows 10 上使用相对路径这应该很容易。假设您预先训练的(基于 pytorch 的)变压器模型位于当前工作目录的“model”文件夹中,以下代码可以加载您的模型。
from transformers import AutoModel
model = AutoModel.from_pretrained('.\model',local_files_only=True)
请注意“.\model”中的“点”。缺少它将使代码不成功。
| 归档时间: |
|
| 查看次数: |
18414 次 |
| 最近记录: |