codecamp

TensorFlow:tf.matmul函数

函数:tf.matmul
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
)

定义在:tensorflow/python/ops/math_ops.py.

参考指南:数学函数>矩阵数学函数

将矩阵 a 乘以矩阵 b,生成a * b

输入必须在任何转换之后是 rank> = 2 的张量,其中内部 2 维度指定有效的矩阵乘法参数,并且任何其他外部维度匹配.

两个矩阵必须是相同类型.支持的类型有:float16,float32,float64,int32,complex64,complex128.

通过将相应的标志之一设置为 True,矩阵可以被转置或 adjointed(共轭和转置).默认情况下,这些都是 False.

如果一个或两个矩阵包含很多的零,则可以通过将相应的 a_is_sparse 或 b_is_sparse 标志设置为 True 来使用更有效的乘法算法,默认为 false.这个优化仅适用于具有数据类型为bfloat16 或 float32 的纯矩阵(rank 为2的张量).

例如:

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

# Since python >= 3.5 the @ operator is supported (see PEP 465).
# In TensorFlow, it simply calls the `tf.matmul()` function, so the
# following lines are equivalent:
d = a @ b @ [[10.], [11.]]
d = tf.matmul(tf.matmul(a, b), [[10.], [11.]])

参数:

  • a:类型为 float16,float32,float64,int32,complex64,complex128 和 rank > 1的张量.
  • b:与 a 具有相同类型和 rank.
  • transpose_a:如果 True,a 在乘法之前转置.
  • transpose_b:如果 True,b 在乘法之前转置.
  • adjoint_a:如果 True,a 在乘法之前共轭和转置.
  • adjoint_b:如果 True,b 在乘法之前共轭和转置.
  • a_is_sparse:如果 True,a 被视为稀疏矩阵.
  • b_is_sparse:如果 True,b 被视为稀疏矩阵.
  • name:操作名称(可选).

返回:

该函数返回与 a 和 b 具有相同类型的张量,其中每个最内矩阵是 a 和 b 中对应矩阵的乘积,例如,如果所有转置或伴随的属性为 False:

output[..., i, j] = sum_k (a[..., i, k] * b[..., k, j]), for all indices i, j

Note:这是矩阵乘积,而不是元素的乘积.

可能引发的异常:

  • ValueError:如果 transpose_a 和 adjoint_a,或者 transpose_b 和 adjoint_b 都设置为 True.
TensorFlow:tf.matching_files函数
TensorFlow矩阵数学函数:tf.matrix_band_part
温馨提示
下载编程狮App,免费阅读超1000+编程语言教程
取消
确定
目录

TensorFlow 函数介绍

TensorFlow 函数模块:tf

TensorFlow的image模块

TensorFlow使用之tf.io

TensorFlow使用之tf.keras

TensorFlow函数教程:tf.keras.applications

TensorFlow函数教程:tf.keras.backend

TensorFlow使用之tf.metrics

TensorFlow使用之tf.nn

TensorFlow使用之tf.python_io

TensorFlow 功能函数

关闭

MIP.setData({ 'pageTheme' : getCookie('pageTheme') || {'day':true, 'night':false}, 'pageFontSize' : getCookie('pageFontSize') || 20 }); MIP.watch('pageTheme', function(newValue){ setCookie('pageTheme', JSON.stringify(newValue)) }); MIP.watch('pageFontSize', function(newValue){ setCookie('pageFontSize', newValue) }); function setCookie(name, value){ var days = 1; var exp = new Date(); exp.setTime(exp.getTime() + days*24*60*60*1000); document.cookie = name + '=' + value + ';expires=' + exp.toUTCString(); } function getCookie(name){ var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)'); return document.cookie.match(reg) ? JSON.parse(document.cookie.match(reg)[2]) : null; }