tf.matmul函數用法
函數:tf.matmul
表示:將矩陣 a 乘以矩陣 b,生成a * b
matmul(
a,
b,
transpose_a=False,
transpose_b=False,
adjoint_a=False,
adjoint_b=False,
a_is_sparse=False,
b_is_sparse=False,
name=None
)
# 2-D tensor `a`
a = tf.constant([1, 2, 3, 4, 5, 6], shape=[2, 3]) => [[1. 2. 3.]
[4. 5. 6.]]
# 2-D tensor `b`
b = tf.constant([7, 8, 9, 10, 11, 12], shape=[3, 2]) => [[7. 8.]
[9. 10.]
[11. 12.]]
c = tf.matmul(a, b) => [[58 64]
[139 154]]
# 3-D tensor `a`
a = tf.constant(np.arange(1, 13, dtype=np.int32),
shape=[2, 2, 3]) => [[[ 1. 2. 3.]
[ 4. 5. 6.]],
[[ 7. 8. 9.]
[10. 11. 12.]]]
# 3-D tensor `b`
b = tf.constant(np.arange(13, 25, dtype=np.int32),
shape=[2, 3, 2]) => [[[13. 14.]
[15. 16.]
[17. 18.]],
[[19. 20.]
[21. 22.]
[23. 24.]]]
c = tf.matmul(a, b) => [[[ 94 100]
[229 244]],
[[508 532]
[697 730]]]
transpose_a:如果 True,a 在乘法之前轉置.
transpose_b:如果 True,b 在乘法之前轉置.
1、如果transpose_b參數設置爲False,那麼x的最後一維要跟y的倒數第二維相等,即:
tf.matmul(x, y, transpose_b = False)
x.shape = [..., x1, x2]
y.shape = [..., y1, y2]
x2 = y1
2、如果transpose_b參數設置爲True,那麼x的最後一維要跟y的最後一維相等,transpose_b=True只是把第二個矩陣(這裏就是y)的最後兩維換了下,即:
tf.matmul(x, y, transpose_b = True)
x.shape = [..., x1, x2]
y.shape = [..., y1, y2]
x2 = y2