问题描述
相关:导入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 秒才能导入.
I am using an ssd and importing TensorFlow. I have 4 ghz 8 core pc with 16 gb ram (Processor AMD FX(tm)-8350 Eight-Core Processor, 4000 Mhz, 4 Core(s), 8 Logical Processor(s)). TensorFlow takes 10-12 seconds to import.
有没有办法选择性地导入部分 TensorFlow?
Is there any way to selectively import parts of TensorFlow?
RAM 磁盘有帮助吗?
Would a RAM disk help?
是否还有其他类似的工作要做:导入 tensorflow.contrib 很慢使用 Python 3,因为 inspect.stack 很慢 #11829?
Is there any more work being done on stuff like this or: Slow to import tensorflow.contrib with Python 3 because inspect.stack is slow #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.不确定环境是什么.
Python 3.6.8 :: Anaconda, Inc. on windoze 8.1.Dos box, cygwin bash are slow at 12 seconds.Vs code bash/power shell is fastest at 8 seconds.Importing in a .py file like: import tensorflow as tf.Not sure what environments are.
编辑 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
编辑 3:使用下面的代码,我在命令提示符中得到 9-10 秒:
Edit 3: Using the code below, i get 9-10 seconds in a command prompt:
(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__)
编辑 4:关闭窗口 degender,我得到 8-9 秒而不是 9-10 秒.
Edit 4: turning of windows degender, i get 8-9 seconds instead of 9-10 seconds.
编辑 5:我找到了一个解决方法:
Edit 5: i found a workaround:
制作笔记本:
import tensorflow as tf
print(tf.__version__)
import tensorflow_datasets as tfds
import code.py
然后在您的 code.py 中:
then in your 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)
...
所以你运行笔记本一次,它需要 10 秒.下次风一样的.
so you run the notebook once and it takes 10 seconds. next time it goes like the wind.
推荐答案
当我从办公室的 Thinkpad T560(带有集成 GPU 的 i7 6600U)导入 tensorflow 时,需要 10 多秒(大约 15 秒).但是最近我在家里买了一台新的笔记本电脑 Lenovo Y7000(i7 10750H 和 GTX 1650 卡,SAMSUNG SSD),并且我安装了 CUDA 依赖项并且能够成功运行 TensorFlow 和 CUDA.
When I import tensorflow from a Thinkpad T560(i7 6600U with integrated GPU) in my office, it takes over 10 secs(about 15 secs).But recently I got a new Laptop Lenovo Y7000 at home(i7 10750H and GTX 1650 Card, SAMSUNG SSD), and I installed CUDA dependencies and able to run TensorFlow with CUDA successfully.
我想说现在我觉得我从一辆旧马车换到了子弹头列车.
I would say now I feel like I switch from an old carriage to a bullet train.
这篇关于在 tensorflow 2 中导入 tensorflow 模块很慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!