简体   繁体   English

tensorflow java模型推断将获取的张量转换为字符串?

[英]tensorflow java model inference to convert fetched tensor to string?

I'm now using tensorflow(python) to train my models, and want to use tensorflow(java) to inference result online. 我现在正在使用tensorflow(python)训练我的模型,并想使用tensorflow(java)在线推断结果。

computation graph has an operation to return shape[1,16] result, each element in the tensor is a string. 计算图具有返回shape [1,16]结果的操作,张量中的每个元素都是一个字符串。 Now I want to convert the result into whole string. 现在,我想将结果转换为整个字符串。

I create a ByteBuffer, and call Tensor.writeTo to write data in the buffer. 我创建一个ByteBuffer,然后调用Tensor.writeTo将数据写入缓冲区。 But when I decode the final buffer, it has some unexpected chars in the headings, I guess the final bytes may include some tensor meta information. 但是,当我解码最终缓冲区时,它的标题中有一些意外的字符,我想最终字节可能包含一些张量元信息。

Tensor predictedTensor = result.get(0);
ByteBuffer bb = ByteBuffer.allocate(predictedTensor.numBytes());
predictedTensor.writeTo(bb);
String predictedTokens = null;
byte[] bbArray = bb.array();
predictedTokens = new String(bbArray, "UTF-8");

and the result is like this: first part is some incorrect codes, the last part is right. 结果是这样的:第一部分是一些不正确的代码,最后一部分是正确的。

& *  ? *  C J M X & *  ? *  C J M X hello,world!

I guess maybe the Tensor with shape(1,16) has meta information in the bytes, but I don't how to fetch what I need part. 我想也许具有shape(1,16)的Tensor在字节中有元信息,但是我不知道如何获取我需要的部分。

Can anyone know how to convert a multi-dimensional tensor into java string in java tensorflow interface? 谁能知道如何在Java tensorflow接口中将多维张量转换为Java字符串吗?

I find a workaroud for this! 我为此找到了工作! When train the model, I call tf.reduce_join on the tensor with shape(1,16) to get one scalar. 训练模型时,我在带有shape(1,16)的张量上调用tf.reduce_join以获得一个标量。 And when inference in java language, I just fetch that scalar node, and call tensor.byteValue() to get tensor bytes. 当使用Java语言进行推理时,我只是获取该标量节点,然后调用tensor.byteValue()以获取张量字节。 It will return pure result without heading codes. 它将返回不带标题代码的纯结果。

If the result of the operation has shape [1, 16] , then it means it is producing 16 different strings, not one string. 如果运算结果的形状为[1, 16] ,则表示它正在产生16个不同的字符串,而不是一个字符串。

Support for multi-dimensional string tensors in Java was added only recently ( github commit ) and is not included in the pre-built binaries for TensorFlow release 1.3 and before. 仅在最近才添加了对Java中多维字符串张量的支持( github commit ),并且未包含在TensorFlow版本1.3及更高版本的预构建二进制文件中。 You will have to either build from source or wait for the TensorFlow 1.4 release for that. 您将不得不从源代码构建,或者为此而等待TensorFlow 1.4版本。

With that feature you should be able to decode your (1, 16) shaped tensor with something like this: 使用该功能,您应该可以使用以下内容解码(1, 16)形的张量:

Tensor predictedTensor = result.get(0);
byte[][][] predictedTokenBytes = predictedTensor.copyTo(new byte[1][16][]);
String[] predictedTokens = new String[16];
for (int i = 0; i < 16; ++i) {
  // This works under the assumption that the model is actually
  // producing UTF-8 strings    
  predictedTokens[i] = new String(predictedTokenBytes[0][i], "UTF-8");
}

If you really need a single string, then yeah, you could use tf.reduce_join to have the model combine the 16 strings into one and then extract the scalar. 如果确实需要单个字符串,那么可以,可以使用tf.reduce_join将模型将16个字符串组合为一个,然后提取标量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM