tf.keras.predict()比Keras的预测()慢得多 [英] tf.keras.predict() is much slower than Keras predict()

查看:640
本文介绍了tf.keras.predict()比Keras的预测()慢得多的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当使用Tensorflow(Tensorflow 2)内置的Keras时,我注意到使用Tensorflow内嵌的Keras的predict()函数和独立Keras的predict()时,计算时间大大增加.请参见下面的玩具代码:

When using the Keras that comes embedded with Tensorflow (Tensorflow 2), I noticed a severe increase in computational time when using the predict() function from the Keras embedded inside Tensorflow and the predict() from standalone Keras. See the toy code below:

import tensorflow
import keras
import numpy as np
import time

test = np.array([[0.1, 0.1, 0.1, 0.1, 0.1, 0.5, 0.1, 0., 0.1, 0.2]])

# Keras from inside Tensorflow
model_1 = tensorflow.keras.Sequential([
  tensorflow.keras.layers.Dense(1, activation='relu', input_shape=(10,)),
])

start_1 = time.time()
for i in range(1000):
    result = model_1.predict(test)
elapsed_time_1 = time.time() - start_1

# Standalone Keras
model_2 = keras.models.Sequential([
  keras.layers.Dense(1, activation='relu', input_shape=(10,)),
]) 

start_2 = time.time()
for i in range(1000):
    result = model_2.predict(test)
elapsed_time_2 = time.time() - start_2

print(elapsed_time_1)
print(elapsed_time_2)

我机器下面代码的输出是

The output from the code below in my machine is

17.82757878303528
0.31248927116394043

与独立Keras的predict()相比,预期的结果是tensorflow.keraspredict()应该花费相同的时间来完成同一任务.

The expected output is that the predict() from tensorflow.keras should take the same amount of time for the same task, when compared to the predict() from standalone Keras.

我的问题是:

  1. 为什么会这样?
  2. 我该如何解决?

我的规格

Python版本:Python 3.6.8

Python version: Python 3.6.8

Keras版本:2.3.1

Keras version: 2.3.1

Tensorflow版本:2.1.0

Tensorflow version: 2.1.0

在Windows 10上运行

Running on Windows 10

推荐答案

主要是由于渴望执行.您可以使用

It's mostly due to eager execution. You can turn off eager execution with

tensorflow.compat.v1.disable_eager_execution()

这样做,tf.keras仍然要慢2倍左右,我不确定为什么,但幅度不大.如果您事先转换为张量,它们两个的运行速度都会更快.

Doing that, the tf.keras is still ~2x slower, which I'm not sure why, but not by orders of magnitude. Both of them go even faster if you convert to tensors beforehand.

这篇关于tf.keras.predict()比Keras的预测()慢得多的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆