tensorflow逐元素矩阵乘法 [英] tensorflow element-wise matrix multiplication

查看:337
本文介绍了tensorflow逐元素矩阵乘法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我在张量流中有两个张量,第一个维代表一批训练实例的索引,其他维代表数据矩阵的一些向量.例如

Say I have two tensors in tensorflow, with the first dimension representing the index of a training example in a batch, and the others representing some vectors of matrices of data. Eg

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

我很好奇执行向量*矩阵的最惯用方式是,对于向量的每对向量,矩阵沿第一个维度共享一个索引.

I'm curious what the most idiomatic way to perform a vector*matrix multiply, for each of the pairs of vectors, matrices that share an index along the first dimension.

又是最惯用的书写方式:

Aka a the most idiomatic way to write:

result = tf.empty([64,50])
for i in range(64):
    result[i,:] = tf.matmul(vector_batch[i,:], matrix_batch[i,:,:])

组织输入矢量形状以使此过程尽可能简单/干净的最佳方法是什么?

What would be the best way to organize the shape of the input vectors to make this process as simple/clean as possible?

推荐答案

可能最惯用的方法是使用

Probably the most idiomatic way to do this is using tf.batch_matmul() operator (in conjunction with tf.expand_dims() and 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]

这篇关于tensorflow逐元素矩阵乘法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆