我正在开发一个 Tensorflow 序列模型,该模型通过 OpenFST 解码图(从二进制文件加载)对 Tensorflow 序列模型的 logits 输出使用波束搜索。
我编写了一个自定义操作,允许我对 logits 执行解码,但每次,在执行解码之前,我都会调用 fst::Read(BINARY_FILE) 操作。只要它保持很小,这可能没问题,但我想避免 I/O 开销。
我已经阅读了 Tensorflow custom op 并试图找到类似的例子,但我仍然迷路了。基本上,我想在图中做的是:
FstDecodingOp.Initialize('BINARY_FILE.bin') #loads the BINARY_FILE.bin into memory
...
for o in output:
FstDecodingOp.decode(o) # uses BINARY_FILE.bin to decode
Run Code Online (Sandbox Code Playgroud)
这当然在 tensorflow 图之外的 Python 中很简单,但我最终需要将其移动到 vanilla TF-Serving 环境中,因此需要将其冻结到导出图中。有没有人遇到过类似的问题?
解决方案:
没有意识到您可以使用“OpKernel(context)”设置私有属性。刚刚使用该函数对其进行了初始化。
编辑:关于我是如何做到的更多细节。还没有尝试服务。
REGISTER_OP("FstDecoder")
.Input("log_likelihoods: float")
.Attr("fst_decoder_path: string")
....
...
template <typename Device, typename T>
class FstDecoderOp : public OpKernel {
private:
fst::Fst<fst::StdArc>* fst_;
float beam_;
public:
explicit FstDecoderOp(OpKernelConstruction* context) : OpKernel(context) {
OP_REQUIRES_OK(context, …Run Code Online (Sandbox Code Playgroud)