CPU/GPU之间的张量流切换

don*_*lan 33 tensorflow

安装了tensorflow GPU(运行在一个可怜的NVIDIA GeForce 950上),我想比较性能和CPU.

我正在运行tensorFlow MNIST教程代码,并注意到速度的急剧增加 - 估计无论如何(我在笔记本电脑i7上运行了CPU版本,批量大小为100,这是在桌面GPU上,批量大小10) - 当我切换时CPU和GPU之间...但我只注意到当我将GPU上的批量大小从100降低到10时速度增加...

现在我缺乏客观的衡量标准.

有没有办法在CPU和GPU张量流之间切换?

Yar*_*tov 30

使GPU不可见

export CUDA_VISIBLE_DEVICES=""
Run Code Online (Sandbox Code Playgroud)

恢复正常

unset CUDA_VISIBLE_DEVICES
Run Code Online (Sandbox Code Playgroud)

  • 在哪里放这个? (8认同)
  • ..或`CUDA_VISIBLE_DEVICES="" python myscript.py` (2认同)

use*_*101 14

尝试将tf.device设置为cpu:0

with tf.Session() as sess:
     with tf.device("/cpu:0"):
Run Code Online (Sandbox Code Playgroud)


Dav*_*Liu 7

另一种选择是在两个虚拟环境中安装 tensorflow 的 cpu 版本和 gpu 版本,这里列出了如何在虚拟环境中安装 tensorflow 的详细说明https://www.tensorflow.org/get_started/os_setup;这样,您可以在两个终端窗口中运行相同的代码,一个使用 CPU,另一个使用 GPU。


DSB*_*BLR 6

# 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))    
Run Code Online (Sandbox Code Playgroud)


lur*_*x66 6

已经过去了相当长的一段时间。最新版本的 Tensorflow(至少从 2.0 起)不需要安装具有不具有GPU 支持的两个版本,因此您可以启动两个单独的jupyter-notebook实例。遵循@Yaroslav 的建议:

$ CUDA_VISIBLE_DEVICES="" jupyter-notebook &
$ jupyter-notebook &
Run Code Online (Sandbox Code Playgroud)

然后,您将在浏览器中打开两个单独的 jupyter 客户端,通常为http://localhost:8888/http://localhost:8889/,分别不带 GPU 支持和带 GPU 支持,您可以在其中运行相同的 .ipynb 笔记本并测量性能差异。


The*_*Way 6

要关闭 GPU,只需将其添加到脚本顶部即可。

import os
os.environ["CUDA_VISIBLE_DEVICES"] = "-1"
Run Code Online (Sandbox Code Playgroud)

(想再次使用GPU时将其注释掉)