释义:将两个矩阵中对应元素各自相乘
示例:
import tensorflow as tf
X = tf.constant([[1, 2, 3], [4, 5 ,6]], dtype=tf.float32, name=None)
Y = tf.constant([[1, 1, 1], [2, 2 ,2]], dtype=tf.float32, name=None)
Z = tf.multiply(X, Y) # 乘法操作,对应位置元素相乘
with tf.Session() as sess:
print(sess.run(Z))
[[ 1. 2. 3.]
[ 8. 10. 12.]]
释义:矩阵乘法
示例:
X = tf.constant([[1, 2, 3], [4, 5, 6]], dtype=tf.float32, name=None)
Y = tf.constant([[1, 2], [1, 2], [1, 2]], dtype=tf.float32, name=None)
Z = tf.matmul(X, Y) # 矩阵乘法操作
with tf.Session() as sess:
print(sess.run(Z))
[[ 6. 12.]
[15. 30.]]
释义:标量和张量相乘(标量乘矩阵或向量)
示例:
x = tf.constant(2, dtype=tf.float32, name=None)
Y1 = tf.constant(3, dtype=tf.float32, name=None)
Z1 = tf.scalar_mul(x, Y1) # 标量×标量
Y2 = tf.constant([1, 2, 3], dtype=tf.float32, name=None)
Z2 = tf.scalar_mul(x, Y2) # 标量×向量
Y3 = tf.constant([[1, 2, 3], [4, 5, 6]], dtype=tf.float32, name=None)
Z3 = tf.scalar_mul(x, Y3) # 标量×矩阵
with tf.Session() as sess:
print(sess.run(Z1))
print('='*30)
print(sess.run(Z2))
print('='*30)
print(sess.run(Z3))
6.0
==============================
[2. 4. 6.]
==============================
[[ 2. 4. 6.]
[ 8. 10. 12.]]