我有一个保存的模型,我设法加载,运行和预测1行9个功能。 (输入)现在我试图预测这样的100行,但是当试图将结果从Tensor.copyTo()读取到结果数组时,我得到了不兼容的形状
java.lang.IllegalArgumentException: cannot copy Tensor with shape [1, 1] into object with shape [100, 1]
显然我设法在循环中运行这一个预测 - 但这比一次运行中100的等效python执行慢20倍。
这是/saved_model_cli.py报告的已保存模型信息
MetaGraphDef with tag-set: 'serve' contains the following SignatureDefs:
signature_def['serving_default']:
The given SavedModel SignatureDef contains the following input(s):
inputs['input'] tensor_info:
dtype: DT_FLOAT
shape: (-1, 9)
name: dense_1_input:0
The given SavedModel SignatureDef contains the following output(s):
outputs['output'] tensor_info:
dtype: DT_FLOAT
shape: (-1, 1)
name: dense_4/BiasAdd:0
Method name is: tensorflow/serving/predict
问题是 - 我需要运行()我想要预测的每一行,如问题here
好的,所以我发现问题我不能为我想要的所有行(预测)运行一次。可能是一个张量流新手问题,我搞砸了输入和输出矩阵。当报表工具(python)说你有一个输入Tensor with shape(-1,9)映射到java long [] {1,9}时,它并不意味着你不能传递输入为long [] {1000,9 - 这意味着1000行用于预测。在此输入之后,定义为[1,1]的输出张量可以是[1000,1]。
这段代码实际运行速度比python快(1.2秒vs 7秒)这里是代码(可能会解释得更好)
public Tensor prepareData(){
Random r = new Random();
float[]inputArr = new float[NUMBER_OF_KEWORDS*NUMBER_OF_FIELDS];
for (int i=0;i<NUMBER_OF_KEWORDS * NUMBER_OF_FIELDS;i++){
inputArr[i] = r.nextFloat();
}
FloatBuffer inputBuff = FloatBuffer.wrap(inputArr, 0, NUMBER_OF_KEWORDS*NUMBER_OF_FIELDS);
return Tensor.create(new long[]{NUMBER_OF_KEWORDS,NUMBER_OF_FIELDS}, inputBuff);
}
public void predict (Tensor inputTensor){
try ( Session s = savedModelBundle.session()) {
Tensor result;
long globalStart = System.nanoTime();
result = s.runner().feed("dense_1_input", inputTensor).fetch("dense_4/BiasAdd").run().get(0);
final long[] rshape = result.shape();
if (result.numDimensions() != 2 || rshape[0] <= NUMBER_OF_KEWORDS) {
throw new RuntimeException(
String.format(
"Expected model to produce a [N,1] shaped tensor where N is the number of labels, instead it produced one with shape %s",
Arrays.toString(rshape)));
}
float[][] resultArray = (float[][]) result.copyTo(new float[NUMBER_OF_KEWORDS][1]);
System.out.println(String.format("Total of %d, took : %.4f ms", NUMBER_OF_KEWORDS, ((double) System.nanoTime() - globalStart) / 1000000));
for (int i=0;i<10;i++){
System.out.println(resultArray[i][0]);
}
}
}