tensorflow - Dot product between constant vector and variable with batch size in Tensorflow
问题描述
In tensorflow I have:
- A constant of 2000 vectors with dimension 1500. (dim = (2000, 1500)) named X
- 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?
解决方案
是的。
使用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)