【问题标题】:Keep the tensorflow estimator in memory while waiting for live prediction inputs在等待实时预测输入时将张量流估计器保留在内存中
【发布时间】:2018-04-22 13:30:21
【问题描述】:

我有一个训练有素的估算器,用于在新输入数据进入时进行实时预测。

在代码的开头我实例化了估算器:

estimator = tf.estimator.Estimator(
    model_fn=model_fn,
    model_dir="{}/model_dir_{}".format(script_dir, 3))

然后在一个循环中,每次我获得足够的新数据进行预测时:

predict_input_fn = tf.estimator.inputs.numpy_input_fn(
    x={"x": np.array([sample.normalized.input_data])},
    num_epochs=1,
    shuffle=False)
predictions = estimator.predict(
    input_fn=predict_input_fn,
)

每次我这样做时,我都会在控制台中收到这些 tensorflow 消息:

2018-04-21 16:01:08.401319: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1195] 创建 TensorFlow 设备 (/device:GPU:0) -> (设备:0,名称:GeForce GTX 1060 6GB, pci bus id: 0000:04:00.0, 计算能力: 6.1)

INFO:tensorflow:从 /home/fgervais/tf/model_dir_3/model.ckpt-103712 恢复参数

似乎每次预测都会重新完成整个 GPU 检测过程和模型加载。

有没有办法在实时输入之间将模型加载到内存中,以便获得更好的预测率?

【问题讨论】:

  • 我不知道为什么人们不赞成这个。我遇到了完全相同的问题,这个解决方案非常有帮助。

标签: tensorflow tensorflow-datasets


【解决方案1】:

解决方案是使用predictor

在问题的具体上下文中,它会这样做:

def serving_input_fn():
    x = tf.placeholder(dtype=tf.float32, shape=[3500], name='x')
    inputs = {'x': x }

    return tf.estimator.export.ServingInputReceiver(inputs, inputs)

estimator = tf.estimator.Estimator(
    model_fn=model_fn,
    model_dir="{}/model_dir_{}/model.ckpt-103712".format(script_dir, 3))

estimator_predictor = tf.contrib.predictor.from_estimator(
                            estimator, serving_input_fn)

p = estimator_predictor(
        {"x": np.array(sample.normalized.input_data)})

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-30
    • 2019-07-20
    • 2020-04-09
    • 2020-04-14
    相关资源
    最近更新 更多