2

有人可以帮助我对加载到 TensorFlow Serving 的 model_server 中的 TensorFlow 的 Wide and Deep Learning 模型进行预测吗?

如果有人可以向我指出相同的资源或文档,那将非常有帮助。

4

2 回答 2

1

您可以尝试调用估计器的 predict 方法并将 as_iterable 设置为 false 的 ndarray

y = m.predict(input_fn=lambda: input_fn(df_test), as_iterable=False)

但是,请注意此处的弃用说明,以便将来兼容。

于 2016-12-19T22:53:59.757 回答
0

如果您的模型是使用导出的Estimator.export_savedmodel()并且您成功构建了 TensorFlow Serving 本身,您可以执行以下操作:

from grpc.beta import implementations
from tensorflow_serving.apis import predict_pb2
from tensorflow_serving.apis import prediction_service_pb2

tf.app.flags.DEFINE_string('server', 'localhost:9000', 'Server host:port.')
tf.app.flags.DEFINE_string('model', 'wide_and_deep', 'Model name.')
FLAGS = tf.app.flags.FLAGS
...
def main(_):

  host, port = FLAGS.server.split(':')
  # Set up a connection to the TF Model Server
  channel = implementations.insecure_channel(host, int(port))
  stub = prediction_service_pb2.beta_create_PredictionService_stub(channel)

  # Create a request that will be sent for an inference
  request = predict_pb2.PredictRequest()
  request.model_spec.name = FLAGS.model
  request.model_spec.signature_name = 'serving_default'

  # A single tf.Example that will get serialized and turned into a TensorProto
  feature_dict = {'age': _float_feature(value=25),
                  'capital_gain': _float_feature(value=0),
                  'capital_loss': _float_feature(value=0),
                  'education': _bytes_feature(value='11th'.encode()),
                  'education_num': _float_feature(value=7),
                  'gender': _bytes_feature(value='Male'.encode()),
                  'hours_per_week': _float_feature(value=40),
                  'native_country': _bytes_feature(value='United-States'.encode()),
                  'occupation': _bytes_feature(value='Machine-op-inspct'.encode()),
                  'relationship': _bytes_feature(value='Own-child'.encode()),
                  'workclass': _bytes_feature(value='Private'.encode())}
  label = 0

  example = tf.train.Example(features=tf.train.Features(feature=feature_dict))
  serialized = example.SerializeToString()

  request.inputs['inputs'].CopyFrom(
    tf.contrib.util.make_tensor_proto(serialized, shape=[1]))

  # Create a future result, and set 5 seconds timeout
  result_future = stub.Predict.future(request, 5.0)
  prediction = result_future.result().outputs['scores']

  print('True label: ' + str(label))
  print('Prediction: ' + str(np.argmax(prediction)))

在这里,我写了一个简单的教程Exporting and Serving a TensorFlow Wide & Deep Model,其中包含更多细节。

希望能帮助到你。

于 2017-05-09T18:59:39.020 回答