Matrix 张量流元素矩阵乘法
假设我在tensorflow中有两个张量,第一个维度表示一批训练示例的索引,其他维度表示数据矩阵的一些向量。乙二醇Matrix 张量流元素矩阵乘法,matrix,matrix-multiplication,tensorflow,Matrix,Matrix Multiplication,Tensorflow,假设我在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,:]
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,:,:])
组织输入向量形状以使该过程尽可能简单/干净的最佳方法是什么?最惯用的方法可能是使用运算符(与and结合使用):
似乎tf.matmul已经支持这种类型的“nd”操作:Hmm
tf.batch\u matmul
在tf 1中不存在-等效代码是什么?普通matmul?
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]