现在我们使用tensorflow/serving进行推理。它公开了 gRPC 服务,我们可以从 proto 文件生成 Java 类。
现在我们可以生成PreditionService
from https://github.com/tensorflow/serving/blob/master/tensorflow_serving/apis/prediction_service.proto https://github.com/tensorflow/serving/blob/master/tensorflow_serving/apis/prediction_service.proto但我怎样才能构建TensorProto
来自多维数组的对象。
我们有一些来自 Python ndarray 和 C++ 的示例。如果有人尝试过 Java 那就太好了。
有一些关于在 Java 中运行 TensorFlow 的工作。这是blog https://medium.com/google-cloud/how-to-invoke-a-trained-tensorflow-model-from-java-programs-27ed5f4f502d#.utw5gb708但我不确定它是否有效,或者我们如何在没有依赖关系的情况下使用它。
TensorProto https://github.com/tensorflow/tensorflow/blob/master/tensorflow/core/framework/tensor.proto支持张量内容的两种表示形式:
各种种类repeated *_val
字段(例如TensorProto.float_val
, TensorProto.int_val
),它将内容存储为原始元素的线性数组,按行优先顺序。
The TensorProto.tensor_content
字段,将内容存储为单字节数组,对应于结果tensorflow::Tensor::AsProtoTensorContent() https://github.com/tensorflow/tensorflow/blob/37256f4857cdadefa09e0505f4acc91ffbf626e2/tensorflow/core/framework/tensor.cc#L654。 (一般来说,这个表示对应于一个内存中的表示tensorflow::Tensor
,转换为字节数组,但是DT_STRING
类型的处理方式不同。)
可能会更容易生成TensorProto
使用第一种格式的对象,尽管效率较低。假设你有一个二维float
数组称为tensorData
在您的 Java 程序中,您可以使用以下代码作为起点:
float[][] tensorData = ...;
TensorProto.Builder builder = TensorProto.newBuilder();
// Set the shape and dtype fields.
// ...
// Set the float_val field.
for (int i = 0; i < tensorData.length; ++i) {
for (int j = 0; j < tensorData[i].length; ++j) {
builder.addFloatVal(tensorData[i][j]);
}
}
TensorProto tensorProto = builder.build();
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)