我在一个共享计算资源的环境中工作,也就是说,我们有一些服务器机器配备了几个Nvidia Titan X GPU.
对于小到中等大小的型号,12GB的Titan X通常足以让2-3人在同一GPU上同时进行训练.如果模型足够小以至于单个模型没有充分利用Titan X的所有计算单元,那么与在另一个训练过程之后运行一个训练过程相比,这实际上可以导致加速.即使在并发访问GPU确实减慢了单个培训时间的情况下,仍然可以灵活地让多个用户同时在GPU上运行.
TensorFlow的问题在于,默认情况下,它在启动时会在GPU上分配全部可用内存.即使对于一个小的2层神经网络,我也看到12 GB的Titan X已用完.
有没有办法让TensorFlow只分配4GB的GPU内存,如果有人知道这个数量对于给定的模型来说足够了?
当我运行keras脚本时,我得到以下输出:
Using TensorFlow backend.
2017-06-14 17:40:44.621761: W
tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow
library wasn't compiled to use SSE4.1 instructions, but these are
available on your machine and could speed up CPU computations.
2017-06-14 17:40:44.621783: W
tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow
library wasn't compiled to use SSE4.2 instructions, but these are
available on your machine and could speed up CPU computations.
2017-06-14 17:40:44.621788: W
tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow
library wasn't compiled to use AVX instructions, but these are
available on your machine and could speed up …Run Code Online (Sandbox Code Playgroud) Google colab在运行时加速器中引入了TPU.我找到了一个例子,如何在官方Tensorflow github中使用TPU .但这个例子并不适用于Google-colaboratory.它停留在以下行:
tf.contrib.tpu.keras_to_tpu_model(model, strategy=strategy)
Run Code Online (Sandbox Code Playgroud)
当我在colab上打印可用设备时,它会返回[]TPU加速器.有谁知道如何在colab上使用TPU?
在tensorflow带有独立keras2.X 的1.X 中,我曾经使用以下代码段在 GPU 上训练和在 CPU 上运行推理(由于某些原因,我的 RNN 模型更快)之间切换:
keras.backend.clear_session()
def set_session(gpus: int = 0):
num_cores = cpu_count()
config = tf.ConfigProto(
intra_op_parallelism_threads=num_cores,
inter_op_parallelism_threads=num_cores,
allow_soft_placement=True,
device_count={"CPU": 1, "GPU": gpus},
)
session = tf.Session(config=config)
k.set_session(session)
Run Code Online (Sandbox Code Playgroud)
此ConfigProto功能在tensorflow2.0 中不再可用(我正在使用集成的tensorflow.keras)。一开始,可以运行tf.config.experimental.set_visible_devices()以禁用 GPU,但任何后续调用都会set_visible_devices导致RuntimeError: Visible devices cannot be modified after being initialized. 有没有办法重新初始化可见设备,或者有没有另一种切换可用设备的方法?
我想为keras / tensorflow提供gpu支持,这就是为什么我安装了tensorflow-gpu的原因。所以我通过pip安装了tensorflow-gpu:
pip install-升级tensorflow-gpu
这导致:
from keras import backend as K
K.tensorflow_backend._get_available_gpus()
> []
Run Code Online (Sandbox Code Playgroud)
然后我找到了这个stackoverflow答案,该答案表明我应该在安装tensorflow-gpu后卸载tensorflow。这导致:
Using TensorFlow backend.
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-1-3d00d838479b> in <module>()
----> 1 from keras import backend as K
2 K.tensorflow_backend._get_available_gpus()
/raid/ntzioras/VirtualEnvironments/DeepLearning/lib/python3.4/site-packages/keras/__init__.py in <module>()
1 from __future__ import absolute_import
2
----> 3 from . import utils
4 from . import activations
5 from . import applications
/raid/ntzioras/VirtualEnvironments/DeepLearning/lib/python3.4/site-packages/keras/utils/__init__.py in <module>()
4 from . import data_utils
5 from . import …Run Code Online (Sandbox Code Playgroud) 我已经成功安装了 tensorflowpip install tensorflow并且一切都按预期工作。
我也可以成功安装 tensorflow-gpupip install tensorflow-gpu但我无法在我的 python 脚本中导入它:
import tensorflow-gpu
File "<stdin>", line 1
import tensorflow-gpu
^
SyntaxError: invalid syntax
Run Code Online (Sandbox Code Playgroud)
我已经安装了 CUDA v9.0 并运行了 Windows 10
我在Windows上运行Keras&Theano的安装(按照本教程).现在我试图将后端切换到Tensorflow,它工作得非常好.
我唯一的问题是,Tensorflow没有检测到我的GPU,而Theano则相反:
from tensorflow.python.client import device_lib
def get_available_gpus():
local_device_protos = device_lib.list_local_devices()
return [x.name for x in local_device_protos if x.device_type == 'GPU']
Run Code Online (Sandbox Code Playgroud)
没有结果但是当与Theano后端一起运行时,它运行得非常好:
C:\Programming\Anaconda3\python.exe D:/cnn_classify_cifar10.py
Using Theano backend.
DEBUG: nvcc STDOUT nvcc warning : The 'compute_20', 'sm_20', and 'sm_21' architectures are deprecated, and may be removed in a future release (Use -Wno-deprecated-gpu-targets to suppress warning).
mod.cu
Creating library C:/Users/Alex/AppData/Local/Theano/compiledir_Windows-10-10.0.14393-SP0-Intel64_Family_6_Model_60_Stepping_3_GenuineIntel-3.5.2-64/tmpgsy496fe/m91973e5c136ea49268a916ff971b7377.lib and object C:/Users/Alex/AppData/Local/Theano/compiledir_Windows-10-10.0.14393-SP0-Intel64_Family_6_Model_60_Stepping_3_GenuineIntel-3.5.2-64/tmpgsy496fe/m91973e5c136ea49268a916ff971b7377.exp
Using gpu device 0: GeForce GTX 770 (CNMeM is enabled with initial size: …Run Code Online (Sandbox Code Playgroud) 我想tensorflow在我的GPU上运行代码,但无法正常工作。我安装了Cuda和cuDNN,并具有兼容的GPU。
我从GPU的官方网站教程中获取了此示例,此处为GPU的Tensorflow教程
# Creates a graph.
a = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2, 3], name='a')
b = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[3, 2], name='b')
c = tf.matmul(a, b)
# Creates a session with log_device_placement set to True.
sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))
# Runs the op.
print(sess.run(c))
Run Code Online (Sandbox Code Playgroud)
这是我的输出:
Device mapping: no known devices.
2017-10-31 16:15:40.298845: I tensorflow/core/common_runtime/direct_session.cc:300] Device mapping:
MatMul: (MatMul): /job:localhost/replica:0/task:0/cpu:0
2017-10-31 16:15:56.895802: I tensorflow/core/common_runtime/simple_placer.cc:872] MatMul: (MatMul)/job:localhost/replica:0/task:0/cpu:0
b: (Const): /job:localhost/replica:0/task:0/cpu:0
2017-10-31 16:15:56.895910: I tensorflow/core/common_runtime/simple_placer.cc:872] …Run Code Online (Sandbox Code Playgroud) 我是否必须自定义为非 GPU Tensorflow 库编写的代码以适合 tensorflow-gpu 库?
我有一个 GPU,想运行仅为非 GPU tensorflow 库编写的 python 代码。我可以简单地安装 tensor-flow gpu 模块并运行代码吗?或者,为了在 GPU 上运行代码,我必须进行任何代码更改吗?
在跨多个服务器和 GPU 训练神经网络时,我想不出ParameterServerStrategy比MultiWorkerMirroredStrategy.
ParameterServerStrategy的主要用例是什么,为什么它比 using 更好MultiWorkerMirroredStrategy?