Tensorflow—tf.multiply与tf.matmul的区别
在Tensorflow中,关于张量tensor有两个比较常见的乘法操作
tf.multiply:两个shape相同的数组,相应位置相乘
tf.matmul:两个数组,按照矩阵乘法进行行列相乘
代码展示
import tensorflow as tf
with tf.Graph().as_default():
a = tf.constant(value=[[1,1,1],[1,1,1],[1,1,1]], dtype=tf.float32, shape=[3,3], name="a")
b = tf.constant(value=[[1,1,1],[1,1,1],[1,1,1]], dtype=tf.float32, shape=[3,3], name="b")
multiply_res = tf.multiply(a,b)
matmul_res = tf.matmul(a,b)
with tf.Session() as sess:
print('multiply_res:',multiply_res.eval())
print('matmul_res:',matmul_res.eval())
'''
运行结果为:
multiply_res: [[1. 1. 1.]
[1. 1. 1.]
[1. 1. 1.]]
matmul_res: [[3. 3. 3.]
[3. 3. 3.]
[3. 3. 3.]]
'''