如何保持查找表初始化以进行预测(而不仅仅是训练)? [英] How to keep lookup tables initialized for prediction (and not just training)?

查看:18
本文介绍了如何保持查找表初始化以进行预测(而不仅仅是训练)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从 tf.contrib.lookup,使用训练数据(作为输入).然后,在通过我的模型之前,我通过该查找表传递每个输入.

I create a lookup table from tf.contrib.lookup, using the training data (as input). Then, I pass every input through that lookup table, before passing it through my model.

这适用于训练,但是当涉及到来自同一模型的在线预测时,它会引发错误:

This works for training, but when it comes to online prediction from this same model, it raises the error:

表未初始化

我正在使用 SavedModel 来保存模型.我从这个保存的模型运行预测.

I'm using SavedModel to save the model. I run the prediction from this saved model.

我如何初始化这个表以使其保持初始化状态?或者有没有更好的方法来保存模型,以便始终初始化表?

How can I initialize this table so that it stays initialized? Or is there a better way to save the model so that the table is always initialized?

推荐答案

当您使用 tf.saved_model.builder.SavedModelBuilder.add_meta_graph,使用main_oplegacy_init_op kwarg.如果需要多个操作,您可以使用单个操作,也可以使用 tf.group 将多个操作组合在一起.

You can specify an "initialization" operation when you add a meta graph to your SavedModel bundle with tf.saved_model.builder.SavedModelBuilder.add_meta_graph, using the main_op or legacy_init_op kwarg. You can either use a single operation, or group together a number of operations with tf.group if you need more than one.

请注意,在 Cloud ML Engine 中,您必须使用 legacy_init_op.但是在以后的 runtime_version 中,您将能够使用 main_op(IIRC,从 runtime_version == 1.2 开始)

Note that in Cloud ML Engine, You'll have to use the legacy_init_op. However in future runtime_versions you will be able to use main_op (IIRC, starting with runtime_version == 1.2)

saved_model 模块提供了一个内置的 tf.saved_model.main_op.main_op 将常见的初始化操作封装在单个操作中(局部变量初始化和表初始化).

The saved_model module provides a built in tf.saved_model.main_op.main_op to wrap up common initialization actions in a single op (local variable initialization, and table initialization).

总之,代码应该是这样的(改编自 这个例子):

So in summary, code should look like this (adapted from this example):

  exporter = tf.saved_model.builder.SavedModelBuilder(
      os.path.join(job_dir, 'export', name))

  # signature_def gets constructed here

  with tf.Session(graph=prediction_graph) as session:
    # Need to be initialized before saved variables are restored
    session.run([tf.local_variables_initializer(), tf.tables_initializer()])
    # Restore the value of the saved variables
    saver.restore(session, latest)
    exporter.add_meta_graph_and_variables(
        session,
        tags=[tf.saved_model.tag_constants.SERVING],
        signature_def_map={
            tf.saved_model.signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY: signature_def
        },
        # Relevant change to the linked example is here!
        legacy_init_op=tf.saved_model.main_op.main_op()
    )

注意:如果您使用的是高级库(例如 tf.estimator) 这应该是默认值,如果您需要指定其他初始化操作,您可以将它们指定为 tf.train.Scaffold 对象,您传递给您的 tf.estimator.EstimatorSpec 在您的 model_fn 中.

NOTE: If you are using the high level libraries (such as tf.estimator) this should be the default, and if you need to specify additional initialization actions you can specify them as part of the tf.train.Scaffold object that you pass to your tf.estimator.EstimatorSpec in your model_fn.

这篇关于如何保持查找表初始化以进行预测(而不仅仅是训练)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆