基于脸部权重建立TensorFlow模型的问题

Problem building tensorflow model from huggingface weights(基于脸部权重建立TensorFlow模型的问题)

本文介绍了基于脸部权重建立TensorFlow模型的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用来自HuggingFace和TensorFlow的预先训练好的BERT模型('dbmdz/bert-base-italian-xxl-cased')(位于this链接)。

在网站上看到这篇文章后

目前只有与PyTorch-Transformers兼容的权重可用。如果您需要访问TensorFlow检查点,请提出问题!

我提出了这个问题,很快就给了我一个指向包含以下文件的档案的下载链接。这些文件如下:

$ ls bert-base-italian-xxl-cased/
config.json                    model.ckpt.index               vocab.txt
model.ckpt.data-00000-of-00001 model.ckpt.meta

我现在正在尝试加载模型并使用它,但所有尝试都失败了。

我尝试遵循来自HuggingFace讨论站点的this建议:

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

此后,我尝试了几种组合以加载模型,但始终不成功。

例如:

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)

总是导致此错误:

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.

所以我的问题是:如何从这些文件加载此预训练的BERT模型并在TensorFlow中使用它?

推荐答案

您可以尝试使用以下代码段在tensorflow中加载dbmdz/bert-base-italian-xxl-cased

from transformers import AutoTokenizer, TFBertModel
model_name = "dbmdz/bert-base-italian-cased"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = TFBertModel.from_pretrained(model_name)

如果要从给定的tensorflow checkpoint加载,可以尝试如下操作:

model = TFBertModel.from_pretrained("../../models/pretrained/bert-base-italian-xxl-cased/model.ckpt.index", config=config, from_tf=True)

这篇关于基于脸部权重建立TensorFlow模型的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:基于脸部权重建立TensorFlow模型的问题

基础教程推荐