TensorFlow四则运算之乘法:tf.multiply()

1.作用:逐元素相乘

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

输入:

x:一个tensor,类型得是bfloat16, half, float32, float64, uint8, int8, uint16, int16, int32, int64, complex64, complex128.
y:跟x同类型。

输出:

逐个元素相乘后的结果。

import tensorflow as tf
a = tf.constant([[3, 2], [5, 1]])
b = tf.constant([[1, 6], [2, 9]])
c = tf.multiply(a, b)
sess = tf.Session()
print sess.run(c)
输出:
[[ 3 12]     【3*1,2*6】
 [10  9]]    【5*2,1*9】

变量广播(有一个维度上,相等,便可在另外一个维度上进行广播):

import tensorflow as tf
a = tf.constant([[3, 2]]) #广播 为[[3, 2], [3, 2]]
b = tf.constant([[1, 6], [2, 9]])
c = tf.multiply(a, b)
sess = tf.Session()
print sess.run(c)
输出:
[[ 3 12]
 [ 6 18]]

import tensorflow as tf
a = tf.constant([[3]]) #广播 为[[3, 3], [3, 3]]
b = tf.constant([[1, 6], [2, 9]])
c = tf.multiply(a, b)
sess = tf.Session()
print sess.run(c)
输出:
[[ 3 18]
 [ 6 27]]

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值