Tensorflow 在 CPU/GPU 之间切换
Tensor flow toggle between CPU/GPU
安装了 tensorflow GPU(运行 在一个可怜的 NVIDIA GeForce 950 上),我想与 CPU 比较性能。
我是 运行 tensorFlow MNIST 教程代码,并且注意到速度有了显着提高——无论如何估计(我 运行 2 天前在笔记本电脑上的 CPU 版本i7 的批量大小为 100,而这在台式机 GPU 上,批量大小为 10)——在 CPU 和我切换时的 GPU 之间......但我只注意到当我降低批量时速度增加GPU 上的大小从 100...
现在我缺少 objective 衡量我所获得的东西的方法。
有没有办法在 CPU 和 GPU 张量流之间切换?
让 GPU 不可见
export CUDA_VISIBLE_DEVICES=""
到return到正常
unset CUDA_VISIBLE_DEVICES
尝试将 tf.device 设置为 cpu:0
with tf.Session() as sess:
with tf.device("/cpu:0"):
另一种选择是在两个虚拟环境中安装cpu版本和gpu版本的tensorflow,这里列出了如何在虚拟环境中安装tensorflow的详细说明https://www.tensorflow.org/get_started/os_setup;这样,您可以在两个终端 windows 中使用相同的代码 运行,一个使用 CPU,另一个使用 GPU。
# Check if the server/ instance is having GPU/ CPU from python code
import sys
import tensorflow as tf
from tensorflow.python.client import device_lib
# device_lib.list_local_devices() ## this command list all the processing device GPU and CPU
device_name = [x.name for x in device_lib.list_local_devices() if x.device_type == 'GPU']
if device_name[0] == "/device:GPU:0":
device_name = "/gpu:0"
#print('GPU')
else:
#print('CPU')
device_name = "/cpu:0"
with tf.device(device_name):
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)
with tf.Session() as sess:
print (sess.run(c))
好久不见了。 Tensorflow 的最新版本(至少从 2.0 开始)不需要安装两个版本 with 和 without gpu 支持,因此您可以启动两个单独的版本jupyter-notebook
个实例。按照@Yaroslav 的建议:
$ CUDA_VISIBLE_DEVICES="" jupyter-notebook &
$ jupyter-notebook &
然后您将在浏览器中打开两个单独的 jupyter 客户端,通常是 http://localhost:8888/
和 http://localhost:8889/
,分别没有和有 GPU 支持,您可以在其中 运行 相同的 .ipynb笔记本并测量性能差异。
要关闭 GPU,只需将其添加到脚本顶部即可。
import os
os.environ["CUDA_VISIBLE_DEVICES"] = "-1"
(想再次使用GPU时注释掉)
安装了 tensorflow GPU(运行 在一个可怜的 NVIDIA GeForce 950 上),我想与 CPU 比较性能。
我是 运行 tensorFlow MNIST 教程代码,并且注意到速度有了显着提高——无论如何估计(我 运行 2 天前在笔记本电脑上的 CPU 版本i7 的批量大小为 100,而这在台式机 GPU 上,批量大小为 10)——在 CPU 和我切换时的 GPU 之间......但我只注意到当我降低批量时速度增加GPU 上的大小从 100...
现在我缺少 objective 衡量我所获得的东西的方法。
有没有办法在 CPU 和 GPU 张量流之间切换?
让 GPU 不可见
export CUDA_VISIBLE_DEVICES=""
到return到正常
unset CUDA_VISIBLE_DEVICES
尝试将 tf.device 设置为 cpu:0
with tf.Session() as sess:
with tf.device("/cpu:0"):
另一种选择是在两个虚拟环境中安装cpu版本和gpu版本的tensorflow,这里列出了如何在虚拟环境中安装tensorflow的详细说明https://www.tensorflow.org/get_started/os_setup;这样,您可以在两个终端 windows 中使用相同的代码 运行,一个使用 CPU,另一个使用 GPU。
# Check if the server/ instance is having GPU/ CPU from python code
import sys
import tensorflow as tf
from tensorflow.python.client import device_lib
# device_lib.list_local_devices() ## this command list all the processing device GPU and CPU
device_name = [x.name for x in device_lib.list_local_devices() if x.device_type == 'GPU']
if device_name[0] == "/device:GPU:0":
device_name = "/gpu:0"
#print('GPU')
else:
#print('CPU')
device_name = "/cpu:0"
with tf.device(device_name):
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)
with tf.Session() as sess:
print (sess.run(c))
好久不见了。 Tensorflow 的最新版本(至少从 2.0 开始)不需要安装两个版本 with 和 without gpu 支持,因此您可以启动两个单独的版本jupyter-notebook
个实例。按照@Yaroslav 的建议:
$ CUDA_VISIBLE_DEVICES="" jupyter-notebook &
$ jupyter-notebook &
然后您将在浏览器中打开两个单独的 jupyter 客户端,通常是 http://localhost:8888/
和 http://localhost:8889/
,分别没有和有 GPU 支持,您可以在其中 运行 相同的 .ipynb笔记本并测量性能差异。
要关闭 GPU,只需将其添加到脚本顶部即可。
import os
os.environ["CUDA_VISIBLE_DEVICES"] = "-1"
(想再次使用GPU时注释掉)