本文转载自:TensorFlow四则运算之乘法:tf.multiply()_tensorflow multiply-优快云博客
值得注意的是,TensorFlow中的乘法和R语言中的乘法是不一样的,例如,在R中,矩阵nXp 乘一个n维向量,会在每一列上乘上这个向量,但是在Python中,是矩阵nXp 乘一个p维向量,是在矩阵的每行上乘这个p维向量。
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]]
————————————————
版权声明:本文为优快云博主「凝眸伏笔」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.youkuaiyun.com/pearl8899/article/details/108632784