gpt4 book ai didi

python - Tensorflow tf.matmul 示例不正确?

转载 作者:行者123 更新时间:2023-11-28 17:19:16 25 4
gpt4 key购买 nike

我阅读了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):

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, e.g. if all transpose or adjoint attributes are False:
output[..., i, j] = sum_k (a[..., i, k] * b[..., k, j]), for all indices i, j.

因此,形状为 [2,2,3] 的矩阵可以与 [2,3,2] 相乘。

关于python - Tensorflow tf.matmul 示例不正确?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42419837/

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