Java 每个推论的预测相同

Java 每个推论的预测相同,java,tensorflow,Java,Tensorflow,我使用tf.saved_model.builder.SavedModelBuilder保存了一个tensorflow模型。 然而,当我尝试用java进行预测时,在大多数情况下,它返回相同的fc8 alexnet结果,在softmax之前的层在某些情况下,它会产生一些实际不同的结果,并且很可能是正确的,因此,我假设训练是正常的。 还有其他人经历过吗?有人知道怎么了吗 我的Java实现: Tensor image = constructAndExecuteGraphToNormalizeImage(

我使用tf.saved_model.builder.SavedModelBuilder保存了一个tensorflow模型。 然而,当我尝试用java进行预测时,在大多数情况下,它返回相同的fc8 alexnet结果,在softmax之前的层在某些情况下,它会产生一些实际不同的结果,并且很可能是正确的,因此,我假设训练是正常的。 还有其他人经历过吗?有人知道怎么了吗

我的Java实现:

Tensor image = constructAndExecuteGraphToNormalizeImage(imageBytes);    
Tensor result = s.runner().feed("input_tensor", image).feed("Placeholder_1",t).fetch("fc8/fc8").run().get(0);

private static Tensor constructAndExecuteGraphToNormalizeImage(byte[] imageBytes) {
    try (Graph g = new Graph()) {
        TF.GraphBuilder b = new TF.GraphBuilder(g);
        // Some constants specific to the pre-trained model at:
        // https://storage.googleapis.com/download.tensorflow.org/models/inception5h.zip
        //
        // - The model was trained with images scaled to 224x224 pixels.
        // - The colors, represented as R, G, B in 1-byte each were converted to
        //   float using (value - Mean)/Scale.
        final int H = 227;
        final int W = 227;
        final float mean = 117f;
        final float scale = 1f;

        // Since the graph is being constructed once per execution here, we can use a constant for the
        // input image. If the graph were to be re-used for multiple input images, a placeholder would
        // have been more appropriate.
        final Output input = b.constant("input", imageBytes);
        final Output output =
                b.div(
                        b.sub(
                                b.resizeBilinear(
                                        b.expandDims(
                                                b.cast(b.decodeJpeg(input, 3), DataType.FLOAT),
                                                b.constant("make_batch", 0)),
                                        b.constant("size", new int[] {H, W})),
                                b.constant("mean", mean)),
                        b.constant("scale", scale));
        try (Session s = new Session(g)) {
            return s.runner().fetch(output.op().name()).run().get(0);
        }
    }
}

我假设您的图形中没有留下任何随机操作,例如辍学。似乎是这样,因为你经常得到同样的结果

唉,比如约化和卷积。我们不得不接受这样一个事实:tensorflow的网络是随机的野兽:它们的性能可以在统计上接近,但它们的输出是不确定的

我相信其他一些框架,比如Theano,在提出确定性操作方面比tensorflow走得更远