# 创建 TensorFlow Lite 解析器

Source: [https://docs.qualcomm.com/doc/80-70014-54Y/topic/create-a-tensorflow-lite-interpreter.html](https://docs.qualcomm.com/doc/80-70014-54Y/topic/create-a-tensorflow-lite-interpreter.html)

使用 TensorFlow C/C++ API，可编译一个解析器来运行模型。

解析器界面允许您执行以下操作：

- 在选定的 delegate 上配置模型执行方法。
- 分配执行前向传播所需的内存。

以下示例代码演示了如何创建解析器。可配置解析器实例以使用特定的 delegate 并执行前向传播。

    //Build the interpreter with the InterpreterBuilder.
    //Note: all Interpreters should be built with the InterpreterBuilder,
    // which allocates memory for the Interpreter and does various set up
    // tasks so that the Interpreter can read the provided model.
      
    tflite::ops::builtin::BuiltinOpResolver resolver;
    tflite::InterpreterBuilder builder(*model, resolver);
    std::unique_ptr<tflite::Interpreter> interpreter;
    builder(&interpreter);
    if (!interpreter) {
       std::cerr << “Failed to construct interpreter on provided tflite model” << std::endl;
    }
    if (interpreter->AllocateTensors() != kTfLiteOk) {
       std::cerr <<  "Failed to allocate tensors!" << std::endl;
       exit(-1);
    }
    Copy to clipboard

**Parent Topic:** [运行推理](https://docs.qualcomm.com/doc/80-70014-54Y/topic/run-inference.html)

Last Published: Aug 06, 2024

[Previous Topic
加载 TensorFlow Lite 模型](https://docs.qualcomm.com/bundle/publicresource/80-70014-54Y/topics/load-a-tensorflow-lite-model.md) [Next Topic
用选定的 delegate 准备一个模型](https://docs.qualcomm.com/bundle/publicresource/80-70014-54Y/topics/prepare-a-model-with-a-chosen-delegate.md)