解决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)  

文章出处登录后可见!

已经登录?立即刷新

共计人评分,平均

到目前为止还没有投票!成为第一位评论此文章。

(0)
扎眼的阳光的头像扎眼的阳光普通用户
上一篇 2023年4月19日
下一篇 2023年4月19日

相关推荐