CPU/GPU 之间的张量流切换 [英] Tensor flow toggle between CPU/GPU
问题描述
安装了 tensorflow GPU(在微不足道的 NVIDIA GeForce 950 上运行)后,我想将性能与 CPU 进行比较.
Having installed tensorflow GPU (running on a measly NVIDIA GeForce 950), I would like to compare performance with the CPU.
我正在运行 tensorFlow MNIST 教程代码,并注意到速度的显着提高 - 无论如何估计(我两天前在笔记本电脑 i7 上运行 CPU 版本,批量大小为 100,这是在桌面 GPU 上运行的),批量大小为 10)--切换时在 CPU 和 GPU 之间...但我只注意到当我将 GPU 上的批量大小从 100 降低到 10 时速度增加...
I am running the tensorFlow MNIST tutorial code, and have noticed a dramatic increase in speed--estimated anyways (I ran the CPU version 2 days ago on a laptop i7 with a batch size of 100, and this on a desktop GPU, batch size of 10)--between the CPU and the GPU when I switched...but I only noticed the speed increase when I lowered the batch size on the GPU to 10 from 100...
现在我缺乏客观衡量我所获得的东西.
Now I lack an objective measure for what I am gaining.
有没有办法在 CPU 和 GPU 张量流之间切换?
Is there a way to toggle between the CPU and GPU tensor flows?
推荐答案
# 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))
这篇关于CPU/GPU 之间的张量流切换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!