首页 > 解决方案 > Dot product between constant vector and variable with batch size in Tensorflow

问题描述

In tensorflow I have:

  1. A constant of 2000 vectors with dimension 1500. (dim = (2000, 1500)) named X
  2. A batch input variable of 75 vectors with dimension 1500 (dim = (?, 75, 1500)) named y

I want the dot product of each vector of X between each vector of y result in a vector of dimension (?, 75, 2000)

Is there a way I can do it using the dot or batch_dot?

标签: tensorflowkerasvectortensorflow2.0

解决方案


是的。

使用tf.matmul()。它将适用于未知批次。


import tensorflow as tf


# random X
X = tf.random.normal([2000, 1500])

print(X.shape)
# (2000, 1500)

# variable-batch y
y = tf.keras.Input([75, 1500])

print(y.shape)
# (None, 75, 1500)

# dot-product
out = tf.matmul(y, X, transpose_b=True)

print(out.shape)
# (None, 75, 2000)

推荐阅读