[英]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());
}
Yes, this is normal behavior and not a cause of alarm. 是的,这是正常现象,不是引起警报的原因。
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.