我是第一次尝试GPU计算,当然希望能够大幅度提高速度。但是,使用tensorflow中的一个基本示例实际上更糟:
在cpu:0上,十次运行中的每一次平均花费2秒,gpu:0花费2.7秒,而gpu:1比cpu:0差3秒,即降低50%。
这是代码:
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。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句