比对 CPU 和 GPU 矩阵乘法速度

2018-09-25  本文已影响177人  朱小虎XiaohuZhu
In [11]: def time_matmul(x):
    ...:     %timeit tf.matmul(x, x)
    ...:     

In [12]: with tf.device("CPU:0"):
    ...:     x = tf.random_uniform([1000, 1000])
    ...:     assert x.device.endswith("CPU:0")
    ...:     time_matmul(x)
    ...:     
The slowest run took 4.25 times longer than the fastest. This could mean that an intermediate result is being cached.
100 loops, best of 3: 8.05 ms per loop

In [13]: with tf.device("GPU:0"):
    ...:     x = tf.random_uniform([1000,1000])
    ...:     assert  x.device.endswith("GPU:0")
    ...:     time_matmul(x)
    ...:     
The slowest run took 448.91 times longer than the fastest. This could mean that an intermediate result is being cached.
10000 loops, best of 3: 222 µs per loop

上一篇 下一篇

猜你喜欢

热点阅读