java—我得到了一个illegalrankexception:在运行我的模型时不是一个标量值异常

goqiplq2  于 2021-07-07  发布在  Java
关注(0)|答案(0)|浏览(287)

当我试着运行以下程序时。我收到一个空的结果。数据和指针都为空。在代码下面,您还可以找到模型的输入和输出签名。一切似乎都很好。有什么想法吗?
图书馆:org.tensorflow:tensorflow-core-platform:0.2.0

private static void tensorflowTest() {

        final var smb = SavedModelBundle.load("./python/project1/models/RNN_Final-10-1606839953.model", "serve");
        final var session = smb.session();

        final var sequence = NdArrays.ofFloats(Shape.of(120, 2));

        for (int i = 0; i < 120; i++) {
            sequence.set(NdArrays.vectorOf(0.2f, 0.5f), i);
        }
        final var sequences = NdArrays.ofFloats(Shape.of(1, 120, 2));
        sequences.set(sequence, 0);

        predict(session, TFloat32.tensorOf(sequences));
    }

    private static void predict(final Session sess, final Tensor<TFloat32> inputTensor) {
        final List<Tensor<?>> results = sess.runner()
                .feed("serving_default_lstm_input:0", inputTensor)
                .fetch("StatefulPartitionedCall:0")
                .run();

        results.forEach(tensor -> {

            // Error here
            // IllegalRankException: Not a scalar value
            // Tensor data and pointer are null.
            final var tensorResult = tensor.expect(TFloat32.DTYPE);

            final var fl = tensorResult.data().getFloat(0);

            log.info("Float value: {}", fl);
        });
    }

下面您可以看到我的模型输入和输出定义:

The given SavedModel SignatureDef contains the following input(s):
  inputs['lstm_input'] tensor_info:
      dtype: DT_FLOAT
      shape: (-1, 120, 2)
      name: serving_default_lstm_input:0
The given SavedModel SignatureDef contains the following output(s):
  outputs['dense_1'] tensor_info:
      dtype: DT_FLOAT
      shape: (-1, 2)
      name: StatefulPartitionedCall:0
Method name is: tensorflow/serving/predict

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题