创建一个接受 JSON 输入的 tf.contrib.learn 估算器服务

Create a tf.contrib.learn Estimator serving that takes JSON input

我正在寻找一些代码,我可以使用这些代码从张量流 Estimator 中导出模型,该模型会将 JSON 作为输入。我可以使用 tf.estimator.export.ServingInputReceivertf.Estimator 一起工作,但对于内置于 tf.contrib.learn 中的模型,我找不到任何文档。有一个示例 here 创建了包含 tf.Example 服务的导出,但是 Example 构建起来有点棘手。

CloudML Engine 的示例中有几个示例 repository, e.g.this code

也就是说,您创建占位符并将它们传递给 ServingInputReceiver 构造函数。最外面的维度应该是 'None' 以处理可变大小的批次。

def build_receiver():
  x = tf.placeholder(tf.float32, size=[None])
  y = tf.placeholder(tf.int32, size=[None, 128, 128, 3])
  features = {'x': x, 'y': y}
  return tf.estimator.export.ServingInputReceiver(features, features)

要使用贡献估计器,您必须查看示例的早期版本。这是一个例子:

https://github.com/GoogleCloudPlatform/training-data-analyst/blob/85c57e4da2e7edeffbb6652636e3c65b313c568f/blogs/babyweight/babyweight/trainer/model.py

并不是说您要返回一个输入函数 ops。话虽如此,如果可以的话,我建议您迁移到 tf.estimator。

查看 here 的一组示例,这些示例展示了如何在 Cloud ML 中为服务模型使用 tensorflow 估计器

代码:

def serving_fn():
receiver_tensor = {
    commons.FEATURE_COL: tf.placeholder(dtype=tf.string, shape=None)
}

features = {
    key: tensor
    for key, tensor in receiver_tensor.items()
}

return tf.estimator.export.ServingInputReceiver(features, receiver_tensor)