gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 10:23:11 26 4
gpt4 key购买 nike

我已经写了python客户端通过tensor flow请求wide&deep模型服务成功,但是我不知道如何使用java来解决它,因为示例和文档太少了。使用python我成功运行了它,因为它可以通过Features Dict告诉Tensor flow Serving如何处理features.like flows:

 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 我不知道如何传递特征字典来告诉张量 flow_serving 如何处理特征。我已经写了 java 客户端但是得到流错误我没有传递特征图

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

最佳答案

这是我设法使宽深度模型客户端 (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客户端请求为wide&deep模型服务的tensorflow或如何使用java加载wide&deep模型并进行预测?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47196792/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com