解决烧心最快方法喝牛奶(解决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 as config 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=True model = BertModel.from_pretrained(./tf_model/my_tf_checkpoint.ckpt.index, from_pt=True, config=config)创心域SEO版权声明:以上内容作者已申请原创保护,未经允许不得转载,侵权必究!授权事宜、对本内容有异议或投诉,敬请联系网站管理员,我们将尽快回复您,谢谢合作!