Tensorflow自定义估算器和tf.contrib.predictor.from_estimator馈送张量-馈送的值不能是tf.Tensor对象

时间:2018-09-25 01:02:01

标签: tensorflow tensorflow-estimator

我有一个自定义估算器E1,它在其预测操作中使用了另一个自定义估算器E2的tf.contrib.predictor.from_estimator

估计器的预测器是通过以下服务输入函数定义的:

def serving_input_fn():
    inputs_x = tf.placeholder(tf.int64, shape=[batch_size, input_len], name='inputs_x')
    inputs_y = tf.placeholder(tf.int64, shape=[batch_size, input_len], name='inputs_y')
    features = {'inputs_x':inputs_x, 'inputs_y':inputs_y}
    return tf.estimator.export.ServingInputReceiver(features, features)

predictor = tf.contrib.predictor.from_estimator(custom_estimator_model_2, serving_input_fn)
model = CustomEstimator(model_dir, params, predictor=predictor)

在E1的模型函数中,我计算出一个张量,需要像这样将其馈送到predictor中。

 feats_x = tf.gather_nd(.....)
 feats_y = tf.gather_nd(.....)
 inputs = {'inputs_x': feats_x, 'inputs_y': feats_y}
 predictor(inputs)

但是,我无法将张量输入到提要中。

TypeError: The value of a feed cannot be a tf.Tensor object. Acceptable feed values include Python scalars, strings, lists, numpy ndarrays, or TensorHandles.

由于我所有的张量操作都在模型函数之内,因此在提供这些张量之前,我无需进行会话来评估这些张量。除了解决这个问题,还有其他选择吗?我应该修改我的serve_input_fn使其不包含占位符吗?由于性能提高,我仍然想使用预测变量。

0 个答案:

没有答案