python的matmul_TensorFlow:tf.matmul函数

函数:tf.matmulmatmul(

a,

b,

transpose_a=False,

transpose_b=False,

adjoint_a=False,

adjoint_b=False,

a_is_sparse=False,

b_is_sparse=False,

name=None

)

将矩阵 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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值