我是第一次尝试 GPU 计算,当然希望能有很大的加速。然而,对于张量流中的基本示例,情况实际上更糟:
在 cpu:0 上,十次运行中的每一次平均需要 2 秒,gpu:0 需要 2.7 秒,gpu:1 比 cpu:0 差 50%,需要 3 秒。
这是代码:
import tensorflow as tf
import numpy as np
import time
import random
for _ in range(10):
with tf.Session() as sess:
start = time.time()
with tf.device('/gpu:0'): # swap for 'cpu:0' or whatever
a = tf.constant([random.random() for _ in xrange(1000 *1000)], shape=[1000, 1000], name='a')
b = tf.constant([random.random() for _ in xrange(1000 *1000)], shape=[1000, 1000], name='b')
c = tf.matmul(a, b)
d = tf.matmul(a, c)
e = tf.matmul(a, d)
f = tf.matmul(a, e)
for _ in range(1000):
sess.run(f)
end = time.time()
print(end - start)
我在这里观察到什么?运行时可能主要是在 RAM 和 GPU 之间复制数据吗?
你用来生成数据的方式是在CPU上执行的(random.random()
是一个常规的 python 函数,而不是 TF-one)。另外,执行它10^6
时间会比请求的慢10^6
一次运行中的随机数。
将代码更改为:
a = tf.random_uniform([1000, 1000], name='a')
b = tf.random_uniform([1000, 1000], name='b')
这样数据将在 GPU 上并行生成,并且不会浪费时间将其从 RAM 传输到 GPU。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)