我有一个训练有素的估计器,当新的输入数据输入时,我用它来做实时预测。
在代码的开头,我实例化了估计器:
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] Creating TensorFlow device (/device:GPU:0) -> (设备: 0,名称: GeForce GTX 1060 6GB,pci总线id: 0000:04:00.0,计算能力: 6.1) 信息:tensorflow:从/home/fgervais/tf/model_dir_3/model.ckpt-103712恢复参数
似乎整个GPU检测过程和模型加载都是在每次预测的基础上进行的。
是否有办法使模型在实时输入之间的内存中被加载,这样我就可以得到更好的预测率?
发布于 2018-05-07 14:52:24
解决方案是使用预测器。
在这个问题的具体背景下,将这样做:
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)})
https://stackoverflow.com/questions/49966447
复制相似问题