功能
将矩阵 a 乘以矩阵 b,生成a * b
语法
matmul(
a,
b,
transpose_a=False,
transpose_b=False,
adjoint_a=False,
adjoint_b=False,
a_is_sparse=False,
b_is_sparse=False,
name=None
)
代码
# import tensorflow as tf
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
x = tf.constant([[3, 5], [2, 1]])
w = tf.constant([[7, 4], [2, 4]])
b = tf.constant([1])
z1 = tf.matmul(x, w)
z2 = tf.matmul(x, w) + b
with tf.Session() as sess:
print('z1:\n', sess.run(z1))
print('z2:\n', sess.run(z2))
z1:
[[31 32]
[16 12]]
z2:
[[32 33]
[17 13]]
shape
# import tensorflow as tf
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
# x = tf.constant([[3, 5], [2, 1]])
x = tf.constant([3, 5, 2, 1], shape=[2, 2])
# w = tf.constant([[7, 4], [2, 4]])
w = tf.constant([7, 4, 2, 4], shape=[2, 2])
b = tf.constant([1])
z1 = tf.matmul(x, w)
z2 = tf.matmul(x, w) + b
with tf.Session() as sess:
print('z1:\n', sess.run(z1))
print('z2:\n', sess.run(z2))
z1:
[[31 32]
[16 12]]
z2:
[[32 33]
[17 13]]