【问题标题】:How to make predictions on TensorFlow's Wide and Deep model loaded in TensorFlow Servings model_server如何对 TensorFlow Servings model_server 中加载的 TensorFlow 的 Wide and Deep 模型进行预测
【发布时间】:2016-11-17 21:53:31
【问题描述】:

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

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

【问题讨论】:

  • 你看完教程了吗:tensorflow.github.io/serving/serving_basic ?您必须在训练计划中导出模型。
  • 是的,我已经浏览了这个文档并且我已经导出了我的模型。但是,我需要对我训练有素的模型进行预测,我需要帮助。
  • mnist和wide n deep模型在服务方面没有太大区别。
  • 如何创建request=predict_pb2.PredictRequest()?
  • 有人可以指导我发送预测请求的文档

标签: tensorflow tensorflow-serving


【解决方案1】:

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

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

但是,请注意弃用说明 here 以备将来兼容。

【讨论】:

    【解决方案2】:

    如果您的模型是使用 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 有更多的细节。

    希望对你有帮助。

    【讨论】:

      猜你喜欢
      • 2016-11-26
      • 2018-04-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-11
      • 2020-06-24
      • 2016-02-16
      • 2020-05-20
      相关资源
      最近更新 更多