0

目标:成功保存和加载 HuggingFace NLP 模型。

内核:conda_pytorch_p36。我执行了 Restart & Run All,并刷新了工作目录中的文件视图。

我正在关注这个代码教程,这是第一个 Python 代码模块。

更新:我需要一个transformers.onnx文件。我在哪里可以下载这个?


pip install transformers
pip install onnxruntime
pip install onnx

代码:

import onnxruntime as ort

from transformers import BertTokenizerFast

tokenizer = BertTokenizerFast.from_pretrained("bert-base-cased")

ort_session = ort.InferenceSession("onnx/bert-base-cased/model.onnx")

inputs = tokenizer("Using BERT in ONNX!", return_tensors="np")
outputs = ort_session.run(["last_hidden_state", "pooler_output"], dict(inputs))

追溯:

---------------------------------------------------------------------------
NoSuchFile                                Traceback (most recent call last)
<ipython-input-5-6c2daa1cf144> in <module>
      5 tokenizer = BertTokenizerFast.from_pretrained("bert-base-cased")
      6 
----> 7 ort_session = ort.InferenceSession("onnx/bert-base-cased/model.onnx")
      8 
      9 inputs = tokenizer("Using BERT in ONNX!", return_tensors="np")

~/anaconda3/envs/pytorch_p36/lib/python3.6/site-packages/onnxruntime/capi/onnxruntime_inference_collection.py in __init__(self, path_or_bytes, sess_options, providers, provider_options)
    278 
    279         try:
--> 280             self._create_inference_session(providers, provider_options)
    281         except RuntimeError:
    282             if self._enable_fallback:

~/anaconda3/envs/pytorch_p36/lib/python3.6/site-packages/onnxruntime/capi/onnxruntime_inference_collection.py in _create_inference_session(self, providers, provider_options)
    305         session_options = self._sess_options if self._sess_options else C.get_default_session_options()
    306         if self._model_path:
--> 307             sess = C.InferenceSession(session_options, self._model_path, True, self._read_config_from_model)
    308         else:
    309             sess = C.InferenceSession(session_options, self._model_bytes, False, self._read_config_from_model)

NoSuchFile: [ONNXRuntimeError] : 3 : NO_SUCHFILE : Load model from onnx/bert-base-cased/model.onnx failed:Load model onnx/bert-base-cased/model.onnx failed. File doesn't exist

如果还有什么我可以添加到帖子中,请告诉我。

4

0 回答 0