解决OSError: Error no file named pytorch_model.bin, tf_model.h5 found in directory
问题:
OSError: Error no file named pytorch_model.bin, tf_model.h5, model.ckpt.index or flax_model.msgpack found in directory
出现过程:
使用transformers的Bertmodel时
问题代码:
model = Bertmodel.from_pretrained('bert_model')
问题原因:
下载的模型文件夹中没有pytorch_model.bin/tf_model.h5等文件,去原网址中查找也未发现相关文件。继而查看transformers官方使用说明:
from_pretrained
(pretrained_model_name_or_path, *model_args, **kwargs)
-
pretrained_model_name_or_path – either: - a string with the shortcut name of a pre-trained model to load from cache or download, e.g.:
bert-base-uncased
. - a string with the identifier name of a pre-trained model that was user-uploaded to our S3, e.g.:dbmdz/bert-base-german-cased
. - a path to a directory containing model weights saved using save_pretrained(), e.g.:./my_model_directory/
. - a path or url to a PyTorch state_dict save file (e.g. ./pt_model/pytorch_model.bin). In this case,from_pt
should be set to True and a configuration object should be provided asconfig
argument. This loading path is slower than converting the PyTorch checkpoint in a TensorFlow model using the provided conversion scripts and loading the TensorFlow model afterwards.
即:如果是加载用save_pretrained保存过的模型,需要加入参数from_pt/from_tf和相应的config
解决方法:
# 模型文件夹中的config文件路径config = BertConfig.from_json_file('./tf_model/my_tf_model_config.json') # 如果下载的是tensorflow模型,则from_tf=True;如果是pytorch模型,则参数设置改为from_pt=Truemodel = BertModel.from_pretrained('./tf_model/my_tf_checkpoint.ckpt.index', from_pt=True, config=config)
来源地址:https://blog.csdn.net/weixin_44231433/article/details/128297602
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341