在 tensorflow 2 中导入 tensorflow 模块很慢

Ray*_*yek 10 python-import python-3.x tensorflow2.0

相关: 导入 TensorFlow contrib 模块在 TensorFlow 1.2.1 中很慢什么会导致 TensorFlow 导入如此缓慢?

我正在使用 ssd 并导入 TensorFlow。我有 4 ghz 8 核 pc 和 16 gb ram(处理器 AMD FX(tm)-8350 八核处理器,4000 Mhz,4 核,8 逻辑处理器)。TensorFlow 需要 10-12 秒才能导入。

有什么办法可以选择性地导入部分 TensorFlow?

RAM 磁盘有帮助吗?

是否有更多的工作在做这样的事情或者:用 Python 3 导入 tensorflow.contrib 因为inspect.stack 很慢#11829 很慢

编辑:Python 3.6.8 :: Anaconda, Inc. on windoze 8.1。Dos 框,cygwin bash 慢 12 秒。vs code bash/power shell 最快 8 秒。在 .py 文件中导入,例如: import tensorflow as tf. 不确定是什么环境。

编辑2:

PS D:\ray\dev\ml\ss1> conda info --envs
# conda environments:
#
base                  *  D:\Anaconda3
first                    D:\Anaconda3\envs\first
                         d:\Anaconda3
first                    d:\Anaconda3\envs\first
Run Code Online (Sandbox Code Playgroud)

编辑 3:使用下面的代码,我在命令提示符中得到 9-10 秒:

(tf2) D:\ray\dev\ml\ss1>python timeimport.py
 import tensorflow: 1 units, 9.796 seconds. 0.1 units/second.
version: 2.0.0

(tf2) D:\ray\dev\ml\ss1>python timeimport.py
 import tensorflow: 1 units, 9.448 seconds. 0.11 units/second.
version: 2.0.0

(tf2) D:\ray\dev\ml\ss1>python timeimport.py
 import tensorflow: 1 units, 9.421 seconds. 0.11 units/second.
version: 2.0.0


from __future__ import absolute_import, division, print_function, unicode_literals
from contextlib import contextmanager
from timeit import default_timer as timer
@contextmanager
def timing(description: str,units=1,title="",before="") -> None:
    if before!="":
        print(before,flush=True)
    start = timer()
    yield
    dt = timer() - start
    frequency=0 if units is None else (units/dt)
    if units is None:
        if title is None: print(f"{description}: {dt} seconds.",flush=True)
        else: print(f"{title} {description}: {dt} seconds.",flush=True)
    else: 
        #"{0:.2f}".format(a)
        dt=round(dt,3)
        frequency=round(frequency,2)
        print(f"{title} {description}: {str(units)} units, {dt} seconds. {str(frequency)} units/second.",flush=True) 
    return dt
with timing("import tensorflow",1):
    import tensorflow as tf
print("version:",tf.__version__)
Run Code Online (Sandbox Code Playgroud)

编辑 4:转动 windows degender,我得到 8-9 秒而不是 9-10 秒。

编辑 5:我找到了一个解决方法:

制作一个笔记本:

import tensorflow as tf
print(tf.__version__)
import tensorflow_datasets as tfds
import code.py
Run Code Online (Sandbox Code Playgroud)

然后在你的 code.py 中:

print("enter imported code")
import tensorflow as tf
print(tf.__version__)

# !pip install -q tensorflow-datasets
import tensorflow_datasets as tfds
import matplotlib.pyplot as plt
import numpy as np

#tfds.list_builders()
ds = tfds.load('mnist', split='train', shuffle_files=True)
...
Run Code Online (Sandbox Code Playgroud)

所以你运行笔记本一次,它需要 10 秒。下次风一样的。

小智 0

首先我想说的是,我使用的是 3 Ghz 四核,在 Python 中导入 TensorFlow 不需要花费近 10 秒的时间。您能否详细说明您在导入时遇到问题的环境(即终端/控制台/命令提示符/Anaconda 中的 Windows/Mac/Linux 等)?您没有指定如何尝试导入 Tensorflow,但考虑到您使用 python-3.x 标记它,我假设您正在使用 Python 导入 Tensorflow。我确信这不会是一个受欢迎的答案,但也许可以考虑将 Tensorflow 与 C++ 等编译语言一起使用。众所周知,Python 等解释语言比编译语言慢得多,如果速度至关重要,那么在其本机语言中使用 TensorFlow 似乎是显而易见的。