TensorFlow元素级矩阵乘法

4

假设我有两个张量在tensorflow中,第一维表示批量训练样本的索引,其他维度表示某些数据向量或矩阵。例如:

vector_batch = tf.ones([64, 50])
matrix_batch = tf.ones([64, 50, 50])

我很好奇如何以最通俗易懂的方式进行向量*矩阵乘法,对于那些在第一维度上共享索引的向量和矩阵对,每对都要执行乘法。也就是说,最通俗易懂的写法是什么:
result = tf.empty([64,50])
for i in range(64):
    result[i,:] = tf.matmul(vector_batch[i,:], matrix_batch[i,:,:])

如何最好地组织输入向量的形状,使得这个过程尽可能简单/清晰?

2个回答

6

可能最符合惯用方式的方法是使用tf.batch_matmul()运算符(与tf.expand_dims()tf.squeeze()结合使用):

vector_batch = tf.placeholder(tf.float32, shape=[64, 50])
matrix_batch = tf.placeholder(tf.float32, shape=[64, 50, 50])

vector_batch_as_matrices = tf.expand_dims(vector_batch, 1)
# vector_batch_as_matrices.get_shape() ==> [64, 1, 50]

result = tf.batch_matmul(vector_batch_as_matrices, matrix_batch)
# result.get_shape() ==> [64, 1, 50]

result = tf.squeeze(result, [1])
# result.get_shape() ==> [64, 50]

4
在 TensorFlow 1 中不存在 tf.batch_matmul 函数,相应的等价代码是使用普通的矩阵乘法函数 matmul - Mr_and_Mrs_D

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接