2017-11-09 10 views
1

私はファイナンスクライアントにテンソルフローによる深いモデル&を書くことができました。しかし、例と文書があまりにも不足しているので、それを解決するためにJavaを使う方法は疑問です。それは機能がfeatures.likeフローを処理する方法をサービングテンソルの流れを伝えるために辞書渡すことができますので、私は、正常に実行にそれを持っている 使用パイソン:Javaクライアントを使用して、テンソルフローをワイド&ディープモデルに対応する方法、またはJavaを使用してワイド&ディープモデルをロードして予測する方法は?

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を使用して私が言うことフィーチャ辞書を渡す方法がわかりませんfeatures.iを処理する方法を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: [[email protected]] Terminated 
End of predict client 

答えて

1

に合格していないここで私は広い&深いモデルクライアント(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()); 
...... 
+0

ありがとう、私はこの問題を同じ方法で解決しました – wangyong

関連する問題