简体   繁体   English

Tensorflow Java API-第1次预测时间与第2次或更长时间

[英]Tensorflow java api - 1st prediction time vs 2nd, or more

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. 我遇到的问题是,如果我使用Java api加载模型并进行预测,那么第一个模型所花的时间将比其他任何模型都要长。 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. First prediction performance: 10.6066 ms 一次预测性能:10.6066毫秒
  2. 2nd prediction performance: 0.4776 ms 第2次预测性能:0.4776毫秒

Short Answer 简短答案

Yes, this is normal behavior and not a cause of alarm. 是的,这是正常现象,不是引起警报的原因。

Longer Answer 更长的答案

Tensorflow uses a graph (as I'm sure you've come to realize) which defines the order and flow of operations. Tensorflow使用一个图形(我确定您已经意识到)来定义操作的顺序和流程。 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. 所以是的,有些延迟加载或更精确的是延迟计算。

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. 您很有可能会使用TensorFlow处理成千上万的条目,因此前1个条目比正常时间长10ms的事实应该不是问题。 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作为服务使用,则可能需要长时间保持会话打开状态,类似于您为多个查询保持SQL连接打开状态,而不是通过tcp / ip或每个请求重新连接的方式。

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

相关问题 Java继承:第2个实例调用第1个实例方法 - Java Inheritance : 2nd instance calls 1st instance method Java多线程第二线程等待第一 - Java multithreading 2nd thread waiting for 1st 在 Java 中的字符串中查找第 1 次和第 2 次出现的正则表达式 - Finding the 1st and 2nd occurrence of a regex in a string in Java 在 java 中将第一个数组值乘以 1,将第二个值乘以 3 - Multiply 1st array value with 1 and 2nd value with 3 in java 当我返回到第一个活动时,第二个活动未调用第二个活动的onCreate()函数 - When I return back to 1st Activity the onCreate() function of 2nd Activity is not called in 2nd time 关于第一个init()和第二个init() - regarding 1st init() and 2nd init() 检查第一个 if 条件,然后检查第二个 - Checking 1st if condition and then 2nd 当第一个 API 和第二个 API 在测试环境中工作时,如何模拟第三个 API 响应 - How to mock 3rd API response when 1st API and 2nd API is working on test environment Java awt计算器+/-,如何用-替换+:第一次单击=“-”,第二次单击=“ +”,依此类推 - Java awt calculator +/- , how to replace + with - : 1st click = “-”, 2nd click = “+” and so on 如何在Java Swing中从第一个面板获取价值并插入第二个面板 - How to get value from 1st panel and insert in 2nd panel in Java Swing
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM