tensorflow - How can I create a Tensor from an Example object in Java? -


my use case: trying serve models trained python within our existing jvm service libtensorflow_jni.

now able load model using savedmodelbundle.load(). find hard feed request model. user request not scalar matrix, map of features, like:

{'gender':1, 'age': 20, 'country': 100, other features ...} 

by searching around tensor flow tutorials, see example protocol buffers may fit here holds list of features. not sure how convert java tensor object.

if create tensor directly serialized example object, tensorflow runtime seems not happy datatype. example, following,

tensor inputtensor = tensor.create(example.tobytearray()); s.runner().feed(inputtensorname, inputtensor).fetch(outputtensorname).run().get(0); 

i illegalargumentexception:

java.lang.illegalargumentexception: expected serialized vector, got shape: [] 

could guys shed light how can move forward here in case happens know or have same use cases?

thanks!

looking @ error message, appears problem model expecting vector of string tensors (most corresponding batch of serialized example protocol buffer messages, tf.parse_example) you're feeding scalar string tensor.

unfortunately, till issue #8531 resolved, java api doesn't have way create tensor of strings except scalars. once issue resolved, things easier.

in mean time, work around constructing tensorflow "model" convert scalar string vector of size 1 :). done this:

// tensorflow "model" reshapes string scalar vector. // should prettier once https://github.com/tensorflow/tensorflow/issues/7149 // resolved. private static class reshaper implements autocloseable {   reshaper() {     this.graph = new graph();     this.session = new session(graph);     this.in =         this.graph.opbuilder("placeholder", "in")             .setattr("dtype", datatype.string)             .build()             .output(0);     try (tensor shape = tensor.create(new int[] {1})) {       output vectorshape =           this.graph.opbuilder("const", "vector_shape")               .setattr("dtype", shape.datatype())               .setattr("value", shape)               .build()               .output(0);       this.out =           this.graph.opbuilder("reshape", "out").addinput(in).addinput(vectorshape).build().output(0);     }   }    @override   public void close() {     this.session.close();     this.graph.close();   }    public tensor vector(tensor input) {     return this.session.runner().feed(this.in, input).fetch(this.out).run().get(0);   }    private final graph graph;   private final session session;   private final output in;   private final output out; } 

with above, can convert example proto tensor vector , feed model you're interested in this:

tensor inputtensor = null; try (tensor scalar = tensor.create(example.tobytearray())) {   inputtensor = reshaper.vector(scalar); } s.runner().feed(inputtensorname, inputtensor).fetch(outputtensorname).run().get(0); 

for full details, see example on github

hope helps!


Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -