Hugging Face H5 加载模型错误:在配置文件中找不到模型
Posted
技术标签:
【中文标题】Hugging Face H5 加载模型错误:在配置文件中找不到模型【英文标题】:Hugging Face H5 load model error : No model found in config file 【发布时间】:2021-11-19 09:55:22 【问题描述】:我正在尝试从 Hugging Face 加载模型,并从这里下载了 h5 模型:https://huggingface.co/distilbert-base-uncased-finetuned-sst-2-english/tree/main
from flask import Flask, jsonify, request # import objects from the Flask model
from keras.models import load_model
from transformers import AutoTokenizer, AutoModelForSequenceClassification,TextClassificationPipeline
model = load_model('./tf_model.h5') # trying to load model here
然后出现错误:
File "C:\D\Learning\Flask\flask-pp-rest\main.py", line 11, in <module>
model = load_model('./tf_model.h5') File "C:\Users\ndrez\AppData\Local\Programs\Python\Python39\lib\site-packages\keras\saving\save.py",
line 200, in load_model
return hdf5_format.load_model_from_hdf5(filepath, custom_objects, File
"C:\Users\ndrez\AppData\Local\Programs\Python\Python39\lib\site-packages\keras\saving\hdf5_format.py",
line 176, in load_model_from_hdf5
raise ValueError('No model found in config file.') ValueError: **No model found in config file.**
有谁知道如何解决这个问题?如果你知道请帮帮我。我会监控这个问题并尝试实施您的解决方案的答案。
【问题讨论】:
你从哪里得到这个 h5 文件或者你是怎么做的? 【参考方案1】:您可以使用TFAutoModelForSequenceClassification 类加载distilbert-base-uncased-finetuned-sst-2-english
的张量流版本:
from transformers import AutoTokenizer, TFAutoModelForSequenceClassification
tokenizer = AutoTokenizer.from_pretrained("distilbert-base-uncased-finetuned-sst-2-english")
model = TFAutoModelForSequenceClassification.from_pretrained("distilbert-base-uncased-finetuned-sst-2-english")
【讨论】:
【参考方案2】:要加载您指定的模型,代码如下:
from transformers import AutoTokenizer, AutoModelForSequenceClassification
tokenizer = AutoTokenizer.from_pretrained("distilbert-base-uncased-finetuned-sst-2-english")
model = AutoModelForSequenceClassification.from_pretrained("distilbert-base-uncased-finetuned-sst-2-english")
【讨论】:
我试过这个,它需要pytorch,我只想使用tensorflow以上是关于Hugging Face H5 加载模型错误:在配置文件中找不到模型的主要内容,如果未能解决你的问题,请参考以下文章
查看 Hugging Face Sagemaker 模型的训练错误指标
Hugging Face 模型 Bio_ClinicalBERT 产生 404 错误