[英]Problem building tensorflow model from huggingface weights
I need to work with the pretrained BERT model ( 'dbmdz/bert-base-italian-xxl-cased'
) from Huggingface with Tensorflow (at this link).我需要使用来自 Huggingface 的预训练 BERT model (
'dbmdz/bert-base-italian-xxl-cased'
) 和 Tensorflow(在此链接)。
After reading this on the website,在网站上阅读此内容后,
Currently only PyTorch-Transformers compatible weights are available.
目前只有 PyTorch-Transformers 兼容的权重可用。 If you need access to TensorFlow checkpoints, please raise an issue!
如果您需要访问 TensorFlow 检查点,请提出问题!
I raised the issue and promptly a download link to an archive containing the following files was given to me.我提出了这个问题,并立即向我提供了指向包含以下文件的存档的下载链接。 The files are the following ones:
这些文件如下:
$ ls bert-base-italian-xxl-cased/
config.json model.ckpt.index vocab.txt
model.ckpt.data-00000-of-00001 model.ckpt.meta
I'm now trying to load the model and work with it but everything I tried failed.我现在正在尝试加载 model 并使用它,但我尝试的一切都失败了。
I tried following this suggestion from an Huggingface discussion site:我尝试遵循Huggingface讨论站点的建议:
bert_folder = str(Config.MODELS_CONFIG.BERT_CHECKPOINT_DIR) # folder in which I have the files extracted from the archive
from transformers import BertConfig, TFBertModel
config = BertConfig.from_pretrained(bert_folder) # this gets loaded correctly
After this point I tried several combinations in order to load the model but always unsuccessfully.在这一点之后,我尝试了几种组合来加载 model 但总是失败。
eg:例如:
model = TFBertModel.from_pretrained("../../models/pretrained/bert-base-italian-xxl-cased/model.ckpt.index", config=config)
model = TFBertModel.from_pretrained("../../models/pretrained/bert-base-italian-xxl-cased/model.ckpt.index", config=config, from_pt=True)
model = TFBertModel.from_pretrained("../../models/pretrained/bert-base-italian-xxl-cased/model.ckpt.index", config=config, from_pt=True)
model = TFBertModel.from_pretrained("../../models/pretrained/bert-base-italian-xxl-cased", config=config, local_files_only=True)
Always results in this error:总是导致此错误:
404 Client Error: Not Found for url: https://huggingface.co/models/pretrained/bert-base-italian-xxl-cased/model.ckpt.index/resolve/main/tf_model.h5
...
...
OSError: Can't load weights for '../../models/pretrained/bert-base-italian-xxl-cased/model.ckpt.index'. Make sure that:
- '../../models/pretrained/bert-base-italian-xxl-cased/model.ckpt.index' is a correct model identifier listed on 'https://huggingface.co/models'
- or '../../models/pretrained/bert-base-italian-xxl-cased/model.ckpt.index' is the correct path to a directory containing a file named one of tf_model.h5, pytorch_model.bin.
So my question is: How can I load this pre-trained BERT model from those files and use it in tensorflow?所以我的问题是:如何从这些文件中加载这个预训练的 BERT model 并在 tensorflow 中使用它?
You can try the following snippet to load dbmdz/bert-base-italian-xxl-cased
in tensorflow
.您可以尝试使用以下代码片段在 tensorflow 中加载
dbmdz/bert-base-italian-xxl-cased
tensorflow
。
from transformers import AutoTokenizer, TFBertModel
model_name = "dbmdz/bert-base-italian-cased"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = TFBertModel.from_pretrained(model_name)
If you want to load from the given tensorflow checkpoint
, you could try like this:如果你想从给定的
tensorflow checkpoint
加载,你可以这样尝试:
model = TFBertModel.from_pretrained("../../models/pretrained/bert-base-italian-xxl-cased/model.ckpt.index", config=config, from_tf=True)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.