Tensorflow用request predict模型如何传scalar

import tensorflow as tf
import numpy as np
from grpc.beta import implementations
from tensorflow_serving.apis import prediction_service_pb2, predict_pb2


channel = implementations.insecure_channel(host, port)
stub = prediction_service_pb2.beta_create_PredictionService_stub(channel)
request = predict_pb2.PredictRequest()
idx = np.array(np.random.randint(0, 10000000, [dim], np.int32))
val = np.ones([dim], dtype=np.float)
sl = 50
pos = 1
request.model_spec.name = 'model'
request.model_spec.signature_name = tf.saved_model.signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY
request.inputs['input_idx'].CopyFrom(tf.contrib.util.make_tensor_proto(idx, shape=[1, dim], dtype=tf.int32)) # 矩阵
request.inputs['sl1'].CopyFrom(tf.contrib.util.make_tensor_proto(sl, shape=[1, ], dtype=tf.float32)) # 向量      
request.inputs['train_phase'].CopyFrom(tf.contrib.util.make_tensor_proto(False, shape=[], dtype=tf.bool)) # 标量
res = stub.Predict(request, 3.0)
ctr_score = res.outputs['output'].float_val
if ctr_score and len(ctr_score) > 0:
    return True

你可能感兴趣的:(Tensorflow用request predict模型如何传scalar)