为什么是随机数发生器tf.random_制服在张量流中比numpy等价物快得多

2024-05-04 02:35:20 发布

您现在位置:Python中文网/ 问答频道 /正文

以下代码是我用来测试性能的代码:

import time
import numpy as np
import tensorflow as tf

t = time.time()
for i in range(400):
    a = np.random.uniform(0,1,(1000,2000))
print("np.random.uniform: {} seconds".format(time.time() - t))

t = time.time()
for i in range(400):
    a = np.random.random((1000,2000))
print("np.random.random:  {} seconds".format(time.time() - t))

t = time.time()
for i in range(400):
    a = tf.random_uniform((1000,2000),dtype=tf.float64);
print("tf.random_uniform: {} seconds".format(time.time() - t))

这三个部分都生成一个均匀随机的1000*2000矩阵,精度为400倍。时间上的差异是惊人的。在我的Mac电脑上

^{pr2}$

为什么tensorflow比numpy快得多?在


Tags: 代码inimportnumpyformatfortimetf
2条回答

您刚刚创建了一个计算图,它生成一个输出随机数的操作。对于要计算的值,必须在tf.Session中执行图形。在

// build the graph
a = tf.random_uniform((1000,2000))

// run the graph
with tf.Session() as sess:
    t = time.time()
    for i in range(400):
        computed_rand_values = sess.run(a)
    //print(...)

我没有测试,但我相信计算时间会比以前的结果长

在本例中,tf.random_uniform返回一个未赋值的张量类型,tensorflow.python.framework.ops.Tensor,如果您设置了一个会话上下文,在其中对tf.random_uniform中的a求值,您将看到它也需要一段时间。在

例如,在tf的例子中,我添加了sess.run(在一台只使用CPU的机器上),计算和具体化需要大约16秒,考虑到在输出时封送到numpy数据类型的开销,这是有意义的。在

In [1]: %cpaste
Pasting code; enter ' ' alone on the line to stop or use Ctrl-D.
:import time
import numpy as np
import tensorflow as tf

t = time.time()
for i in range(400):
    a = np.random.uniform(0,1,(1000,2000))
print("np.random.uniform: {} seconds".format(time.time() - t))

t = time.time()
for i in range(400):
    a = np.random.random((1000,2000))
print("np.random.random:  {} seconds".format(time.time() - t))

sess = tf.Session()
t = time.time()
for i in range(400):
    a = sess.run(tf.random_uniform((1000,2000),dtype=tf.float64))
print("tf.random_uniform: {} seconds".format(time.time() - t))::::::::::::::::::
: 
np.random.uniform: 11.066569805145264 seconds
np.random.random:  9.299575090408325 seconds
2018-10-29 18:34:58.612160: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.1 instructions, but these are available on your machine and could speed up CPU computations.
2018-10-29 18:34:58.612191: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.2 instructions, but these are available on your machine and could speed up CPU computations.
2018-10-29 18:34:58.612210: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX instructions, but these are available on your machine and could speed up CPU computations.
tf.random_uniform: 16.619441747665405 seconds

相关问题 更多 >