【发布时间】:2020-09-07 19:38:35
【问题描述】:
我在设备上使用 .tflite 模型。最后一层是 ConditionalRandomField 层,我需要该层的权重来进行预测。 如何使用 c++ api 获取权重?
相关:How can I view weights in a .tflite file?
Netron 或 flatc 无法满足我的需求。设备太重了。
似乎 TfLiteNode 将权重存储在 void* user_data 或 void* builtin_data 中。如何阅读它们?
更新:
结论:.tflite 不存储 CRF 权重,而 .h5 剂量。 (也许是因为它们不影响输出。)
我做什么:
// obtain from model.
Interpreter *interpreter;
// get the last index of nodes.
// I'm not sure if the index sequence of nodes is the direction which tensors or layers flows.
const TfLiteNode *node = &((interpreter->node_and_registration(interpreter->nodes_size()-1))->first);
// then follow the answer of @yyoon
【问题讨论】:
-
.tflite 模型中层的权重是固定的,因此通常您不需要在设备运行时读取这些权重。你能解释一下你为什么需要这个吗?
-
@yyoon 在命名实体识别任务中,ConditionalRandomField 层的权重不会影响输出,但会影响最佳路径。没有 CRF
-
所以我的问题实际上是关于为什么您需要使用 TFLite API“在运行时”读取值。您能否提前从 .tflite 模型中读取权重,然后在您的应用程序中烘焙这些值以进行预测?
-
@yyoon 如果没有 CRF 权重,我只能使用 [max(P(i))] 作为 NER 标签。使用 CRF 权重,维特比算法将应用于输出以获得标签的最佳路径。目前,我在训练时将 CRF 权重与 .tflite 一起保存为 .txt。但我认为这是一个不好的做法。 “提前从 .tflite 模型中读取权重”而不是来自训练是我所要求的。
-
有道理。请在下面查看我的答案。
标签: tensorflow-lite