在下面的python代码中,为什么通过numpy进行乘法的时间远小于via tensorflow?
import tensorflow as tf
import numpy as np
import time
size=10000
x = tf.placeholder(tf.float32, shape=(size, size))
y = tf.matmul(x, x)
with tf.Session() as sess:
rand_array = np.random.rand(size, size)
start_time = time.time()
np.multiply(rand_array,rand_array)
print("--- %s seconds numpy multiply ---" % (time.time() - start_time))
start_time = time.time()
sess.run(y, Feed_dict={x: rand_array})
print("--- %s seconds tensorflow---" % (time.time() - start_time))
输出是
--- 0.22089099884 seconds numpy multiply ---
--- 34.3198359013 seconds tensorflow---
解决方法:
好吧,引用文档:
numpy.multiply(x1, x2[, out]) = Multiply arguments
element-wise.
和
tf.matmul(a, b, transpose_a=False, transpose_b=False,
a_is_sparse=False, b_is_sparse=False, name=None)Multiplies matrix a by matrix b, producing a * b.
The inputs must be two-dimensional matrices, with matching inner
dimensions, possibly after transposition.
这表明您比较不同的操作:O(n ^ 2)逐点乘法与O(n ^ 3)矩阵乘法.我纠正了测试,在两种情况下使用矩阵乘法2次:
import tensorflow as tf
import numpy as np
import time
size=2000
x = tf.placeholder(tf.float32, shape=(size, size))
y = tf.matmul(x, x)
z = tf.matmul(y, x)
with tf.Session() as sess:
rand_array = np.random.rand(size, size)
start_time = time.time()
for _ in xrange(10):
np.dot(np.dot(rand_array,rand_array), rand_array)
print("--- %s seconds numpy multiply ---" % (time.time() - start_time))
start_time = time.time()
for _ in xrange(10):
sess.run(z, Feed_dict={x: rand_array})
print("--- %s seconds tensorflow---" % (time.time() - start_time))
并得到了结果:
--- 2.92911195755 seconds numpy multiply ---
--- 0.32932305336 seconds tensorflow---
使用快速GPU(gtx 1070).
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。