本文介绍了Tensorflow java api - 第一个预测时间与第二个或更多的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到这样的情况,如果我用 java api 加载模型并进行预测,第一个模型花费的时间比其他任何模型都长得多.有一些懒惰的评估之类的东西,我的意思是模型权重直到第一次预测才加载?这背后的原因是什么?

I'm experiencing that if I load a model with the java api and does prediction the first one takes significantly longer than any other later. There's some lazy evaluation like thing, I mean the model weights not loaded till the first prediction? What's the reason behind this?

        try (Session s = load.session()) {
            Tensor result = null;
            startTime = System.nanoTime();
            result = s.runner().feed("input", data).fetch("prediction").run().get(0);
            endTime = System.nanoTime();
            System.out.println(String.format("First prediction performance: %.4f ms", ((double)endTime - startTime)/1000000));

            startTime = System.nanoTime();
            result = s.runner().feed("input", data).fetch("prediction").run().get(0);
            endTime = System.nanoTime();
            System.out.println(String.format("2nd prediction performance: %.4f ms", ((double)endTime - startTime)/1000000));

            System.out.println(result.toString());

        }
  1. 首次预测性能:10.6066 毫秒
  2. 第二次预测性能:0.4776 毫秒

推荐答案

简答

是的,这是正常行为,而不是引起警报的原因.

Short Answer

Yes, this is normal behavior and not a cause of alarm.

Tensorflow 使用一个图(我相信你已经意识到)它定义了操作的顺序和流程.在您定义它们时,它没有定义如何以最佳方式执行它们.这一切都是在第一次运行时解决的.所以是的,一些延迟加载或更精确的延迟计算.

Tensorflow uses a graph (as I'm sure you've come to realize) which defines the order and flow of operations. It does not define how to optimally execute them while you define them. That is all sorted out upon the first run. So yes, some lazy loading or more precisely lazy computation.

很可能您将使用 TensorFlow 处理数千或数百万个条目,因此前 1 个比正常情况长 10 毫秒这一事实应该不成问题.如果您使用 TensorFlow 作为服务,那么您可能希望长时间保持会话打开,类似于您为多个查询保持 SQL 连接打开并且不通过 tcp/ip 或每个请求重新连接的方式.

Most likely you'll be using TensorFlow to process thousands or millions of entries, so the fact that the first 1 takes 10ms longer than normal should not be a problem. If you are using TensorFlow as a service then you'll probably want to keep a session open for a long time, similar to how you'd keep an SQL connection open for multiple queries and not reconnect over tcp/ip or each request.

这篇关于Tensorflow java api - 第一个预测时间与第二个或更多的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 16:33