Taa*_*aam 4 matrix matrix-multiplication tensorflow
假设我在tensorflow中有两个张量,第一个维度表示批处理中训练示例的索引,其他维度表示一些数据矩阵向量.例如
vector_batch = tf.ones([64, 50])
matrix_batch = tf.ones([64, 50, 50])
Run Code Online (Sandbox Code Playgroud)
我很好奇是什么最常用的方法来执行向量*矩阵乘法,对于每对向量,共享沿第一维度的索引的矩阵.
Aka是最惯用的写作方式:
result = tf.empty([64,50])
for i in range(64):
result[i,:] = tf.matmul(vector_batch[i,:], matrix_batch[i,:,:])
Run Code Online (Sandbox Code Playgroud)
组织输入向量形状以使此过程尽可能简单/干净的最佳方法是什么?
可能最常用的方法是使用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]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8466 次 |
| 最近记录: |