[英]Tensorflow tf.matmul example is incorrect?
我阅读了tf.matmul的官方文档,我理解了第一个例子。 这是一个简单的[2,3] x [3,2]操作:
a = tf.constant([1, 2, 3, 4, 5, 6], shape=[2, 3])
b = tf.constant([7, 8, 9, 10, 11, 12], shape=[3, 2])
c = tf.matmul(a, b) => [[58 64]
[139 154]]
但是,第二个例子似乎很奇怪:
a = tf.constant(np.arange(1, 13, dtype=np.int32),
shape=[2, 2, 3])
b = tf.constant(np.arange(13, 25, dtype=np.int32),
shape=[2, 3, 2])
c = tf.matmul(a, b) => [[[ 94 100]
[229 244]],
[[508 532]
[697 730]]]
为什么允许形状[2,2,3]的矩阵乘以[2,3,2]?
从同一页面( https://web.archive.org/web/20170223153510/https://www.tensorflow.org/api_docs/python/tf/matmul ):
返回:与
a
和b
相同类型的Tensor
,其中每个最内部矩阵是a
和b
相应矩阵的乘积,例如,如果所有转置或伴随属性都为False
:
output
[...,i,j] = sum_k(a
[...,i,k] *b
[...,k,j]),对于所有索引i,j。
因此,允许形状[2,2,3]的矩阵与[2,3,2]相乘。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.