gpt4 book ai didi

python - 为什么 tensorflow 中的随机数生成器 tf.random_uniform 比 numpy 等效项快得多

转载 作者:太空狗 更新时间:2023-10-29 20:55:14 26 4
gpt4 key购买 nike

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

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))

所有三个段以 double 生成一个均匀随机的 1000*2000 矩阵 400 次。时间差异是惊人的。在我的 Mac 上,

np.random.uniform: 10.4318959713 seconds
np.random.random: 8.76161003113 seconds
tf.random_uniform: 1.21312117577 seconds

为什么 tensorflow 比 numpy 快得多?

最佳答案

tf.random_uniform 在这种情况下返回一个未评估 张量类型,tensorflow.python.framework.ops.Tensor,如果您在 tf.random_uniform 情况下设置一个 session 上下文来评估 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

关于python - 为什么 tensorflow 中的随机数生成器 tf.random_uniform 比 numpy 等效项快得多,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53054699/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com