java - 如何使用java客户端请求tensorflow为wide&deep模型提供服务或者如何使用java加载wide&deep模型并预测?

标签 java tensorflow client tensorflow-serving

我已经编写了python客户端通过 tensorflow 请求宽和深模型服务成功,但我不确定如何使用java来解决它,因为示例和文档太缺乏。 使用python我已经成功运行它,因为它可以通过Features Dict告诉Tensor flow Serving如何处理features.like flow:

 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]))
  result_future = stub.Predict.future(request, 1.0)

但是使用java我不知道如何传递feature dict来告诉tensor flow_serving如何处理features。我已经编写了java客户端但是得到了流错误我没有传递feature map

Nov 09, 2017 7:18:09 AM com.bj58.gul.model.entity.TestWideAndDeepModelClient predict
WARNING: RPC failed: Status{code=INVALID_ARGUMENT, description=Name: <unknown>, Feature: getGBDTDiffTimeBetweenItemShowTimeAndCreatedTime (data type: float) is required but could not be found.
     [[Node: ParseExample/ParseExample = ParseExample[Ndense=15, Nsparse=66, Tdense=[DT_FLOAT, DT_FLOAT, DT_FLOAT, DT_FLOAT, DT_FLOAT, DT_FLOAT, DT_FLOAT, DT_FLOAT, DT_FLOAT, DT_FLOAT, DT_FLOAT, DT_FLOAT, DT_FLOAT, DT_FLOAT, DT_FLOAT], _output_shapes=[[?,2], [?,2], [?,2], [?,2], [?,2], [?,2], [?,2], [?,2], [?,2], [?,2], [?,2], [?,2], [?,2], [?,2], [?,2], [?,2], [?,2], [?,2], [?,2], [?,2], [?,2], [?,2], [?,2], [?,2], [?,2], [?,2], [?,2], [?,2], [?,2], [?,2], [?,2], [?,2], [?,2], [?,2], [?,2], [?,2], [?,2], [?,2], [?,2], [?,2], [?,2], [?,2], [?,2], [?,2], [?,2], [?,2], [?,2], [?,2], [?,2], [?,2], [?,2], [?,2], [?,2], [?,2], [?,2], [?,2], [?,2], [?,2], [?,2], [?,2], [?,2], [?,2], [?,2], [?,2], [?,2], [?,2], [?], [?], [?], [?], [?], [?], [?], [?], [?], [?], [?], [?], [?], [?], [?], [?], [?], [?], [?], [?], [?], [?], [?], [?], [?], [?], [?], [?], [?], [?], [?], [?], [?], [?], [?], [?], [?]...TRUNCATED, cause=null}
Nov 09, 2017 7:18:09 AM io.grpc.internal.ManagedChannelImpl maybeTerminateChannel
INFO: [ManagedChannelImpl@3cb5cdba] Terminated
End of predict client

最佳答案

这是一段伪代码片段,我设法使 Wide&Deep 模型客户端 (java) 工作。

前提条件:您已将导出的模型投入使用( wide_and_deep_tutorial )。

import com.google.common.collect.ImmutableMap;
import com.google.protobuf.ByteString;
import com.google.protobuf.Int64Value;
import org.tensorflow.example.*;
import org.tensorflow.framework.DataType;
import org.tensorflow.framework.TensorProto;
import org.tensorflow.framework.TensorShapeProto;
import org.tensorflow.framework.TensorShapeProto.Dim;
import tensorflow.serving.Model.ModelSpec;
import tensorflow.serving.Predict.PredictRequest;
import tensorflow.serving.Predict.PredictResponse;
import tensorflow.serving.PredictionServiceGrpc.PredictionServiceBlockingStub;

......(here the declare of class and function is neglected, only showing the core part below)

private static final PredictionServiceBlockingStub stub = PredictionServiceGrpc.newBlockingStub(new ForceDeadlineChannel(TF_SERVICE_HOST, TF_SERVICE_PORT, 5000));
private HashMap<String, Feature> inputFeatureMap = new HashMap();
private ByteString inputStr;
Integer modelVer = 123;

......

for (each feature in feature list) {
    Feature feature = null;
        if (type is string) {
            feature = Feature.newBuilder().setBytesList(BytesList.newBuilder().addValue(ByteString.copyFromUtf8("dummy string"))).build();
        } else if (type if float) {
            feature = Feature.newBuilder().setFloatList(FloatList.newBuilder().addValue(3.1415f)).build();
        } else if (type is int) {
            feature = Feature.newBuilder().setInt64List(Int64List.newBuilder().addValue(1l)).build();
        }
        if (feature != null) {
            inputFeatureMap.put(name, feature);
        }
        Features features = Features.newBuilder().putAllFeature(inputFeatureMap).build();
        inputStr = Example.newBuilder().setFeatures(features).build().toByteString();
}

TensorProto proto = TensorProto.newBuilder()
            .addStringVal(inputStr)
            .setTensorShape(TensorShapeProto.newBuilder().addDim(TensorShapeProto.Dim.newBuilder().setSize(1).build()).build())
            .setDtype(DataType.DT_STRING)
            .build();

PredictRequest req = PredictRequest.newBuilder()
            .setModelSpec(ModelSpec.newBuilder()
                    .setName("your serving model name")
                    .setSignatureName("serving_default")
                    .setVersion(Int64Value.newBuilder().setValue(modelVer)))
            .putAllInputs(ImmutableMap.of("inputs", proto))
            .build();

PredictResponse response = stub.predict(req);

System.out.println(response.getOutputsMap());
......

关于java - 如何使用java客户端请求tensorflow为wide&deep模型提供服务或者如何使用java加载wide&deep模型并预测?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47196792/

相关文章:

java - 为什么我的 java 猜谜游戏在第一个循环后不起作用

java - 在任何范围内都找不到 bean org.apache.struts.taglib.html.BEAN

python - 如何在 Linux 上使用没有 CUDA 的 TensorFlow?

tensorflow - 将稀疏张量密集形状转换为 tensorflow 中的整数值

java - CXF RESTful 客户端 - 如何信任所有证书?

javascript - 在 JavaScript 中播放字符串中的音频文件

无法使用在 c 中处理多个客户端的服务器在客户端之间平均分配工作

java - 我的刚体碰撞代码如何不一致?

java - 使用我的 java 代码连接到 MySQL 数据库时遇到问题

python - Tensorflow:如何将张量提供给经过训练的神经网络?