简体   繁体   English

Tensorflow tf.matmul示例不正确?

[英]Tensorflow tf.matmul example is incorrect?

I read the official document for tf.matmul and I understand the first example. 我阅读了tf.matmul的官方文档,我理解了第一个例子。 It is a simple [2,3] x [3,2] operation: 这是一个简单的[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]]

However, the second example seems very strange : 但是,第二个例子似乎很奇怪:

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

Why the matrix with shape [2,2,3] is allowed to multiply with [2,3,2] ? 为什么允许形状[2,2,3]的矩阵乘以[2,3,2]?

From the same page ( https://web.archive.org/web/20170223153510/https://www.tensorflow.org/api_docs/python/tf/matmul ): 从同一页面( https://web.archive.org/web/20170223153510/https://www.tensorflow.org/api_docs/python/tf/matmul ):

Returns: A Tensor of the same type as a and b where each inner-most matrix is the product of the corresponding matrices in a and b , eg if all transpose or adjoint attributes are False : 返回:与ab相同类型的Tensor ,其中每个最内部矩阵是ab相应矩阵的乘积,例如,如果所有转置或伴随属性都为False
output [..., i, j] = sum_k ( a [..., i, k] * b [..., k, j]), for all indices i, j. output [...,i,j] = sum_k( a [...,i,k] * b [...,k,j]),对于所有索引i,j。

Hence a matrix with shape [2,2,3] is allowed to multiply with [2,3,2]. 因此,允许形状[2,2,3]的矩阵与[2,3,2]相乘。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM