Transformers documentation

导出为 TFLite

You are viewing v4.40.1 version. A newer version v4.46.3 is available.
Hugging Face's logo
Join the Hugging Face community

and get access to the augmented documentation experience

to get started

导出为 TFLite

TensorFlow Lite 是一个轻量级框架,用于资源受限的设备上,如手机、嵌入式系统和物联网(IoT)设备,部署机器学习模型。TFLite 旨在在计算能力、内存和功耗有限的设备上优化和高效运行模型。模型以一种特殊的高效可移植格式表示,其文件扩展名为 .tflite

🤗 Optimum 通过 exporters.tflite 模块提供将 🤗 Transformers 模型导出至 TFLite 格式的功能。请参考 🤗 Optimum 文档 以获取支持的模型架构列表。

要将模型导出为 TFLite 格式,请安装所需的依赖项:

pip install optimum[exporters-tf]

请参阅 🤗 Optimum 文档 以查看所有可用参数,或者在命令行中查看帮助:

optimum-cli export tflite --help

运行以下命令,以从 🤗 Hub 导出模型的检查点(checkpoint),以 google-bert/bert-base-uncased 为例:

optimum-cli export tflite --model google-bert/bert-base-uncased --sequence_length 128 bert_tflite/

你应该能在日志中看到导出进度以及生成的 model.tflite 文件的保存位置,如下所示:

Validating TFLite model...
	-[✓] TFLite model output names match reference model (logits)
	- Validating TFLite Model output "logits":
		-[✓] (1, 128, 30522) matches (1, 128, 30522)
		-[x] values not close enough, max diff: 5.817413330078125e-05 (atol: 1e-05)
The TensorFlow Lite export succeeded with the warning: The maximum absolute difference between the output of the reference model and the TFLite exported model is not within the set tolerance 1e-05:
- logits: max diff = 5.817413330078125e-05.
 The exported model was saved at: bert_tflite

上面的示例说明了从 🤗 Hub 导出检查点的过程。导出本地模型时,首先需要确保将模型的权重和分词器文件保存在同一目录(local_path)中。在使用 CLI(命令行)时,将 local_path 传递给 model 参数,而不是 🤗 Hub 上的检查点名称。

< > Update on GitHub