TensorFlow中矩阵乘操作tf.matmul(或tf.linalg.matmul)和矩阵元素乘tf.multiply(或tf.math.multiply)用法对比

一、环境

TensorFlow API r1.12

CUDA 9.2 V9.2.148

cudnn64_7.dll

Python 3.6.3

Windows 10

二、官方说明

tf.linalg.matmul或tf.matmul:矩阵乘

tf.linalg.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
)

输入:

(1)张量a:支持张量类型float16, float32, float64, int32, complex64,complex128,阶>1

(2)张量b:支持张量类型同张量a

(3)transpose_a:布尔型参数,如果是True,矩阵乘之前先执行转置

(4)transpose_b:布尔型参数,如果是True,矩阵乘之前先执行转置

(5)adjoint_a:布尔型参数,如果是True,矩阵乘之前先执行共轭转置

(6)adjoint_b:布尔型参数,如果是True,矩阵乘之前先执行共轭转置

(7)a_is_sparse:布尔型参数,如果是True,矩阵a被当做稀疏矩阵

(8)b_is_sparse:布尔型参数,如果是True,矩阵b被当做稀疏矩阵

(9)name:可选参数,操作的名称

返回结果:

(1)与输入张量a和输入张量b相同类型的张量,返回的结果张量最内层矩阵是输入矩阵a和输入矩阵b相应矩阵的乘积。

 

tf.math.multiply或tf.multiply:矩阵元素乘

tf.math.multiply(
    x,
    y,
    name=None
)

输入:

(1)张量x:数据类型可以是bfloat16, half, float32, float64, uint8, int8, uint16, int16, int32, int64, complex64, complex128

(2)张量y:数据类型跟x一样

返回结果:

(1)张量:跟输入张量同一种数据类型

 

三、实例

(1)矩阵乘(tf.linalg.matmul或tf.matmul)

>>> x = tf.constant([[1,2,3],[4,5,6]])
>>> sess = tf.Session()
>>> sess.run(tf.shape(x))
# array([2, 3])
>>> y = tf.constant([[1,2],[3,4],[5,6]])
>>> sess.run(tf.shape(y))
# array([3, 2])
>>> result = tf.matmul(x,y)
>>> sess.run(tf.shape(result))
# array([2, 2])

(2)矩阵元素乘(tf.math.multiply或tf.multiply)

>>> import tensorflow as tf
>>> x = tf.constant([1,2,3])
>>> y = tf.constant([4,5,6])
>>> result1 = tf.multiply(x,y)
>>> result1
<tf.Tensor 'Mul:0' shape=(3,) dtype=int32>
>>> sess = tf.Session()
>>> sess.run(result1)
array([ 4, 10, 18])

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

csdn-WJW

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值