python - 导入 tflearn 非常慢

标签 python tensorflow tflearn

与其他模块相比,导入 tflearn 需要花费大量时间。

在新创建的虚拟环境中,仅使用 pip 安装了 tflearn 和最少的依赖项:

  • 导入 numpy as np 需要约 0.18 秒
  • 将tensorflow导入为tf大约需要0.90s
  • import tflearn 需要大约 3.1 秒(并要求我安装 h5py 和 scipy)。

按照建议安装 h5py 和 scipy 后,import tflearn 消耗的时间约为 3.5 秒。如何减少导入时间?

(上面的所有测试都是通过将语句放入 test.py 并运行 time python test.py 几次来完成的。报告的时间是 time 报告的“真实”时间 bash 内置。)

最佳答案

简短回答

在 Jupyter 或其他具有长期解释器 session 的工具中完成初始工作,因此您无需为导入等待太多时间。

长答案

您可以使用python -v来跟踪导入。我在原始的 python:3.6-stretch 中从 apt 安装了 moreutils,从 pip 安装了 tensorflowtflearn Docker 容器...

root@10e4bcd91377:/# python -v -c 'import tensorflow' 2>&1 | ts '%H:%M:%.S' | grep 'import ' | wc -l
954
root@10e4bcd91377:/# python -v -c 'import tflearn' 2>&1 | ts '%H:%M:%.S' | grep 'import ' | wc -l
1768

很明显,导入 tflearn 会导入大量包。 哪些?

# python -v -c 'import tflearn' 2>&1 | grep 'import ' | cut -f1 -d# | sort | uniq > tflearn-imports.txt
# python -v -c 'import tensorflow' 2>&1 | grep 'import ' | cut -f1 -d# | sort | uniq > tensorflow-imports.txt
# diff --suppress-common-lines tensorflow-imports.txt tflearn-imports.txt

我会保留 831 行输出,但看起来 tflearn 导入了所有 tensorflow.contrib,这需要相当长的时间,而且不是什么东西导入tensorflow本身就可以。有了这些信息,我们可以查看原始的 python -v -c 'import tflearn' 2>&1 输出 - 它看起来像 tflearn.variables 是导入 tensorflow.contrib...

# <snip>
import 'tensorflow.contrib.summary.summary'
import 'tensorflow.contrib'
import 'tflearn.variables'
import 'tflearn.config'
# <snip>

难道是this from tensorflow.contrib.framework.python.ops import add_arg_scope as contrib_add_arg_scope statement ?让我们来看看...

# time python -v -c 'from tensorflow.contrib.framework.python.ops import add_arg_scope as contrib_add_arg_scope' 2>&1 | grep 'import ' | wc -l
1727

real    0m4.010s
user    0m3.820s
sys 0m0.410s
root@10e4bcd91377:/#

哎呀,看起来像这样!由于 Python 导入的工作方式,导入子模块必须评估整个包,并且由于 tensorflow.contrib 不使用 Tensorflow's Python lazy loader (which does mention contrib) ,需要一段时间。

(这里曾经在模块中讨论过供应商,但这无关紧要,因为:)

不幸的是,tflearn 中的其他地方也从 contrib 导入零碎内容,因此消除这种依赖关系不会有太大帮助。

关于python - 导入 tflearn 非常慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51061689/

相关文章:

python - 从列表中删除字典的子集

python - 有没有办法使用 Motor 定义 MongoDB 模式?

tensorflow - TensorFlow:打开由SummaryWriter编写的日志数据

python - 将 Facenet 模型 .pb 文件转换为 TFLITE 格式时出错

python-2.7 - 无效参数错误 : You must feed a value for placeholder tensor 'input_1/X' with dtype float

python - 余弦相似度的 tflearn 自定义损失函数

python - 使用本地依赖项用诗歌构建可安装的 tar.gz/whl

python - 按月份和年份对 pandas 数据框系列进行排序?

c - Tensorflow:TF_SessionRun 返回 TF_INVALID_ARGUMENT

python - 将列表转换为 n 维数组以馈送到 TFlearn