【问题标题】:Cannot load BERT from local disk无法从本地磁盘加载 BERT
【发布时间】:2021-07-13 02:56:44
【问题描述】:

我正在尝试使用 Huggingface 转换器 api 加载本地下载的 M-BERT 模型,但它引发了异常。 我克隆了这个仓库:https://huggingface.co/bert-base-multilingual-cased

bert = TFBertModel.from_pretrained("input/bert-base-multilingual-cased")

目录结构为:

但我收到此错误:

Traceback (most recent call last):
  File "/usr/local/lib/python3.7/dist-packages/transformers/modeling_tf_utils.py", line 1277, in from_pretrained
    missing_keys, unexpected_keys = load_tf_weights(model, resolved_archive_file, load_weight_prefix)
  File "/usr/local/lib/python3.7/dist-packages/transformers/modeling_tf_utils.py", line 467, in load_tf_weights
    with h5py.File(resolved_archive_file, "r") as f:
  File "/usr/local/lib/python3.7/dist-packages/h5py/_hl/files.py", line 408, in __init__
    swmr=swmr)
  File "/usr/local/lib/python3.7/dist-packages/h5py/_hl/files.py", line 173, in make_fid
    fid = h5f.open(name, flags, fapl=fapl)
  File "h5py/_objects.pyx", line 54, in h5py._objects.with_phil.wrapper
  File "h5py/_objects.pyx", line 55, in h5py._objects.with_phil.wrapper
  File "h5py/h5f.pyx", line 88, in h5py.h5f.open
OSError: Unable to open file (file signature not found)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "train.py", line 81, in <module>
    __main__()
  File "train.py", line 59, in __main__
    model = create_model(num_classes)
  File "/content/drive/My Drive/msc-project/code/model.py", line 26, in create_model
    bert = TFBertModel.from_pretrained("input/bert-base-multilingual-cased")
  File "/usr/local/lib/python3.7/dist-packages/transformers/modeling_tf_utils.py", line 1280, in from_pretrained
    "Unable to load weights from h5 file. "
OSError: Unable to load weights from h5 file. If you tried to load a TF 2.0 model from a PyTorch checkpoint, please set from_pt=True. 

我哪里错了? 需要帮忙! 提前致谢。

【问题讨论】:

  • 试试local_files_only=True
  • 还要检查您运行脚本的位置是否与/input处于同一级别

标签: python tensorflow bert-language-model huggingface-transformers


【解决方案1】:

正如 cmets 中已经指出的那样 - 您的 from_pretrained 参数应该是托管在 huggingface.co 上的模型的 ID 或本地路径:

包含使用保存的模型权重的目录的路径 save_pretrained(),例如,./my_model_directory/。

documentation

查看您的堆栈跟踪,您的代码似乎在其中运行:

/content/drive/My Drive/msc-project/code/model.py 所以除非你的模型在: /content/drive/My Drive/msc-project/code/input/bert-base-multilingual-cased/ 加载不出来。

我还将路径设置为类似于文档示例,即:

bert = TFBertModel.from_pretrained("./input/bert-base-multilingual-cased/")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-10-23
    • 2022-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-28
    • 2020-11-27
    • 1970-01-01
    相关资源
    最近更新 更多