张量流中的矩阵乘法方法和函数有什么区别?

what is the difference between matrix multiplication methods and functions in tensorflow?

tensorflow中两个矩阵相乘的这三种方式有什么区别?三种方式是:

  1. @
  2. tf.tensordot()
  3. tf.matmul()

我测试了它们,它们给出了相同的结果。但我想知道是否存在任何潜在差异。

让我们通过下面的例子来理解这一点,我用了两个 matrix a, b 来执行这些功能:

import tensorflow as tf

a = tf.constant([[1, 2],
                 [3, 4]])
b = tf.constant([[1, 1],
                 [1, 1]]) # or `tf.ones([2,2])`

tf.matmul(a,b)(a @ b) - 都执行 matrix mutiplication

print(tf.matmul(a, b), "\n")   # matrix - multiplication

输出:

tf.Tensor(
[[3 3]
 [7 7]], shape=(2, 2), dtype=int32) 

对于相同的矩阵,您也可以在此处看到相同的输出:

print(a @ b, "\n") # @ used as matrix_multiplication operator

输出:

tf.Tensor(
[[3 3]
 [7 7]], shape=(2, 2), dtype=int32) 

tf.tensordot() - Tensordot(也称为张量收缩)对 a 和 b 中的元素在轴指定的索引上的乘积求和。

如果我们取 axes=0(标量,无轴):

print(tf.tensordot(a, b, axes=0), "\n")
#One by one each element(scalar) of first matrix multiply with all element of second matrix and keeps output in separate matrix for each element multiplication.

输出:

tf.Tensor(
[[[[1 1]
   [1 1]]

  [[2 2]
   [2 2]]]


 [[[3 3]
   [3 3]]

  [[4 4]
   [4 4]]]], shape=(2, 2, 2, 2), dtype=int32) 

如果我们改变 axes=1:

print(tf.tensordot(a, b, axes=1), "\n")  
# performs matrix-multiplication 

输出:

tf.Tensor(
[[3 3]
 [7 7]], shape=(2, 2), dtype=int32) 

axes=2:

print(tf.tensordot(a, b, axes=2), "\n") 
# performs element-wise multiplication,sums the result into scalar.

输出:

tf.Tensor(10, shape=(), dtype=int32) 

您可以在给定链接中探索有关 tf.tensordot() and basic details on axes 的更多信息。