- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
在下面的python代码中,为什么numpy乘法的时间比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)。
关于python - 为什么 TensorFlow matmul() 比 NumPy multiply() 慢得多?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41244472/
代码: x = tf.constant([1.,2.,3.], shape = (3,2,4)) y = tf.constant([1.,2.,3.], shape = (3,21,4)) tf.ma
有人可以解释一下,TensorFlow 的 eager 模式是如何工作的吗?我正在尝试构建一个简单的回归,如下所示: import tensorflow as tf tfe = tf.contrib.
我对 Tensorflow 很陌生。我已经在搜索相同的问题,但我不明白。有代码。希望你能帮助我。 代码: import tensorflow as tf w1 = tf.Variable(tf.ran
我对使用 * 和 matmul 的两个张量之间的乘法感到困惑。下面是我的代码 import torch torch.manual_seed(7) features = torch.randn((2,
我有 3 个张量 X 形状(1, c, h, w),假设(1, 20, 40, 50) Fx 形状(num, w, N),假设(1000, 50, 10) Fy shape (num, N, h),假
我已经计算了 Fortran 的 MATMUL 函数使用不同乘法大小(32 × 32、64 × 64,...)花费的时间,我对结果有疑问。 这些是结果: SIZE ----- TIME IN SECO
a = [1, 2, 3] b = [10, 10, 10] np.matmul(a, b) 结果是 60。 numpy 如何乘以 (3,) 和 (3,) 维度并返回点积而不是外积(3 * 3)或抛出
我看到许多机器学习教程通过构造两个矩阵、权重矩阵和输入(或激活)矩阵来解释全连接网络,并执行矩阵到矩阵乘法(matmul)以形成线性方程。 我看到的所有示例都将输入作为 matmul 的第一个参数,将
当我在代码中的某行调用 np.matmul 时出现此错误。这是我在解决 python 调试器错误时得到的信息: > /home/marcos/Desktop/Machine_Learning_for_
我想在等级 2 和等级 3 的两个张量之间广播 tf.matmul 运算,其中一个包含“未知”形状的维度(基本上是特定维度中的“无”值) )。 问题是动态尺寸 tf.reshape 和 tf.broa
我尝试在 tensorflow 中编写和(逻辑运算),有两个输入和两个权重将它们相乘得到一个数字并将这个数字加到偏差中,我在 matmul 中的问题是发送 X(输入)和 W(权重) 以方法形。[[1]
我正在研究并行编程概念并尝试优化单核上的矩阵乘法示例。到目前为止,我想出的最快的实现如下: /* This routine performs a dgemm operation * C := C
我有一些由 input_x 表示的数据。它是一个未知大小的张量(应该批量输入),每个项目的大小为 n。 input_x 经历 tf.nn.embedding_lookup,因此 embed 现在具有维
在 Python 中,@ 运算符传递给元素的 __matmul__ 属性。当实现一个与实际后端无关的方法时,这会派上用场。例如 def inner(x, y): return x @ y
关闭。这个问题是opinion-based .它目前不接受答案。 想要改进这个问题吗? 更新问题,以便 editing this post 提供事实和引用来回答它. 关闭 2 年前。 Improve
我正在尝试使用 tf.matmul() 执行稀疏矩阵乘法。 但是,推理速度比密集矩阵乘法慢得多。 根据 tf.sparse_matmul() 中的描述: 在一个平台上使用此乘法与密集矩阵相乘的盈亏平衡
我有一个我一直在努力解决的问题。与 tf.matmul() 相关并且没有广播。 我在 https://github.com/tensorflow/tensorflow/issues/216 上发现了类
我有许多带有形状的矩阵 w1、w2、w3...wn (k*n1 、k*n2、k*n3...k*nn) 和 x1、x2、x3...xn 具有形状(n1*m、n2*m、n3*m...nn*m >). 我想
我阅读了tf.matmul的官方文档我理解第一个例子。这是一个简单的 [2,3] x [3,2] 操作: a = tf.constant([1, 2, 3, 4, 5, 6], shape=[2, 3
我正在尝试使用 4D-numpy 数组数据在 TensorFlow 中实现多层感知器我在 MatMul 函数上遇到了这个问题。我希望有人能在这里帮助我,非常感谢。 ValueError: Shape
我是一名优秀的程序员,十分优秀!