Как сохранить токенизатор из трансформеров Huggingface в ONNX?Python

Программы на Python
Ответить Пред. темаСлед. тема
Anonymous
 Как сохранить токенизатор из трансформеров Huggingface в ONNX?

Сообщение Anonymous »

Я загружаю токенизатор и модель Берта из преобразователей Huggingface и экспортирую модель Берта в ONNX:

Код: Выделить всё

from transformers import AutoTokenizer, AutoModelForTokenClassification
import torch

# Load the tokenizer
tokenizer = AutoTokenizer.from_pretrained("huawei-noah/TinyBERT_General_4L_312D")

# Load the model
model = AutoModelForTokenClassification.from_pretrained("huawei-noah/TinyBERT_General_4L_312D")

# Example usage
text = "Hugging Face is creating a tool that democratizes AI."
inputs = tokenizer(text, return_tensors="pt")

# We need to use the inputs to trace the model
input_names = ["input_ids", "attention_mask"]
output_names = ["output"]

# Export the model to ONNX
torch.onnx.export(
model,                                           # model being run
(inputs["input_ids"], inputs["attention_mask"]), # model input (or a tuple for multiple inputs)
"TinyBERT_General_4L_312D.onnx",                 # where to save the model
export_params=True,                              # store the trained parameter weights inside the model file
opset_version=11,                                # the ONNX version to export the model to
do_constant_folding=True,                        # whether to execute constant folding for optimization
input_names=input_names,                         # the model's input names
output_names=output_names,                       # the model's output names
dynamic_axes={                                   # variable length axes
"input_ids": {0: "batch_size"},
"attention_mask": {0: "batch_size"},
"output": {0: "batch_size"}
}
)

print("Model has been successfully exported to ONNX")
Требования:

Код: Выделить всё

pip install transformers torch onnx
Как сохранить токенизатор в ONNX?

Подробнее здесь: https://stackoverflow.com/questions/785 ... rs-to-onnx
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение

Вернуться в «Python»