TensorFlow - tf.multiply和tf.matmul 区别

 a
# [[1, 2, 3],
#  [4, 5, 6]] a = tf.constant([1, 2, 3, 4, 5, 6], shape=[2, 3])

# b1
# [[ 7,  8],
#  [ 9, 10],
#  [11, 12]] b1 = tf.constant([7, 8, 9, 10, 11, 12], shape=[3, 2])

#b2
#[[ 7  8  9]
# [10 11 12]] b2 = tf.constant([7, 8, 9, 10, 11, 12], shape=[2, 3])


# c矩阵相乘 第一个矩阵的列数(column)等于第二个矩阵的行数(row)
# [[ 58,  64],
#  [139, 154]] c = tf.matmul(a, b1)

# d`数元素各自相乘
#[[ 7 16 27]
# [40 55 72]] d = tf.multiply(a, b2) #维度必须相等 with tf.Session():
    print(d.eval())

关于其他计算

b3 = tf.constant([7, 8, 9,], shape=[1, 3])
tf.multiply(a, b3)
结果是
[[ 7 16 27]
 [28 40 54]]


b4 = tf.constant([7, 8], shape=[2, 1])
tf.multiply(a, b4)
结果是
[[ 7 14 21]
 [32 40 48]]

b5 = tf.constant([7], shape=[1, 1])
tf.multiply(a, b5)
结果是
[[ 7 14 21]
 [28 35 42]]

猜你喜欢

转载自blog.csdn.net/qq_30638831/article/details/80634503