gpt4 book ai didi

java - 从 Java 中的 Tensorflow 服务模型获取 Matrix 响应

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

我目前正在使用 Python 构建模型,并从另一个 Java 客户端获取结果。

我需要知道如何获得 float[][]List<List<Float>> (类似的东西)来自具有超过 1 维的 TensorProto。

在 Python 中,完成这项工作可能非常容易:

from tensorflow.python.framework import tensor_util
.
.
.
print tensor_util.MakeNdarray(tensorProto)

=====更新=======:

Java 的 tensorProto.getFloatValList()如果它是由 Python 创建的 tensor_util.make_tensor_proto(vector) 则不起作用,或者。

以上所有情况都可以通过@Ash的回答来解决

最佳答案

正如艾伦在评论中提到的,这可能是一个很好的功能请求。

但在此期间,解决方法是构建并运行一个图来解析编码的 protobuf 并返回一个 Tensor。它不会特别有效,但你可以这样做:

import org.tensorflow.*;
import java.util.Arrays;

public final class ProtoToTensor {

public static Tensor<Float> tensorFromSerializedProto(byte[] serialized) {
// One may way to cache the Graph and Session as member variables to avoid paying the cost of
// graph and session construction on each call.
try (Graph g = buildGraphToParseProto();
Session sess = new Session(g);
Tensor<String> input = Tensors.create(serialized)) {
return sess.runner()
.feed("input", input)
.fetch("output")
.run()
.get(0)
.expect(Float.class);
}
}

private static Graph buildGraphToParseProto() {
Graph g = new Graph();
// The graph construction process in Java is currently (as of TensorFlow 1.4) very verbose.
// Once https://github.com/tensorflow/tensorflow/issues/7149 is resolved, this should become
// *much* more convenient and succint.
Output<String> in =
g.opBuilder("Placeholder", "input")
.setAttr("dtype", DataType.STRING)
.setAttr("shape", Shape.scalar())
.build()
.output(0);
g.opBuilder("ParseTensor", "output").setAttr("out_type", DataType.FLOAT).addInput(in).build();
return g;
}

public static void main(String[] args) {
// Let's say you got a byte[] representation of the proto somehow.
// In this case, I got it from Python from the following program
// that serializes the 1x1 matrix:
/*
import tensorflow as tf
list(bytearray(tf.make_tensor_proto([[1.]]).SerializeToString()))
*/
byte[] bytes = {8, 1, 18, 8, 18, 2, 8, 1, 18, 2, 8, 1, 42, 4, 0, 0, (byte)128, 63};
try (Tensor<Float> t = tensorFromSerializedProto(bytes)) {
// You can now get an float[][] array using t.copyTo().
// t.shape() gives shape information.
System.out.println("Tensor: " + t);
float[][] f = t.copyTo(new float[1][1]);
System.out.println("float[][]: " + Arrays.deepToString(f));
}
}
}

正如您所看到的,这是使用一些相当低级的 API 来构建图表和 session 。提出一个用一行替换所有这些的功能请求是合理的:

Tensor<Float> t = Tensor.createFromProto(serialized);

关于java - 从 Java 中的 Tensorflow 服务模型获取 Matrix 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47174646/

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