【问题标题】:Broadcast dot product in tensorflow张量流中的广播点积
【发布时间】:2017-06-06 06:59:40
【问题描述】:

在tensorflow中,我有以下问题。

我有一个形状为 [batch_size, dim_a, dim_b] 的张量 m 和一个形状为 [batch_size, dim_b] 的矩阵 u

M = tf.constant(shape=[batch_size, sequence_size, embed_dim])
U = tf.constant(shape=[batch_size, embed_dim])

我要实现的是我批次的每个索引的 [i, dim_a, dim_b] x [i, dim_b] 的点积。

P[i] = tf.matmul(M[i, :, :], tf.expand_dims(U[i, :], 1)) for each i.

基本上,在批处理轴上广播点积。这可能吗?我该如何实现?

【问题讨论】:

  • 由于每个向量都是一个有 1 列的矩阵,因此您可以重塑 U。Tensorflow 在 1.0 版本之前曾经有 batch_matmul,现在已与 matmul 合并。

标签: python tensorflow


【解决方案1】:

这可以通过 tf.einsum() 来实现:

import tensorflow as tf
import numpy as np

batch_size = 2
sequence_size = 3
embed_dim = 4

M = tf.constant(range(batch_size * sequence_size * embed_dim), shape=[batch_size, sequence_size, embed_dim])
U = tf.constant(range(batch_size, embed_dim), shape=[batch_size, embed_dim])

prod = tf.einsum('bse,be->bs', M, U)

with tf.Session():
  print "M"
  print M.eval()
  print
  print "U"
  print U.eval()
  print
  print "einsum result"
  print prod.eval()
  print

  print "numpy, example 0"
  print np.matmul(M.eval()[0], U.eval()[0])
  print
  print "numpy, example 1"
  print np.matmul(M.eval()[1], U.eval()[1])

输出:

M
[[[ 0  1  2  3]
  [ 4  5  6  7]
  [ 8  9 10 11]]

 [[12 13 14 15]
  [16 17 18 19]
  [20 21 22 23]]]

U
[[2 3 3 3]
 [3 3 3 3]]

einsum result
[[ 18  62 106]
 [162 210 258]]

numpy, example 0
[ 18  62 106]

numpy, example 1
[162 210 258]

【讨论】:

    猜你喜欢
    • 2017-11-01
    • 2018-12-01
    • 2017-01-30
    • 2021-07-05
    • 1970-01-01
    • 2017-04-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-04
    相关资源
    最近更新 更多