tensorflow - tf.multiply and tf.matmul

tensorflow - tf.multiply and tf.matmul

tf.multiply
tf.multiply(
    x,
    y,
    name=None
)
Return x * y element-wise.

参数:

  • x: A Tensor. Must be one of the following types: bfloat16, half, float32, float64, uint8, int8, uint16, int16, int32, int64,complex64, complex128
  • y: A Tensor. Must have the same type as x
  • name: A name for the operation (optional)
  • 返回值为x 和 y各个元素分别相乘

tf.multiply(a,b)a*b的结果相同
一般来说,x 和y 的维度需要相同,输出的值z 和x, y的维度也相同 [3,2]=tf.multiply([3,2],[3,2]),也可以是[3,2]=tf.multiply([3,1],[3,2])

x=tf.constant([1,2,3,4,5,6],shape=[3,2])
y=tf.constant([1,2,3,4,5,6],shape=[2,3])
a=tf.constant([[1,2,3]])
with tf.Session() as sess:

    print("x:\n",sess.run(x))
    print("y:\n",sess.run(y))
    print("a:\n",sess.run(a))

x:
[[1 2]
[3 4]
[5 6]]
y:
[[1 2 3]
[4 5 6]]
a:
[[1 2 3]]
b:
2

with tf.Session() as sess:
    print("x*x:\n",sess.run(x*x))
    print("tf.multiply(x,x):\n",sess.run(tf.multiply(x,x)))
    print("tf.multiply(y,a):\n",sess.run(tf.multiply(y,a)))
    print("tf.multiply(y,b):\n",sess.run(tf.multiply(y,b)))

x*x:
[[ 1 4]
[ 9 16]
[25 36]]
tf.multiply(x,x):
[[ 1 4]
[ 9 16]
[25 36]]
tf.multiply(y,a):
[[ 1 4 9]
[ 4 10 18]]
tf.multiply(y,b):
[[ 2 4 6]
[ 8 10 12]]

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

就是正常的矩阵乘法运算

print("tf.matmul(x,y):\n",sess.run(tf.matmul(x,y)))

tf.matmul(x,y):
[[ 9 12 15]
[19 26 33]
[29 40 51]]

a = tf.constant(np.arange(1, 13, dtype=np.int32),
                shape=[2, 2, 3])
b = tf.constant(np.arange(1, 13, dtype=np.int32),
                shape=[2, 3, 2])
with tf.Session() as sess:
    print("a:\n",sess.run(a))
    print("b:\n",sess.run(b))
    print("tf.matmul(a,b):\n",sess.run(tf.matmul(a,b)))

a:
[[[ 1 2 3]
[ 4 5 6]]

[[ 7 8 9]
[10 11 12]]]
b:
[[[ 1 2]
[ 3 4]
[ 5 6]]

[[ 7 8]
[ 9 10]
[11 12]]]
tf.matmul(a,b):
[[[ 22 28]
[ 49 64]]

[[220 244]
[301 334]]]

参考:https://tensorflow.google.cn/api_docs/python/tf/matmul
https://tensorflow.google.cn/api_docs/python/tf/multiply

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值