【问题标题】:How to inference .pb model converted from huggingface?如何推断从拥抱脸转换的.pb模型?
【发布时间】:2020-11-06 04:41:42
【问题描述】:

一般来说,我需要在浏览器中运行 DistilBERT。起初,我将 DistilBERT 从 huggingface 转换为 TensorFlow .pb 格式。但是,我不明白如何推断它。

转换代码:

from transformers import TFAutoModel, AutoTokenizer

model = TFAutoModel.from_pretrained('distilbert-base-uncased')
tokenizer = AutoTokenizer.from_pretrained('distilbert-base-uncased')
dir = "distilbert_savedmodel"
model._set_inputs(tf.TensorSpec([1, 384], tf.int32))
tf.saved_model.save(model, dir)

推理代码:

encoded = tokenizer.encode('Hello, world!', add_special_tokens=True, return_tensors="tf")
model = tf.keras.models.load_model(dir)
model(encoded)

错误:

ValueError: Could not find matching function to call loaded from the SavedModel. Got:
  Positional arguments (1 total):
    * Tensor("inputs:0", shape=(1, 1, 6), dtype=int32)
  Keyword arguments: {'training': False}

Expected these arguments to match one of the following 4 option(s):

Option 1:
  Positional arguments (1 total):
    * {'input_ids': TensorSpec(shape=(None, 5), dtype=tf.int32, name='input_ids')}
  Keyword arguments: {'training': False}

Option 2:
  Positional arguments (1 total):
    * {'input_ids': TensorSpec(shape=(None, 5), dtype=tf.int32, name='inputs/input_ids')}
  Keyword arguments: {'training': True}

Option 3:
  Positional arguments (1 total):
    * {'input_ids': TensorSpec(shape=(None, 5), dtype=tf.int32, name='inputs/input_ids')}
  Keyword arguments: {'training': False}

Option 4:
  Positional arguments (1 total):
    * {'input_ids': TensorSpec(shape=(None, 5), dtype=tf.int32, name='input_ids')}
  Keyword arguments: {'training': True}

笔记本链接:https://colab.research.google.com/drive/1otfNIYv8DRo2OZ0D2IpdoywrL0pN9k0I?usp=sharing

P。 S. 我是 TensorFlow 的新手。

【问题讨论】:

  • 选项 1 到 4 基本上是说模型需要一个位置参数input_ids。尝试使用model(encoded.ids) 看看它是否有效
  • encoded 已经是 tf 张量。而且它没有属性“ids”

标签: tensorflow tensorflow2.0 tensorflow.js huggingface-transformers


【解决方案1】:

看起来转换器与 saved_model 格式不兼容,并且一旦加载就会产生垃圾。

【讨论】:

    猜你喜欢
    • 2021-08-08
    • 2022-01-17
    • 2020-11-26
    • 1970-01-01
    • 2021-03-30
    • 2021-04-01
    • 2021-08-16
    • 2021-12-30
    • 1970-01-01
    相关资源
    最近更新 更多